Skip to content

Commit c462530

Browse files
fix: tets hang up (#1906)
1 parent 11bb9bd commit c462530

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

test/abstract_client.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,15 +3202,30 @@ export default function abstractTest(server, config, ports) {
32023202
})
32033203

32043204
it('should emit connack timeout error', function _test(t, done) {
3205-
const client = connect({
3206-
connectTimeout: 0,
3207-
reconnectPeriod: 5000,
3205+
// Use fake timers to simulate the timeout. The setTimeout inside the client connection
3206+
// will inactive by other tests (maybe) causing this test never ends.
3207+
const clock = sinon.useFakeTimers({
3208+
...fakeTimersOptions,
3209+
toFake: ['setTimeout'],
32083210
})
32093211

3210-
client.on('error', (err) => {
3211-
assert.equal(err.message, 'connack timeout')
3212-
client.end(true, done)
3212+
const connectTimeout = 10
3213+
3214+
t.after(() => {
3215+
clock.restore()
32133216
})
3217+
3218+
const client = connect({
3219+
connectTimeout,
3220+
reconnectPeriod: 5000,
3221+
})
3222+
.on('connect', () => {
3223+
clock.tick(connectTimeout)
3224+
})
3225+
.on('error', (err) => {
3226+
assert.equal(err.message, 'connack timeout')
3227+
client.end(true, done)
3228+
})
32143229
})
32153230

32163231
it(

0 commit comments

Comments
 (0)