@@ -291,35 +291,37 @@ module.exports = function (server, config) {
291
291
'system/registry/event/new_device' , 'system/+/+/new_device'
292
292
] ,
293
293
function ( err ) {
294
- client . end ( )
295
- if ( err ) {
296
- return done ( new Error ( err ) )
297
- }
298
- done ( )
294
+ client . end ( function ( ) {
295
+ if ( err ) {
296
+ return done ( new Error ( err ) )
297
+ }
298
+ done ( )
299
+ } )
299
300
}
300
301
)
301
302
} )
302
303
303
304
it ( 'should return an error (via callbacks) for topic #/event' , function ( done ) {
304
305
var client = connect ( )
305
306
client . subscribe ( [ '#/event' , 'event#' , 'event+' ] , function ( err ) {
306
- client . end ( )
307
- if ( err ) {
308
- return done ( )
309
- }
310
- done ( new Error ( 'Validations do NOT work' ) )
307
+ client . end ( false , function ( ) {
308
+ if ( err ) {
309
+ return done ( )
310
+ }
311
+ done ( new Error ( 'Validations do NOT work' ) )
312
+ } )
311
313
} )
312
314
} )
313
315
314
316
it ( 'should return an empty array for duplicate subs' , function ( done ) {
315
317
var client = connect ( )
316
318
client . subscribe ( 'event' , function ( err , granted1 ) {
317
319
if ( err ) {
318
- return done ( )
320
+ return done ( err )
319
321
}
320
322
client . subscribe ( 'event' , function ( err , granted2 ) {
321
323
if ( err ) {
322
- return done ( )
324
+ return done ( err )
323
325
}
324
326
granted2 . should . Array ( )
325
327
granted2 . should . be . empty ( )
@@ -331,33 +333,36 @@ module.exports = function (server, config) {
331
333
it ( 'should return an error (via callbacks) for topic #/event' , function ( done ) {
332
334
var client = connect ( )
333
335
client . subscribe ( '#/event' , function ( err ) {
334
- client . end ( )
335
- if ( err ) {
336
- return done ( )
337
- }
338
- done ( new Error ( 'Validations do NOT work' ) )
336
+ client . end ( function ( ) {
337
+ if ( err ) {
338
+ return done ( )
339
+ }
340
+ done ( new Error ( 'Validations do NOT work' ) )
341
+ } )
339
342
} )
340
343
} )
341
344
342
345
it ( 'should return an error (via callbacks) for topic event#' , function ( done ) {
343
346
var client = connect ( )
344
347
client . subscribe ( 'event#' , function ( err ) {
345
- client . end ( )
346
- if ( err ) {
347
- return done ( )
348
- }
349
- done ( new Error ( 'Validations do NOT work' ) )
348
+ client . end ( function ( ) {
349
+ if ( err ) {
350
+ return done ( )
351
+ }
352
+ done ( new Error ( 'Validations do NOT work' ) )
353
+ } )
350
354
} )
351
355
} )
352
356
353
357
it ( 'should return an error (via callbacks) for topic system/#/event' , function ( done ) {
354
358
var client = connect ( )
355
359
client . subscribe ( 'system/#/event' , function ( err ) {
356
- client . end ( )
357
- if ( err ) {
358
- return done ( )
359
- }
360
- done ( new Error ( 'Validations do NOT work' ) )
360
+ client . end ( function ( ) {
361
+ if ( err ) {
362
+ return done ( )
363
+ }
364
+ done ( new Error ( 'Validations do NOT work' ) )
365
+ } )
361
366
} )
362
367
} )
363
368
@@ -375,11 +380,12 @@ module.exports = function (server, config) {
375
380
it ( 'should return an error (via callbacks) for topic system/+/#/event' , function ( done ) {
376
381
var client = connect ( )
377
382
client . subscribe ( 'system/+/#/event' , function ( err ) {
378
- client . end ( )
379
- if ( err ) {
380
- return done ( )
381
- }
382
- done ( new Error ( 'Validations do NOT work' ) )
383
+ client . end ( true , function ( ) {
384
+ if ( err ) {
385
+ return done ( )
386
+ }
387
+ done ( new Error ( 'Validations do NOT work' ) )
388
+ } )
383
389
} )
384
390
} )
385
391
} )
@@ -395,7 +401,9 @@ module.exports = function (server, config) {
395
401
396
402
client . once ( 'connect' , function ( ) {
397
403
client . queue . length . should . equal ( 0 )
398
- client . end ( true , done )
404
+ setTimeout ( function ( ) {
405
+ client . end ( true , done )
406
+ } , 10 )
399
407
} )
400
408
} )
401
409
@@ -404,72 +412,94 @@ module.exports = function (server, config) {
404
412
405
413
client . publish ( 'test' , 'test' , { qos : 0 } )
406
414
client . queue . length . should . equal ( 0 )
407
- client . end ( true , done )
415
+ client . on ( 'connect' , function ( ) {
416
+ setTimeout ( function ( ) {
417
+ client . end ( true , done )
418
+ } , 10 )
419
+ } )
408
420
} )
409
421
410
- it ( 'should not queue qos != 0 messages' , function ( done ) {
422
+ it ( 'should queue qos != 0 messages' , function ( done ) {
411
423
var client = connect ( { queueQoSZero : false } )
412
424
413
425
client . publish ( 'test' , 'test' , { qos : 1 } )
414
426
client . publish ( 'test' , 'test' , { qos : 2 } )
415
427
client . subscribe ( 'test' )
416
428
client . unsubscribe ( 'test' )
417
429
client . queue . length . should . equal ( 2 )
418
- client . end ( true , done )
430
+ client . on ( 'connect' , function ( ) {
431
+ setTimeout ( function ( ) {
432
+ client . end ( true , done )
433
+ } , 10 )
434
+ } )
419
435
} )
420
436
421
437
it ( 'should call cb if an outgoing QoS 0 message is not sent' , function ( done ) {
422
438
var client = connect ( { queueQoSZero : false } )
439
+ var called = false
423
440
424
441
client . publish ( 'test' , 'test' , { qos : 0 } , function ( ) {
425
- client . end ( true , done )
442
+ called = true
443
+ } )
444
+
445
+ client . on ( 'connect' , function ( ) {
446
+ called . should . equal ( true )
447
+ setTimeout ( function ( ) {
448
+ client . end ( true , done )
449
+ } , 10 )
426
450
} )
427
451
} )
428
452
429
- if ( ! process . env . TRAVIS ) {
430
- it ( 'should delay ending up until all inflight messages are delivered' , function ( done ) {
431
- var client = connect ( )
453
+ it ( 'should delay ending up until all inflight messages are delivered' , function ( done ) {
454
+ var client = connect ( )
455
+ var subscribeCalled = false
432
456
433
- client . on ( 'connect' , function ( ) {
434
- client . subscribe ( 'test' , function ( ) {
457
+ client . on ( 'connect' , function ( ) {
458
+ client . subscribe ( 'test' , function ( ) {
459
+ subscribeCalled = true
460
+ } )
461
+ client . publish ( 'test' , 'test' , function ( ) {
462
+ client . end ( false , function ( ) {
463
+ subscribeCalled . should . be . equal ( true )
435
464
done ( )
436
465
} )
437
- client . publish ( 'test' , 'test' , function ( ) {
438
- client . end ( )
439
- } )
440
466
} )
441
467
} )
468
+ } )
442
469
443
- it ( 'wait QoS 1 publish messages' , function ( done ) {
444
- var client = connect ( )
470
+ it ( 'wait QoS 1 publish messages' , function ( done ) {
471
+ var client = connect ( )
472
+ var messageReceived = false
445
473
446
- client . on ( 'connect' , function ( ) {
447
- client . subscribe ( 'test' )
448
- client . publish ( 'test' , 'test' , { qos : 1 } , function ( ) {
449
- client . end ( )
450
- } )
451
- client . on ( 'message' , function ( ) {
474
+ client . on ( 'connect' , function ( ) {
475
+ client . subscribe ( 'test' )
476
+ client . publish ( 'test' , 'test' , { qos : 1 } , function ( ) {
477
+ client . end ( false , function ( ) {
478
+ messageReceived . should . equal ( true )
452
479
done ( )
453
480
} )
454
481
} )
482
+ client . on ( 'message' , function ( ) {
483
+ messageReceived = true
484
+ } )
485
+ } )
455
486
456
- server . once ( 'client' , function ( serverClient ) {
457
- serverClient . on ( 'subscribe' , function ( ) {
458
- serverClient . on ( 'publish' , function ( packet ) {
459
- serverClient . publish ( packet )
460
- } )
487
+ server . once ( 'client' , function ( serverClient ) {
488
+ serverClient . on ( 'subscribe' , function ( ) {
489
+ serverClient . on ( 'publish' , function ( packet ) {
490
+ serverClient . publish ( packet )
461
491
} )
462
492
} )
463
493
} )
494
+ } )
464
495
465
- it ( 'does not wait acks when force-closing' , function ( done ) {
466
- // non-running broker
467
- var client = connect ( 'mqtt://localhost:8993' )
496
+ it ( 'does not wait acks when force-closing' , function ( done ) {
497
+ // non-running broker
498
+ var client = connect ( 'mqtt://localhost:8993' )
468
499
469
- client . publish ( 'test' , 'test' , { qos : 1 } )
470
- client . end ( true , done )
471
- } )
472
- }
500
+ client . publish ( 'test' , 'test' , { qos : 1 } )
501
+ client . end ( true , done )
502
+ } )
473
503
} )
474
504
475
505
describe ( 'publishing' , function ( ) {
@@ -480,16 +510,21 @@ module.exports = function (server, config) {
480
510
481
511
client . publish ( topic , payload )
482
512
483
- server . once ( 'client' , function ( serverClient ) {
513
+ server . on ( 'client' , onClient )
514
+
515
+ function onClient ( serverClient ) {
516
+ serverClient . once ( 'connect' , function ( ) {
517
+ server . removeListener ( 'client' , onClient )
518
+ } )
519
+
484
520
serverClient . once ( 'publish' , function ( packet ) {
485
521
packet . topic . should . equal ( topic )
486
522
packet . payload . toString ( ) . should . equal ( payload )
487
523
packet . qos . should . equal ( 0 )
488
524
packet . retain . should . equal ( false )
489
- client . end ( )
490
- done ( )
525
+ client . end ( true , done )
491
526
} )
492
- } )
527
+ }
493
528
} )
494
529
495
530
it ( 'should publish a message (online)' , function ( done ) {
0 commit comments