Skip to content

Commit 34bcf72

Browse files
committed
update basic.js
1 parent c71ebf3 commit 34bcf72

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

test/basic.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { once } from 'node:events'
33
import { Duplex } from 'node:stream'
44
import {
55
brokerPublish,
6+
checkNoPacket,
67
connect,
78
createAndConnect,
89
delay,
910
nextPacket,
10-
nextPacketWithTimeOut,
1111
setup,
1212
subscribe,
1313
subscribeMultiple,
@@ -373,8 +373,7 @@ test('unsubscribe', async (t) => {
373373
resolve()
374374
})
375375
})
376-
const noPacket = await nextPacketWithTimeOut(s, 10)
377-
t.assert.ok(!noPacket, 'expected no packet')
376+
await checkNoPacket(t, s)
378377
})
379378

380379
test('unsubscribe without subscribe', async (t) => {
@@ -411,11 +410,6 @@ test('unsubscribe on disconnect for a clean=true client', async (t) => {
411410
s.conn.destroy(null)
412411
t.assert.equal(s.conn.destroyed, true, 'closed streams')
413412

414-
const noPacket = async () => {
415-
const packet = await nextPacketWithTimeOut(s, 10)
416-
t.assert.ok(!packet, 'should not receive any more messages')
417-
}
418-
419413
const emittedUnsubscribe = async () => {
420414
await once(s.broker, 'unsubscribe')
421415
t.assert.ok(true, 'should emit unsubscribe')
@@ -430,7 +424,7 @@ test('unsubscribe on disconnect for a clean=true client', async (t) => {
430424
t.assert.ok(true, 'calls the callback')
431425
}
432426
// run parallel
433-
await Promise.all([noPacket(), emittedUnsubscribe(), publishPacket()])
427+
await Promise.all([checkNoPacket(t, s), emittedUnsubscribe(), publishPacket()])
434428
})
435429

436430
test('unsubscribe on disconnect for a clean=false client', async (t) => {
@@ -443,14 +437,10 @@ test('unsubscribe on disconnect for a clean=false client', async (t) => {
443437
s.conn.destroy(null, () => {
444438
t.assert.ok(true, 'closed streams')
445439
})
446-
const noPacket = async () => {
447-
const packet = await nextPacketWithTimeOut(s, 10)
448-
t.assert.ok(!packet, 'should not receive any more messages')
449-
}
450440

451-
const emittedUnsubscribe = async () => {
452-
await once(s.broker, 'unsubscribe')
453-
t.assert.ok(true, 'should emit unsubscribe')
441+
const emittedNoUnsubscribe = async () => {
442+
const result = await withTimeout(once(s.broker, 'unsubscribe'), 10, null)
443+
t.assert.equal(result, null, 'should not emit unsubscribe')
454444
}
455445
const publishPacket = async () => {
456446
await brokerPublish(s, {
@@ -461,7 +451,7 @@ test('unsubscribe on disconnect for a clean=false client', async (t) => {
461451
t.assert.ok(true, 'calls the callback')
462452
}
463453
// run parallel
464-
await Promise.all([noPacket(), emittedUnsubscribe(), publishPacket()])
454+
await Promise.all([checkNoPacket(t, s), emittedNoUnsubscribe(), publishPacket()])
465455
})
466456

467457
test('disconnect', async (t) => {
@@ -770,8 +760,7 @@ test('do not restore QoS 0 subscriptions when clean', async (t) => {
770760
payload: 'world',
771761
qos: 0
772762
})
773-
const packet = await nextPacketWithTimeOut(subscriber2, 10)
774-
t.assert.ok(!packet, 'no packet received')
763+
await checkNoPacket(t, subscriber2)
775764
})
776765

777766
test('double sub does not double deliver', async (t) => {
@@ -799,8 +788,7 @@ test('double sub does not double deliver', async (t) => {
799788

800789
const packet = await nextPacket(s)
801790
t.assert.deepEqual(structuredClone(packet), expected, 'packet matches')
802-
const noPacket = await nextPacketWithTimeOut(s, 10)
803-
t.assert.ok(!noPacket, 'no packet received')
791+
await checkNoPacket(t, s)
804792
})
805793

806794
test('overlapping sub does not double deliver', async (t) => {
@@ -827,8 +815,7 @@ test('overlapping sub does not double deliver', async (t) => {
827815

828816
const packet = await nextPacket(s)
829817
t.assert.deepEqual(structuredClone(packet), expected, 'packet matches')
830-
const noPacket = await nextPacketWithTimeOut(s, 10)
831-
t.assert.ok(!noPacket, 'no packet received')
818+
await checkNoPacket(t, s)
832819
})
833820

834821
test('clear drain', async (t) => {

0 commit comments

Comments
 (0)