smartsocket/test/test.expressserver.ts

104 lines
3.0 KiB
TypeScript
Raw Normal View History

2019-04-24 15:57:16 +00:00
// tslint:disable-next-line:no-implicit-dependencies
2023-07-21 01:53:41 +00:00
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
2019-04-24 15:57:16 +00:00
2023-07-21 01:53:41 +00:00
import * as isohash from '@push.rocks/isohash';
2023-09-09 21:32:12 +00:00
import * as smartexpress from '@api.global/typedserver';
2019-04-24 15:57:16 +00:00
2022-03-14 21:40:55 +00:00
import * as smartsocket from '../ts/index.js';
2019-04-24 15:57:16 +00:00
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
2019-09-09 21:58:32 +00:00
let testSocketFunction1: smartsocket.SocketFunction<any>;
2023-07-21 01:53:41 +00:00
let myseServer: smartexpress.servertools.Server;
2019-04-24 15:57:16 +00:00
const testConfig = {
2021-01-23 04:12:55 +00:00
port: 3000,
2019-04-24 15:57:16 +00:00
};
// class smartsocket
tap.test('should create a new smartsocket', async () => {
2022-01-19 14:34:52 +00:00
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver', port: testConfig.port });
2022-03-14 21:40:55 +00:00
expect(testSmartsocket).toBeInstanceOf(smartsocket.Smartsocket);
2019-04-24 15:57:16 +00:00
});
tap.test('Should accept an smartExpressServer as server', async () => {
2023-07-21 01:53:41 +00:00
myseServer = new smartexpress.servertools.Server({
2019-04-24 15:57:16 +00:00
cors: true,
forceSsl: false,
2020-09-24 18:04:11 +00:00
port: testConfig.port,
2019-04-24 15:57:16 +00:00
});
testSmartsocket.setExternalServer('smartexpress', myseServer);
await myseServer.start();
});
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({
2019-08-12 20:31:40 +00:00
funcDef: async (dataArg, socketConnectionArg) => {
2019-04-24 15:57:16 +00:00
return dataArg;
},
2020-09-24 18:04:11 +00:00
funcName: 'testFunction1',
2019-04-24 15:57:16 +00:00
});
2019-08-12 20:31:40 +00:00
testSmartsocket.addSocketFunction(testSocketFunction1);
console.log(testSmartsocket.socketFunctions);
});
tap.test('should start listening when .started is called', async () => {
await testSmartsocket.start();
2019-04-24 15:57:16 +00:00
});
// class SmartsocketClient
tap.test('should react to a new websocket connection from client', async () => {
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: 'http://localhost',
alias: 'testClient1',
});
2019-08-12 20:31:40 +00:00
testSmartsocketClient.addSocketFunction(testSocketFunction1);
await testSmartsocketClient.connect();
2019-04-24 15:57:16 +00:00
});
2019-08-12 20:31:40 +00:00
2020-09-24 18:04:11 +00:00
tap.test('client should disconnect and reconnect', async (tools) => {
2019-08-12 20:31:40 +00:00
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
2019-04-24 15:57:16 +00:00
});
tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test
});
tap.test('should be able to make a functionCall from client to server', async () => {
2020-12-16 02:20:28 +00:00
const totalCycles = 20000;
2020-12-16 01:38:57 +00:00
let counter = 0;
let startTime = Date.now();
while (counter < totalCycles) {
2020-12-16 02:20:28 +00:00
const randomString = `hello ${Math.random()}`;
const response: any = await testSmartsocketClient.serverCall('testFunction1', {
value1: randomString,
2020-12-16 01:38:57 +00:00
});
2022-03-14 21:40:55 +00:00
expect(response.value1).toEqual(randomString);
2020-12-16 01:38:57 +00:00
if (counter % 100 === 0) {
2020-12-16 02:20:28 +00:00
console.log(
`processed 100 more messages in ${Date.now() - startTime}ms. ${
totalCycles - counter
} messages to go.`
);
2020-12-16 01:38:57 +00:00
startTime = Date.now();
2020-12-16 02:20:28 +00:00
}
2020-12-16 01:38:57 +00:00
counter++;
}
2019-04-24 15:57:16 +00:00
});
2019-08-12 20:46:57 +00:00
tap.test('should be able to make a functionCall from server to client', async () => {});
2019-04-24 15:57:16 +00:00
// terminate
tap.test('should close the server', async () => {
await testSmartsocket.stop();
await myseServer.stop();
});
tap.start();