Skip to content

Commit

Permalink
Merge pull request #653 from soketi/fix/resolves
Browse files Browse the repository at this point in the history
[1.x/fix] Resolves from 2.x
  • Loading branch information
rennokki committed Aug 18, 2022
2 parents 9d4fbb2 + 5f849d7 commit cc692c9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"lint": "eslint --ext .js,.ts ./src",
"prepublish": "npm run build",
"test": "jest --detectOpenHandles --forceExit --silent",
"test:local": "jest --detectOpenHandles --forceExit --verbose",
"test:watch": "npm test -- --watch"
},
"bin": {
Expand Down
18 changes: 9 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,22 @@ export class Server {
Log.warningTitle('⚡ All sockets were closed. Now closing the server.');
}

if (this.serverProcess) {
uWS.us_listen_socket_close(this.serverProcess);
}

if (this.metricsServerProcess) {
uWS.us_listen_socket_close(this.metricsServerProcess);
}

setTimeout(() => {
Promise.all([
this.metricsManager.clear(),
this.queueManager.disconnect(),
this.rateLimiter.disconnect(),
this.cacheManager.disconnect(),
]).then(() => {
this.adapter.disconnect().then(() => resolve());
this.adapter.disconnect().then(() => {
if (this.serverProcess) {
uWS.us_listen_socket_close(this.serverProcess);
}

if (this.metricsServerProcess) {
uWS.us_listen_socket_close(this.metricsServerProcess);
}
}).then(() => resolve());
});
}, this.options.shutdownGracePeriod);
});
Expand Down
6 changes: 5 additions & 1 deletion src/ws-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,11 @@ export class WsHandler {
this.clearTimeout(ws);

ws.timeout = setTimeout(() => {
ws.end(4201);
try {
ws.end(4201);
} catch (e) {
//
}
}, 120_000);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/presence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe('presence channel test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('user authentication works if conn immediately joins a presence channel', (done) => {
Utils.shouldRun(Utils.appManagerIs('array'))('user authentication works if conn immediately joins a presence channel', (done) => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let user = {
user_id: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('private channel test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('user authentication works if conn immediately joins a private channel', (done) => {
Utils.shouldRun(Utils.appManagerIs('array'))('user authentication works if conn immediately joins a private channel', (done) => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let client = Utils.newClientForPrivateChannel();
let channelName = `private-${Utils.randomChannelName()}`;
Expand Down
14 changes: 8 additions & 6 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class Utils {
'database.postgres.password': process.env.TEST_POSTGRES_PASSWORD || 'testing',
'database.postgres.database': process.env.TEST_POSTGRES_DATABASE || 'testing',
'queue.sqs.queueUrl': process.env.TEST_SQS_URL || 'http://localhost:4566/000000000000/test.fifo',
'debug': process.env.TEST_DEBUG || false,
'shutdownGracePeriod': 1_000,
};

return (new Server(options)).start((server: Server) => {
Expand Down Expand Up @@ -108,15 +110,15 @@ export class Utils {

let server = webhooksApp.listen(3001, () => {
Log.successTitle('🎉 Webhook Server is up and running!');
});

server.on('error', err => {
console.log('Websocket server error', err);
});
server.on('error', err => {
console.log('Websocket server error', err);
});

this.httpServers.push(server);
this.httpServers.push(server);

onReadyCallback(server);
onReadyCallback(server);
});
}

static flushWsServers(): Promise<void> {
Expand Down
26 changes: 15 additions & 11 deletions tests/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('webhooks test', () => {
return Utils.flushServers();
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from client events', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks from client events', done => {
let webhooks = [{
event_types: ['client_event'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from channel_occupied and channel_vacated', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks from channel_occupied and channel_vacated', done => {
let webhooks = [{
event_types: ['channel_occupied', 'channel_vacated'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from member_added and member_removed', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks from member_added and member_removed', done => {
let webhooks = [{
event_types: ['member_added', 'member_removed'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from member_added and member_removed alongside filtering', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks from member_added and member_removed alongside filtering', done => {
let channelName = `presence-${Utils.randomChannelName()}`;

let webhooks = [
Expand Down Expand Up @@ -334,7 +334,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('lambda webhooks', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('lambda webhooks', done => {
let webhooks = [{
event_types: ['client_event'],
lambda_function: 'some-lambda-function',
Expand All @@ -346,8 +346,8 @@ describe('webhooks test', () => {
}];

let channelName = `private-${Utils.randomChannelName()}`;
let client1 = Utils.newClientForPrivateChannel();
let client2 = Utils.newClientForPrivateChannel();
let client1;
let client2;

Utils.newServer({
'appManager.array.apps.0.enableClientMessages': true,
Expand All @@ -364,10 +364,14 @@ describe('webhooks test', () => {
client2.disconnect();
done();
}, (activeHttpServer) => {
client1 = Utils.newClientForPrivateChannel();

client1.connection.bind('connected', () => {
let channel = client1.subscribe(channelName);

channel.bind('pusher:subscription_succeeded', () => {
client2 = Utils.newClientForPrivateChannel();

client2.connection.bind('connected', () => {
let channel = client2.subscribe(channelName);

Expand All @@ -383,7 +387,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks filtered by channel name', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks filtered by channel name', done => {
const sharedWebhookConfig = {
event_types: ['channel_occupied'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -463,7 +467,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks can have custom headers', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks can have custom headers', done => {
const webhooks = [{
event_types: ['channel_occupied'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -504,7 +508,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local') && Utils.queueDriverIs('sqs'))('sqs batching', done => {
Utils.shouldRun(Utils.appManagerIs('array') && Utils.queueDriverIs('sqs'))('sqs batching', done => {
let webhooks = [{
event_types: ['client_event'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down Expand Up @@ -568,7 +572,7 @@ describe('webhooks test', () => {
});
}, 60 * 1000);

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('webhooks from cache_miss', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('webhooks from cache_miss', done => {
let webhooks = [{
event_types: ['cache_miss'],
url: 'http://127.0.0.1:3001/webhook',
Expand Down
12 changes: 6 additions & 6 deletions tests/ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('ws test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('client events for presence channels', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('client events for presence channels', done => {
Utils.newServer({ 'appManager.array.apps.0.enableClientMessages': true }, (server: Server) => {
let user1 = {
user_id: 1,
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('ws test', () => {
});
});
});
});
}, 60_000);

Utils.shouldRun(Utils.appManagerIs('array'))('client events dont get emitted when client messaging is disabled', done => {
Utils.newServer({ 'appManager.array.apps.0.enableClientMessages': false }, (server: Server) => {
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('ws test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('signin after connection', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('signin after connection', done => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let client = Utils.newClientForPrivateChannel({}, 6001, 'app-key', { id: 1 });

Expand All @@ -440,7 +440,7 @@ describe('ws test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('not calling signin after connection throws right error code', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('not calling signin after connection throws right error code', done => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let client = Utils.newClientForPrivateChannel({}, 6001, 'app-key', { id: 1 });

Expand All @@ -456,7 +456,7 @@ describe('ws test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('not having user id throws an error', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('not having user id throws an error', done => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let client = Utils.newClientForPrivateChannel({}, 6001, 'app-key', { name: 'John' });

Expand All @@ -472,7 +472,7 @@ describe('ws test', () => {
});
});

Utils.shouldRun(Utils.appManagerIs('array') && Utils.adapterIs('local'))('sending wrong user data token throws error', done => {
Utils.shouldRun(Utils.appManagerIs('array'))('sending wrong user data token throws error', done => {
Utils.newServer({ 'appManager.array.apps.0.enableUserAuthentication': true, 'userAuthenticationTimeout': 5_000 }, (server: Server) => {
let client = Utils.newClientForPrivateChannel({
userAuthentication: {
Expand Down

0 comments on commit cc692c9

Please sign in to comment.