@@ -59,12 +59,12 @@ describe('MqttClient', function () {
5959 } )
6060
6161 describe ( 'message ids' , function ( ) {
62- it ( 'should increment the message id' , function ( ) {
62+ it ( 'should increment the message id' , function ( done ) {
6363 client = mqtt . connect ( config )
6464 const currentId = client . _nextId ( )
6565
6666 assert . equal ( client . _nextId ( ) , currentId + 1 )
67- client . end ( )
67+ client . end ( ( err ) => done ( err ) )
6868 } )
6969
7070 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 () {
8383
8484 client . on ( 'packetsend' , function ( packet ) {
8585 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+ } )
8991 }
9092 } )
9193 } )
@@ -108,6 +110,9 @@ describe('MqttClient', function () {
108110
109111 client . on ( 'message' , function ( t , p , packet ) {
110112 if ( ++ count === max ) {
113+ // BUGBUG: the client.end callback never gets called here
114+ // client.end((err) => done(err))
115+ client . end ( )
111116 done ( )
112117 }
113118 } )
@@ -143,9 +148,9 @@ describe('MqttClient', function () {
143148 describe ( 'flushing' , function ( ) {
144149 it ( 'should attempt to complete pending unsub and send on ping timeout' , function ( done ) {
145150 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 } )
149154 } )
150155 } ) . listen ( ports . PORTAND72 )
151156
@@ -168,10 +173,11 @@ describe('MqttClient', function () {
168173 unsubscribeCallbackCalled = true
169174 } )
170175 setTimeout ( ( ) => {
171- client . end ( ( ) => {
176+ client . end ( ( err1 ) => {
172177 assert . strictEqual ( pubCallbackCalled && unsubscribeCallbackCalled , true , 'callbacks not invoked' )
173- server3 . close ( )
174- done ( )
178+ server2 . close ( ( err2 ) => {
179+ done ( err1 || err2 )
180+ } )
175181 } )
176182 } , 5000 )
177183 } )
@@ -200,7 +206,7 @@ describe('MqttClient', function () {
200206 innerServer . kill ( 'SIGINT' ) // mocks server shutdown
201207 client . once ( 'close' , function ( ) {
202208 assert . exists ( client . reconnectTimer )
203- client . end ( true , done )
209+ client . end ( true , ( err ) => done ( err ) )
204210 } )
205211 } )
206212 } )
@@ -261,7 +267,7 @@ describe('MqttClient', function () {
261267 client . on ( 'reconnect' , function ( ) {
262268 reconnects ++
263269 if ( reconnects >= expectedReconnects ) {
264- client . end ( true , done )
270+ client . end ( true , ( err ) => done ( err ) )
265271 }
266272 } )
267273 } )
@@ -294,6 +300,7 @@ describe('MqttClient', function () {
294300 client . end ( true , ( err ) => done ( err ) )
295301 } else {
296302 debug ( 'calling client.end()' )
303+ // Do not call done. We want to trigger a reconnect here.
297304 client . end ( true )
298305 }
299306 } , 2000 )
@@ -313,26 +320,14 @@ describe('MqttClient', function () {
313320 } )
314321
315322 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 ) {
330323 debug ( 'client received on server2.' )
331324 debug ( 'subscribing to topic `topic`' )
332325 client . subscribe ( 'topic' , function ( ) {
333326 debug ( 'once subscribed to topic, end client, destroy serverClient, and close server.' )
334327 serverClient . destroy ( )
335- server2 . close ( ( ) => { client . end ( true , done ) } )
328+ server2 . close ( ( ) => {
329+ client . end ( true , ( err ) => done ( err ) )
330+ } )
336331 } )
337332
338333 serverClient . on ( 'subscribe' , function ( packet ) {
@@ -361,7 +356,7 @@ describe('MqttClient', function () {
361356 } )
362357 }
363358 } )
364- } )
359+ } ) . listen ( ports . PORTAND46 )
365360 } )
366361
367362 it ( 'should not fill the queue of subscribes if it cannot connect' , function ( done ) {
@@ -388,9 +383,7 @@ describe('MqttClient', function () {
388383
389384 setTimeout ( function ( ) {
390385 assert . equal ( client . queue . length , 1 )
391- client . end ( true , ( ) => {
392- done ( )
393- } )
386+ client . end ( true , ( err ) => done ( err ) )
394387 } , 1000 )
395388 } )
396389 } )
@@ -424,9 +417,11 @@ describe('MqttClient', function () {
424417
425418 server2 . on ( 'client' , function ( serverClient ) {
426419 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+ } )
430425 } )
431426
432427 serverClient . on ( 'publish' , function onPublish ( packet ) {
@@ -462,7 +457,7 @@ describe('MqttClient', function () {
462457 it ( 'check emit error on checkDisconnection w/o callback' , function ( done ) {
463458 this . timeout ( 15000 )
464459
465- const server118 = new MqttServer ( function ( client ) {
460+ const server2 = new MqttServer ( function ( client ) {
466461 client . on ( 'connect' , function ( packet ) {
467462 client . connack ( {
468463 reasonCode : 0
@@ -486,14 +481,12 @@ describe('MqttClient', function () {
486481 // wait for the client to receive an error...
487482 client . on ( 'error' , function ( error ) {
488483 assert . equal ( error . message , 'client disconnecting' )
489- server118 . close ( )
490- done ( )
484+ server2 . close ( ( err ) => done ( err ) )
491485 } )
492486 client . on ( 'connect' , function ( ) {
493487 client . end ( function ( ) {
494488 client . _checkDisconnecting ( )
495489 } )
496- server118 . close ( )
497490 } )
498491 } )
499492} )
0 commit comments