BREAKING CHANGE(smartsocket): Replace setExternalServer with hooks-based SmartServe integration and refactor SocketServer to support standalone and hooks modes
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
|
||||
import * as smartsocket from '../ts/index.js';
|
||||
|
||||
let testSmartsocket: smartsocket.Smartsocket;
|
||||
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
||||
let testSocketFunction1: smartsocket.SocketFunction<any>;
|
||||
|
||||
const testConfig = {
|
||||
port: 3000,
|
||||
};
|
||||
|
||||
// class smartsocket
|
||||
tap.test('should create a new smartsocket', async () => {
|
||||
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver', port: testConfig.port });
|
||||
expect(testSmartsocket).toBeInstanceOf(smartsocket.Smartsocket);
|
||||
});
|
||||
|
||||
// class SocketFunction
|
||||
tap.test('should register a new Function', async () => {
|
||||
testSocketFunction1 = new smartsocket.SocketFunction({
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return dataArg;
|
||||
},
|
||||
funcName: 'testFunction1',
|
||||
});
|
||||
testSmartsocket.addSocketFunction(testSocketFunction1);
|
||||
console.log(testSmartsocket.socketFunctions);
|
||||
});
|
||||
|
||||
tap.test('should start listening when .started is called', async () => {
|
||||
await testSmartsocket.start();
|
||||
});
|
||||
|
||||
// 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',
|
||||
});
|
||||
testSmartsocketClient.addSocketFunction(testSocketFunction1);
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
tap.test('client should disconnect and reconnect', async (tools) => {
|
||||
await testSmartsocketClient.disconnect();
|
||||
await tools.delayFor(100);
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
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 () => {
|
||||
const totalCycles = 100; // Reduced for faster test
|
||||
let counter = 0;
|
||||
let startTime = Date.now();
|
||||
while (counter < totalCycles) {
|
||||
const randomString = `hello ${Math.random()}`;
|
||||
const response: any = await testSmartsocketClient.serverCall('testFunction1', {
|
||||
value1: randomString,
|
||||
});
|
||||
expect(response.value1).toEqual(randomString);
|
||||
if (counter % 50 === 0) {
|
||||
console.log(
|
||||
`processed 50 more messages in ${Date.now() - startTime}ms. ${
|
||||
totalCycles - counter
|
||||
} messages to go.`
|
||||
);
|
||||
startTime = Date.now();
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should be able to make a functionCall from server to client', async () => {});
|
||||
|
||||
// terminate
|
||||
tap.test('should close the server', async () => {
|
||||
await testSmartsocketClient.stop();
|
||||
await testSmartsocket.stop();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
91
test/test.smartserve.ts
Normal file
91
test/test.smartserve.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as smartsocket from '../ts/index.js';
|
||||
import { SmartServe } from '@push.rocks/smartserve';
|
||||
|
||||
let smartserveInstance: SmartServe;
|
||||
let testSmartsocket: smartsocket.Smartsocket;
|
||||
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
||||
let testSocketFunction: smartsocket.SocketFunction<any>;
|
||||
|
||||
const testConfig = {
|
||||
port: 3000,
|
||||
};
|
||||
|
||||
// Setup smartsocket with smartserve integration
|
||||
tap.test('should create smartsocket and smartserve with websocket hooks', async () => {
|
||||
// Create smartsocket (no port - hooks mode for smartserve integration)
|
||||
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver-smartserve' });
|
||||
expect(testSmartsocket).toBeInstanceOf(smartsocket.Smartsocket);
|
||||
|
||||
// Get websocket hooks from smartsocket and pass to smartserve
|
||||
const wsHooks = testSmartsocket.getSmartserveWebSocketHooks();
|
||||
smartserveInstance = new SmartServe({
|
||||
port: testConfig.port,
|
||||
websocket: wsHooks,
|
||||
});
|
||||
// That's it! No setExternalServer needed - hooks connect everything
|
||||
});
|
||||
|
||||
tap.test('should register a socket function', async () => {
|
||||
testSocketFunction = new smartsocket.SocketFunction({
|
||||
funcDef: async (dataArg, socketConnectionArg) => {
|
||||
return dataArg;
|
||||
},
|
||||
funcName: 'testFunction1',
|
||||
});
|
||||
testSmartsocket.addSocketFunction(testSocketFunction);
|
||||
});
|
||||
|
||||
tap.test('should start smartserve', async () => {
|
||||
await smartserveInstance.start();
|
||||
// No need to call testSmartsocket.start() - hooks mode doesn't need it
|
||||
});
|
||||
|
||||
tap.test('should connect client through smartserve', async () => {
|
||||
testSmartsocketClient = new smartsocket.SmartsocketClient({
|
||||
port: testConfig.port,
|
||||
url: 'http://localhost',
|
||||
alias: 'testClient1',
|
||||
});
|
||||
testSmartsocketClient.addSocketFunction(testSocketFunction);
|
||||
await testSmartsocketClient.connect();
|
||||
});
|
||||
|
||||
tap.test('should be able to make a functionCall from client to server', async () => {
|
||||
const response: any = await testSmartsocketClient.serverCall('testFunction1', {
|
||||
value1: 'hello from smartserve test',
|
||||
});
|
||||
expect(response.value1).toEqual('hello from smartserve test');
|
||||
});
|
||||
|
||||
tap.test('should be able to make multiple function calls', async () => {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const randomString = `message-${i}-${Math.random()}`;
|
||||
const response: any = await testSmartsocketClient.serverCall('testFunction1', {
|
||||
value1: randomString,
|
||||
});
|
||||
expect(response.value1).toEqual(randomString);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('client should disconnect and reconnect through smartserve', async (tools) => {
|
||||
await testSmartsocketClient.disconnect();
|
||||
await tools.delayFor(100);
|
||||
await testSmartsocketClient.connect();
|
||||
|
||||
// Verify connection still works after reconnect
|
||||
const response: any = await testSmartsocketClient.serverCall('testFunction1', {
|
||||
value1: 'after reconnect',
|
||||
});
|
||||
expect(response.value1).toEqual('after reconnect');
|
||||
});
|
||||
|
||||
// Cleanup
|
||||
tap.test('should close the server', async (tools) => {
|
||||
await testSmartsocketClient.stop();
|
||||
await testSmartsocket.stop();
|
||||
await smartserveInstance.stop();
|
||||
tools.delayFor(1000).then(() => process.exit(0));
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -139,8 +139,10 @@ tap.test('should be able to locate a connection tag after reconnect', async (too
|
||||
});
|
||||
|
||||
// terminate
|
||||
tap.test('should close the server', async () => {
|
||||
tap.test('should close the server', async (tools) => {
|
||||
await testSmartsocketClient.stop();
|
||||
await testSmartsocket.stop();
|
||||
tools.delayFor(1000).then(() => process.exit(0));
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
Reference in New Issue
Block a user