Skip to content

Commit

Permalink
Tests run concurrently
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Jan 17, 2025
1 parent 043822f commit ea6bfc0
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 30 deletions.
25 changes: 18 additions & 7 deletions src/api/tests/globalSetup.ts → src/api/tests/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import assert from "assert";
import IBMi from "../IBMi";
import { ENV_CREDS } from "./env";
import { afterAll, beforeAll, expect } from "vitest";
import { CodeForIStorage } from "../configuration/storage/CodeForIStorage";
import { CustomQSh } from "../components/cqsh";
import path from "path";
Expand All @@ -11,8 +8,17 @@ import { GetNewLibl } from "../components/getNewLibl";
import { extensionComponentRegistry } from "../components/manager";
import { JsonConfig, JsonStorage } from "./testConfigSetup";

export const testStorage = new JsonStorage();
const testConfig = new JsonConfig();
const testStorage = new JsonStorage();

export const CONNECTION_TIMEOUT = process.env.VITE_CONNECTION_TIMEOUT ? parseInt(process.env.VITE_CONNECTION_TIMEOUT) : 25000;

const ENV_CREDS = {
host: process.env.VITE_SERVER || `localhost`,
user: process.env.VITE_DB_USER,
password: process.env.VITE_DB_PASS,
port: parseInt(process.env.VITE_DB_PORT || `22`)
}

export async function newConnection() {
const virtualStorage = testStorage;
Expand Down Expand Up @@ -62,13 +68,18 @@ export async function newConnection() {
}
);

expect(result).toBeDefined();
expect(result.success).toBeTruthy();
if (!result.success) {
throw new Error(`Failed to connect to IBMi`);
}

return conn;
}

export function disposeConnection(conn: IBMi) {
export function disposeConnection(conn?: IBMi) {
if (!conn) {
return;
}

conn.dispose();
testStorage.save();
testConfig.save();
Expand Down
11 changes: 0 additions & 11 deletions src/api/tests/env.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/api/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { TestProject } from "vitest/node";
import { disposeConnection, newConnection, testStorage } from "./connection";

export async function setup(project: TestProject) {
// You might pre-connect to simply create the configuration files.
// When the config files exist, it makes future connections just slightly faster.
// Mostly useful during the CI stage.
if (!testStorage.exists()) {
console.log(``);
console.log(`Testing connection before tests run since configs do not exist.`);

const conn = await newConnection();
disposeConnection(conn);

console.log(`Testing connection complete. Configs written.`);
console.log(``);
}
}
4 changes: 2 additions & 2 deletions src/api/tests/suites/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { GetMemberInfo } from '../../components/getMemberInfo';
import { GetNewLibl } from '../../components/getNewLibl';
import { Tools } from '../../Tools';
import IBMi from '../../IBMi';
import { disposeConnection, newConnection } from '../globalSetup';
import { CONNECTION_TIMEOUT, disposeConnection, newConnection } from '../connection';

describe('Component Tests', () => {
let connection: IBMi
beforeAll(async () => {
connection = await newConnection();
})
}, CONNECTION_TIMEOUT)

afterAll(async () => {
disposeConnection(connection);
Expand Down
4 changes: 2 additions & 2 deletions src/api/tests/suites/connection.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

import { expect, describe, afterAll, beforeAll, it } from 'vitest'
import { Tools } from '../../Tools';
import { disposeConnection, newConnection } from '../globalSetup';
import { disposeConnection, newConnection } from '../connection';
import IBMi from '../../IBMi';
import { getJavaHome } from '../../configuration/DebugConfiguration';

describe(`connection tests`, {concurrent: true}, () => {
let connection: IBMi
beforeAll(async () => {
connection = await newConnection();
})
}, 25000)

afterAll(async () => {
disposeConnection(connection);
Expand Down
2 changes: 1 addition & 1 deletion src/api/tests/suites/content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tmp from 'tmp';
import { Tools } from '../../Tools';
import { posix } from 'path';
import IBMi from '../../IBMi';
import { newConnection, disposeConnection } from '../globalSetup';
import { newConnection, disposeConnection } from '../connection';

describe('Content Tests', {concurrent: true}, () => {
let connection: IBMi
Expand Down
4 changes: 2 additions & 2 deletions src/api/tests/suites/encoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import IBMi from "../../IBMi";
import { Tools } from "../../Tools";
import { IBMiObject } from "../../types";
import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import { newConnection, disposeConnection } from "../globalSetup";
import { newConnection, disposeConnection } from "../connection";

const contents = {
'37': [`Hello world`],
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Encoding tests', {concurrent: true} ,() => {
let connection: IBMi
beforeAll(async () => {
connection = await newConnection();
})
}, 25000)

afterAll(async () => {
disposeConnection(connection);
Expand Down
4 changes: 2 additions & 2 deletions src/api/tests/suites/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import { parseFilter } from '../../Filter';
import { Search } from '../../Search';
import IBMi from '../../IBMi';
import { newConnection, disposeConnection } from '../globalSetup';
import { newConnection, disposeConnection } from '../connection';

describe('Search Tests', {concurrent: true}, () => {
let connection: IBMi
beforeAll(async () => {
connection = await newConnection();
})
}, 25000)

afterAll(async () => {
disposeConnection(connection);
Expand Down
6 changes: 5 additions & 1 deletion src/api/tests/testConfigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export class JsonStorage extends BaseStorage {
super();
}

exists() {
return existsSync(storagePath);
}

public async load() {
if (existsSync(storagePath)) {
if (this.exists()) {
const data = await import(storagePath);
for (const key in data) {
this.globalState.set(key, data[key]);
Expand Down
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { defineConfig } from 'vite'
export default defineConfig({
test: {
// ... Specify options here.
fileParallelism: false,
root: './src/api',
testTimeout: 10000
globalSetup: [`tests/setup.ts`],
testTimeout: 10000,
},
})

0 comments on commit ea6bfc0

Please sign in to comment.