fix(core): update
This commit is contained in:
parent
c424d589ea
commit
4e7455fa26
@ -1,5 +1,150 @@
|
|||||||
import { tap, expect } from '@pushrocks/tapbundle';
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
tap.test('should run a test', async () => {});
|
import smartsocket = require('../ts/index');
|
||||||
|
|
||||||
|
let testSmartsocket: smartsocket.Smartsocket;
|
||||||
|
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
||||||
|
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
|
||||||
|
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
|
||||||
|
|
||||||
|
export interface IReqResClient {
|
||||||
|
method: 'testFunction1';
|
||||||
|
request: {
|
||||||
|
value1: string;
|
||||||
|
};
|
||||||
|
response: {
|
||||||
|
value1: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IReqResServer {
|
||||||
|
method: 'testFunction2';
|
||||||
|
request: {
|
||||||
|
hi: string;
|
||||||
|
};
|
||||||
|
response: {
|
||||||
|
hi: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const testConfig = {
|
||||||
|
port: 3000,
|
||||||
|
};
|
||||||
|
|
||||||
|
// class smartsocket
|
||||||
|
tap.test('should create a new smartsocket', async () => {
|
||||||
|
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver1', port: testConfig.port });
|
||||||
|
await testSmartsocket.start();
|
||||||
|
});
|
||||||
|
|
||||||
|
// class SocketFunction
|
||||||
|
tap.test('should register a new Function', async () => {
|
||||||
|
testSocketFunctionForServer = new smartsocket.SocketFunction({
|
||||||
|
funcDef: async (dataArg, socketConnectionArg) => {
|
||||||
|
return dataArg;
|
||||||
|
},
|
||||||
|
funcName: 'testFunction1',
|
||||||
|
});
|
||||||
|
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||||
|
|
||||||
|
testSocketFunctionClient = new smartsocket.SocketFunction({
|
||||||
|
funcDef: async (dataArg, socketConnectionArg) => {
|
||||||
|
return dataArg;
|
||||||
|
},
|
||||||
|
funcName: 'testFunction2',
|
||||||
|
});
|
||||||
|
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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',
|
||||||
|
autoReconnect: true,
|
||||||
|
});
|
||||||
|
testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
|
||||||
|
await testSmartsocketClient.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to tag a connection from client', async (tools) => {
|
||||||
|
await testSmartsocketClient.addTag({
|
||||||
|
id: 'awesome',
|
||||||
|
payload: 'yes',
|
||||||
|
});
|
||||||
|
const tagOnServerSide = await testSmartsocket.socketConnections
|
||||||
|
.findSync((socketConnection) => {
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.getTagById('awesome');
|
||||||
|
expect(tagOnServerSide.payload).to.equal('yes');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to tag a connection from server', async (tools) => {
|
||||||
|
await testSmartsocket.socketConnections
|
||||||
|
.findSync((socketConnection) => {
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.addTag({
|
||||||
|
id: 'awesome2',
|
||||||
|
payload: 'absolutely',
|
||||||
|
});
|
||||||
|
const tagOnClientSide = await testSmartsocketClient.socketConnection.getTagById('awesome2');
|
||||||
|
expect(tagOnClientSide.payload).to.equal('absolutely');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to make a functionCall from client to server', async () => {
|
||||||
|
const response = await testSmartsocketClient.serverCall<IReqResClient>('testFunction1', {
|
||||||
|
value1: 'hello',
|
||||||
|
});
|
||||||
|
console.log(response);
|
||||||
|
expect(response.value1).to.equal('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to make a functionCall from server to client', async () => {
|
||||||
|
const response = await testSmartsocket.clientCall<IReqResServer>(
|
||||||
|
'testFunction2',
|
||||||
|
{
|
||||||
|
hi: 'hi there from server',
|
||||||
|
},
|
||||||
|
testSmartsocket.socketConnections.findSync((socketConnection) => {
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
console.log(response);
|
||||||
|
expect(response.hi).to.equal('hi there from server');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('client should disconnect and reconnect', async (tools) => {
|
||||||
|
await testSmartsocketClient.disconnect();
|
||||||
|
await testSmartsocketClient.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
// class smartsocket
|
||||||
|
tap.test('should be able to switch to a new server', async (toolsArg) => {
|
||||||
|
await testSmartsocket.stop();
|
||||||
|
await toolsArg.delayFor(5000);
|
||||||
|
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver2', port: testConfig.port });
|
||||||
|
await testSmartsocket.start();
|
||||||
|
await toolsArg.delayFor(30000);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to locate a connection tag after reconnect', async (tools) => {
|
||||||
|
console.log(testSmartsocket.socketConnections.getArray().length);
|
||||||
|
const tagOnServerSide = await testSmartsocket.socketConnections
|
||||||
|
.findSync((socketConnection) => {
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
.getTagById('awesome');
|
||||||
|
expect(tagOnServerSide.payload).to.equal('yes');
|
||||||
|
});
|
||||||
|
|
||||||
|
// terminate
|
||||||
|
tap.test('should close the server', async () => {
|
||||||
|
await testSmartsocketClient.stop();
|
||||||
|
await testSmartsocket.stop();
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -2,11 +2,9 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import smartsocket = require('../ts/index');
|
import smartsocket = require('../ts/index');
|
||||||
import * as isohash from '@pushrocks/isohash';
|
|
||||||
|
|
||||||
let testSmartsocket: smartsocket.Smartsocket;
|
let testSmartsocket: smartsocket.Smartsocket;
|
||||||
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
||||||
let testSocketConnection: smartsocket.SocketConnection;
|
|
||||||
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
|
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
|
||||||
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
|
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ export class SmartsocketClient {
|
|||||||
* try a reconnection
|
* try a reconnection
|
||||||
*/
|
*/
|
||||||
public async tryDebouncedReconnect() {
|
public async tryDebouncedReconnect() {
|
||||||
await plugins.smartdelay.delayForRandom(10000, 50000);
|
await plugins.smartdelay.delayForRandom(10000, 20000);
|
||||||
await this.connect();
|
await this.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user