@@ -426,30 +426,52 @@ module.exports = (common) => {
426
426
} )
427
427
428
428
after ( ( ) => {
429
- ipfs1 . pubsub . setMaxListeners ( 11 )
430
- ipfs2 . pubsub . setMaxListeners ( 11 )
429
+ ipfs1 . pubsub . setMaxListeners ( 10 )
430
+ ipfs2 . pubsub . setMaxListeners ( 10 )
431
+ } )
432
+
433
+ it ( 'call publish 1k times' , ( done ) => {
434
+ const count = 1000
435
+ let sendCount = 0
436
+
437
+ whilst (
438
+ ( ) => sendCount < count ,
439
+ ( cb ) => {
440
+ sendCount ++
441
+ ipfs1 . pubsub . publish ( topic , new Buffer ( 'hey there' ) , cb )
442
+ } ,
443
+ done
444
+ )
431
445
} )
432
446
433
447
it ( 'send/receive 10k messages' , function ( done ) {
434
448
this . timeout ( 2 * 60 * 1000 )
435
449
436
- const expectedString = 'hello'
450
+ const msgData = new Buffer ( 'hello' )
437
451
const count = 10000
438
452
let sendCount = 0
439
453
let receivedCount = 0
440
454
let startTime
455
+ let counter = 0
456
+
457
+ function check ( ) {
458
+ if ( ++ counter === 2 ) {
459
+ ipfs1 . pubsub . unsubscribe ( topic , sub1 )
460
+ ipfs2 . pubsub . unsubscribe ( topic , sub2 )
461
+ done ( )
462
+ }
463
+ }
441
464
442
465
const sub1 = ( msg ) => {
443
- expect ( msg . data . toString ( ) ) . to . equal ( expectedString )
466
+ expect ( msg . data ) . to . eql ( msgData )
444
467
445
468
receivedCount ++
446
469
447
470
if ( receivedCount >= count ) {
448
471
const duration = new Date ( ) . getTime ( ) - startTime
449
472
console . log ( `Send/Receive 10k messages took: ${ duration } ms, ${ Math . floor ( count / ( duration / 1000 ) ) } ops / s\n` )
450
473
451
- ipfs1 . pubsub . unsubscribe ( topic , sub1 )
452
- ipfs2 . pubsub . unsubscribe ( topic , sub2 )
474
+ check ( )
453
475
}
454
476
}
455
477
@@ -467,50 +489,37 @@ module.exports = (common) => {
467
489
( ) => sendCount < count ,
468
490
( cb ) => {
469
491
sendCount ++
470
- ipfs2 . pubsub . publish ( topic , new Buffer ( expectedString ) , cb )
492
+ ipfs2 . pubsub . publish ( topic , msgData , cb )
471
493
} ,
472
- done
494
+ check
473
495
)
474
496
} )
475
497
} )
476
498
477
- it ( 'call publish 1k times' , ( done ) => {
478
- const expectedString = 'hello'
479
- const count = 1000
480
- let sendCount = 0
481
-
482
- whilst (
483
- ( ) => sendCount < count ,
484
- ( cb ) => {
485
- sendCount ++
486
- ipfs1 . pubsub . publish ( topic , new Buffer ( expectedString ) , cb )
487
- } ,
488
- done
489
- )
490
- } )
491
-
492
499
it ( 'call subscribe/unsubscribe 1k times' , ( done ) => {
493
500
const count = 1000
494
501
let sendCount = 0
495
502
const handlers = [ ]
496
503
504
+ const someTopic = 'some-other-topic'
505
+
497
506
whilst (
498
507
( ) => sendCount < count ,
499
508
( cb ) => {
500
509
sendCount ++
501
510
const handler = ( msg ) => { }
502
511
handlers . push ( handler )
503
- ipfs1 . pubsub . subscribe ( topic , handler , cb )
512
+ ipfs1 . pubsub . subscribe ( someTopic , handler , cb )
504
513
} ,
505
514
( err ) => {
506
515
expect ( err ) . to . not . exist
507
516
handlers . forEach ( ( handler ) => {
508
- ipfs1 . pubsub . unsubscribe ( topic , handler )
517
+ ipfs1 . pubsub . unsubscribe ( someTopic , handler )
509
518
} )
510
519
511
520
ipfs1 . pubsub . ls ( ( err , topics ) => {
512
521
expect ( err ) . to . not . exist
513
- expect ( topics ) . to . be . eql ( [ ] )
522
+ expect ( topics ) . to . eql ( [ ] )
514
523
done ( )
515
524
} )
516
525
}
0 commit comments