@@ -426,35 +426,61 @@ 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 msgBase = 'msg - '
437
451
const count = 10000
438
452
let sendCount = 0
439
453
let receivedCount = 0
440
454
let startTime
455
+ let counter = 0
441
456
442
457
const sub1 = ( msg ) => {
443
- expect ( msg . data . toString ( ) ) . to . equal ( expectedString )
458
+ const expectedMsg = msgBase + receivedCount
459
+ const receivedMsg = msg . data . toString ( )
460
+ expect ( receivedMsg ) . to . eql ( expectedMsg )
444
461
445
462
receivedCount ++
446
463
447
464
if ( receivedCount >= count ) {
448
465
const duration = new Date ( ) . getTime ( ) - startTime
449
- console . log ( `Send/Receive 10k messages took: ${ duration } ms, ${ Math . floor ( count / ( duration / 1000 ) ) } ops / s\n` )
466
+ const opsPerSec = Math . floor ( count / ( duration / 1000 ) )
450
467
451
- ipfs1 . pubsub . unsubscribe ( topic , sub1 )
452
- ipfs2 . pubsub . unsubscribe ( topic , sub2 )
468
+ console . log ( `Send/Receive 10k messages took: ${ duration } ms, ${ opsPerSec } ops / s\n` )
469
+
470
+ check ( )
453
471
}
454
472
}
455
473
456
474
const sub2 = ( msg ) => { }
457
475
476
+ function check ( ) {
477
+ if ( ++ counter === 2 ) {
478
+ ipfs1 . pubsub . unsubscribe ( topic , sub1 )
479
+ ipfs2 . pubsub . unsubscribe ( topic , sub2 )
480
+ done ( )
481
+ }
482
+ }
483
+
458
484
series ( [
459
485
( cb ) => ipfs1 . pubsub . subscribe ( topic , sub1 , cb ) ,
460
486
( cb ) => ipfs2 . pubsub . subscribe ( topic , sub2 , cb ) ,
@@ -466,51 +492,39 @@ module.exports = (common) => {
466
492
whilst (
467
493
( ) => sendCount < count ,
468
494
( cb ) => {
495
+ const msgData = new Buffer ( msgBase + sendCount )
469
496
sendCount ++
470
- ipfs2 . pubsub . publish ( topic , new Buffer ( expectedString ) , cb )
497
+ ipfs2 . pubsub . publish ( topic , msgData , cb )
471
498
} ,
472
- done
499
+ check
473
500
)
474
501
} )
475
502
} )
476
503
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
504
it ( 'call subscribe/unsubscribe 1k times' , ( done ) => {
493
505
const count = 1000
494
506
let sendCount = 0
495
507
const handlers = [ ]
496
508
509
+ const someTopic = 'some-other-topic'
510
+
497
511
whilst (
498
512
( ) => sendCount < count ,
499
513
( cb ) => {
500
514
sendCount ++
501
515
const handler = ( msg ) => { }
502
516
handlers . push ( handler )
503
- ipfs1 . pubsub . subscribe ( topic , handler , cb )
517
+ ipfs1 . pubsub . subscribe ( someTopic , handler , cb )
504
518
} ,
505
519
( err ) => {
506
520
expect ( err ) . to . not . exist
507
521
handlers . forEach ( ( handler ) => {
508
- ipfs1 . pubsub . unsubscribe ( topic , handler )
522
+ ipfs1 . pubsub . unsubscribe ( someTopic , handler )
509
523
} )
510
524
511
525
ipfs1 . pubsub . ls ( ( err , topics ) => {
512
526
expect ( err ) . to . not . exist
513
- expect ( topics ) . to . be . eql ( [ ] )
527
+ expect ( topics ) . to . eql ( [ ] )
514
528
done ( )
515
529
} )
516
530
}
0 commit comments