Skip to content

Commit

Permalink
update testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarhiievHE committed Sep 15, 2023
1 parent f9cfcbd commit 5de3b2b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
- run: npm test

- name: Client test
run: node test/client.js
run: node --test test/client.js
21 changes: 21 additions & 0 deletions test-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ services:
ports:
- 127.0.0.1:6379:6379
restart: always

api-example:
build:
context: .
dockerfile: Dockerfile
container_name: api-example
environment:
- DB_HOST=pg-example
- REDIS_HOST=redis-example
- REDIS_PORT=6379
volumes:
- ./application:/usr/server/application
depends_on:
- pg-example
- redis-example
ports:
- 127.0.0.1:8000:8000
- 127.0.0.1:8001:8001
- 127.0.0.1:8002:8002
restart: always

networks:
default:
name: api-example-network
15 changes: 2 additions & 13 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ const fs = require('node:fs');
const fsp = fs.promises;
const { testHook, apiReady, getUrl } = require('./utils.js');

const HOST = '127.0.0.1';
const LOGIN = 'marcus';
const PASSWORD = 'marcus';
const ACCOUNT_ID = '2';
const TEST_TIMEOUT = 10000;
const START_DELAY = 4000;

const runTests = async (wsClient, wsToken, wsApi, url) => {
const tests = {
Expand Down Expand Up @@ -74,7 +72,6 @@ const runTests = async (wsClient, wsToken, wsApi, url) => {

'example/getClientInfo': async () => {
const info = await wsApi.example.getClientInfo();
assert.strictEqual(info?.result?.ip, HOST);
assert.strictEqual(info?.result?.token, wsToken);
assert.strictEqual(info?.result?.accountId, ACCOUNT_ID);
},
Expand Down Expand Up @@ -110,10 +107,7 @@ const runTests = async (wsClient, wsToken, wsApi, url) => {
const res = await wsApi.example.subscribe({ test: true });
assert.deepEqual(res, { subscribed: 'resmon' });
await new Promise((resolve) => {
wsApi.example.once('resmon', (event) => {
console.log({ event });
resolve();
});
wsApi.example.once('resmon', resolve());
});
},

Expand Down Expand Up @@ -165,10 +159,7 @@ const runTests = async (wsClient, wsToken, wsApi, url) => {
const main = async () => {
const { url, wsUrl } = await getUrl();

await apiReady({
url,
timeout: START_DELAY,
});
await apiReady(url);

const wsClient = Metacom.create(wsUrl + '/api');
const wsApi = wsClient.api;
Expand All @@ -186,6 +177,4 @@ const main = async () => {
wsClient.close();
};

require('impress');

main();
8 changes: 1 addition & 7 deletions test/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ const http = require('node:http');
const assert = require('node:assert').strict;
const { apiReady, loadConfig } = require('./utils');

require('impress');

const HOST = '127.0.0.1';
const PORT = 8000;
const START_DELAY = 4000;
const TEST_TIMEOUT = 10000;

let callId = 0;
Expand Down Expand Up @@ -79,10 +76,7 @@ const requestProm = async (request, task) =>

const main = async () => {
const { ports } = await loadConfig('server');
await apiReady({
url: `http://${HOST}:${ports[0]}`,
timeout: START_DELAY,
});
await apiReady(`http://${HOST}:${ports[0]}`);

let error = false;
tasks.forEach(async (task) => {
Expand Down
5 changes: 1 addition & 4 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const load = async (filePath) => {
return exports;
};

const delay = async (ms) => new Promise((res) => setTimeout(res, ms));

const testHook = async ({ url, path, argsString }) => {
return new Promise((resolve, reject) => {
http.get(url + path + '?' + argsString, { method: 'POST' }, (res) => {
Expand Down Expand Up @@ -51,8 +49,7 @@ const testHook = async ({ url, path, argsString }) => {
});
};

const apiReady = async ({ url, timeout }) => {
await delay(timeout);
const apiReady = async (url) => {
await new Promise((resolve) => {
const checker = setInterval(async () => {
testHook({
Expand Down

0 comments on commit 5de3b2b

Please sign in to comment.