Skip to content

Commit d3fe084

Browse files
test: attempt to stabilize socket mode unit tests (#2198)
Co-authored-by: Eden Zimbelman <eden.zimbelman@salesforce.com>
1 parent 0101781 commit d3fe084

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

packages/socket-mode/test/integration.spec.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,22 @@ describe('Integration tests with a WebSocket server', () => {
198198
client.on('close', () => {
199199
closed++;
200200
});
201+
202+
let elapseTime = 0;
203+
let retries = 0;
204+
const startTime = Date.now();
205+
201206
// do not use await here, since `start()` won't return until the connection is established. we are intentionally testing connection establishment failure, so that will never finish. so, let's run this in a rogue "thread", e.g. fire off an async method and let it do its thing!
202207
client.start();
203-
await sleep(50);
204-
// after 50ms, with a timeout of 20ms, we would expect 2 retries.
208+
do {
209+
await sleep(2);
210+
retries = closed;
211+
elapseTime = Date.now() - startTime;
212+
} while (retries < 2 && elapseTime < 50);
213+
// after less then 50 milliseconds, with a timeout of 20ms, we would expect 2 retries.
205214
// crucially, the bug reported in https://github.com/slackapi/node-slack-sdk/issues/2094 shows that on every reconnection attempt, we spawn _another_ websocket instance, which attempts to reconnect forever and is never cleaned up.
206215
// effectively: with each reconnection attempt, we double the number of websockets, eventually causing crashes / out-of-memory issues / rate-limiting from Slack APIs.
207216
// with the bug not fixed, this assertion fails as `close` event was emitted 4 times! if we waited another 20ms, we would see this event count double again (8), and so on.
208-
const retries = closed;
209217
await client.disconnect();
210218
await new Promise((res, rej) => {
211219
// shut down the bad server
@@ -215,6 +223,7 @@ describe('Integration tests with a WebSocket server', () => {
215223
});
216224
});
217225
assert.equal(retries, 2, 'unexpected number of times `close` event was raised during reconnection!');
226+
assert.isAtLeast(elapseTime, 25, 'unexpectedly rapid `close` events raised during reconnection!');
218227
});
219228
});
220229
describe('lifecycle events', () => {

0 commit comments

Comments
 (0)