@@ -59,12 +59,12 @@ describe('MqttClient', function () {
59
59
} )
60
60
61
61
describe ( 'message ids' , function ( ) {
62
- it ( 'should increment the message id' , function ( ) {
62
+ it ( 'should increment the message id' , function ( done ) {
63
63
client = mqtt . connect ( config )
64
64
const currentId = client . _nextId ( )
65
65
66
66
assert . equal ( client . _nextId ( ) , currentId + 1 )
67
- client . end ( )
67
+ client . end ( ( err ) => done ( err ) )
68
68
} )
69
69
70
70
it ( 'should not throw an error if packet\'s messageId is not found when receiving a pubrel packet' , function ( done ) {
@@ -83,9 +83,11 @@ describe('MqttClient', function () {
83
83
84
84
client . on ( 'packetsend' , function ( packet ) {
85
85
if ( packet . cmd === 'pubcomp' ) {
86
- client . end ( )
87
- server2 . close ( )
88
- done ( )
86
+ client . end ( ( err1 ) => {
87
+ server2 . close ( ( err2 ) => {
88
+ done ( err1 || err2 )
89
+ } )
90
+ } )
89
91
}
90
92
} )
91
93
} )
@@ -108,6 +110,9 @@ describe('MqttClient', function () {
108
110
109
111
client . on ( 'message' , function ( t , p , packet ) {
110
112
if ( ++ count === max ) {
113
+ // BUGBUG: the client.end callback never gets called here
114
+ // client.end((err) => done(err))
115
+ client . end ( )
111
116
done ( )
112
117
}
113
118
} )
@@ -143,9 +148,9 @@ describe('MqttClient', function () {
143
148
describe ( 'flushing' , function ( ) {
144
149
it ( 'should attempt to complete pending unsub and send on ping timeout' , function ( done ) {
145
150
this . timeout ( 10000 )
146
- const server3 = new MqttServer ( function ( client ) {
147
- client . on ( 'connect' , function ( packet ) {
148
- client . connack ( { returnCode : 0 } )
151
+ const server2 = new MqttServer ( function ( serverClient ) {
152
+ serverClient . on ( 'connect' , function ( packet ) {
153
+ serverClient . connack ( { returnCode : 0 } )
149
154
} )
150
155
} ) . listen ( ports . PORTAND72 )
151
156
@@ -168,10 +173,11 @@ describe('MqttClient', function () {
168
173
unsubscribeCallbackCalled = true
169
174
} )
170
175
setTimeout ( ( ) => {
171
- client . end ( ( ) => {
176
+ client . end ( ( err1 ) => {
172
177
assert . strictEqual ( pubCallbackCalled && unsubscribeCallbackCalled , true , 'callbacks not invoked' )
173
- server3 . close ( )
174
- done ( )
178
+ server2 . close ( ( err2 ) => {
179
+ done ( err1 || err2 )
180
+ } )
175
181
} )
176
182
} , 5000 )
177
183
} )
@@ -200,7 +206,7 @@ describe('MqttClient', function () {
200
206
innerServer . kill ( 'SIGINT' ) // mocks server shutdown
201
207
client . once ( 'close' , function ( ) {
202
208
assert . exists ( client . reconnectTimer )
203
- client . end ( true , done )
209
+ client . end ( true , ( err ) => done ( err ) )
204
210
} )
205
211
} )
206
212
} )
@@ -261,7 +267,7 @@ describe('MqttClient', function () {
261
267
client . on ( 'reconnect' , function ( ) {
262
268
reconnects ++
263
269
if ( reconnects >= expectedReconnects ) {
264
- client . end ( true , done )
270
+ client . end ( true , ( err ) => done ( err ) )
265
271
}
266
272
} )
267
273
} )
@@ -294,6 +300,7 @@ describe('MqttClient', function () {
294
300
client . end ( true , ( err ) => done ( err ) )
295
301
} else {
296
302
debug ( 'calling client.end()' )
303
+ // Do not call done. We want to trigger a reconnect here.
297
304
client . end ( true )
298
305
}
299
306
} , 2000 )
@@ -313,26 +320,14 @@ describe('MqttClient', function () {
313
320
} )
314
321
315
322
const server2 = new MqttServer ( function ( serverClient ) {
316
- serverClient . on ( 'error' , function ( ) { } )
317
- debug ( 'setting serverClient connect callback' )
318
- serverClient . on ( 'connect' , function ( packet ) {
319
- if ( packet . clientId === 'invalid' ) {
320
- debug ( 'connack with returnCode 2' )
321
- serverClient . connack ( { returnCode : 2 } )
322
- } else {
323
- debug ( 'connack with returnCode 0' )
324
- serverClient . connack ( { returnCode : 0 } )
325
- }
326
- } )
327
- } ) . listen ( ports . PORTAND46 )
328
-
329
- server2 . on ( 'client' , function ( serverClient ) {
330
323
debug ( 'client received on server2.' )
331
324
debug ( 'subscribing to topic `topic`' )
332
325
client . subscribe ( 'topic' , function ( ) {
333
326
debug ( 'once subscribed to topic, end client, destroy serverClient, and close server.' )
334
327
serverClient . destroy ( )
335
- server2 . close ( ( ) => { client . end ( true , done ) } )
328
+ server2 . close ( ( ) => {
329
+ client . end ( true , ( err ) => done ( err ) )
330
+ } )
336
331
} )
337
332
338
333
serverClient . on ( 'subscribe' , function ( packet ) {
@@ -361,7 +356,7 @@ describe('MqttClient', function () {
361
356
} )
362
357
}
363
358
} )
364
- } )
359
+ } ) . listen ( ports . PORTAND46 )
365
360
} )
366
361
367
362
it ( 'should not fill the queue of subscribes if it cannot connect' , function ( done ) {
@@ -388,9 +383,7 @@ describe('MqttClient', function () {
388
383
389
384
setTimeout ( function ( ) {
390
385
assert . equal ( client . queue . length , 1 )
391
- client . end ( true , ( ) => {
392
- done ( )
393
- } )
386
+ client . end ( true , ( err ) => done ( err ) )
394
387
} , 1000 )
395
388
} )
396
389
} )
@@ -424,9 +417,11 @@ describe('MqttClient', function () {
424
417
425
418
server2 . on ( 'client' , function ( serverClient ) {
426
419
client . publish ( 'topic' , 'data' , { qos : 1 } , function ( ) {
427
- serverClient . destroy ( )
428
- server2 . close ( )
429
- client . end ( true , done )
420
+ client . end ( true , ( err1 ) => {
421
+ server2 . close ( ( err2 ) => {
422
+ done ( err1 || err2 )
423
+ } )
424
+ } )
430
425
} )
431
426
432
427
serverClient . on ( 'publish' , function onPublish ( packet ) {
@@ -462,7 +457,7 @@ describe('MqttClient', function () {
462
457
it ( 'check emit error on checkDisconnection w/o callback' , function ( done ) {
463
458
this . timeout ( 15000 )
464
459
465
- const server118 = new MqttServer ( function ( client ) {
460
+ const server2 = new MqttServer ( function ( client ) {
466
461
client . on ( 'connect' , function ( packet ) {
467
462
client . connack ( {
468
463
reasonCode : 0
@@ -486,14 +481,12 @@ describe('MqttClient', function () {
486
481
// wait for the client to receive an error...
487
482
client . on ( 'error' , function ( error ) {
488
483
assert . equal ( error . message , 'client disconnecting' )
489
- server118 . close ( )
490
- done ( )
484
+ server2 . close ( ( err ) => done ( err ) )
491
485
} )
492
486
client . on ( 'connect' , function ( ) {
493
487
client . end ( function ( ) {
494
488
client . _checkDisconnecting ( )
495
489
} )
496
- server118 . close ( )
497
490
} )
498
491
} )
499
492
} )
0 commit comments