@@ -421,22 +421,32 @@ module.exports = (common) => {
421
421
} )
422
422
423
423
describe ( 'multiple nodes' , ( ) => {
424
+ let topic
425
+ let sub1
426
+ let sub2
427
+
428
+ beforeEach ( ( ) => {
429
+ topic = getTopic ( )
430
+ } )
431
+
432
+ afterEach ( ( ) => {
433
+ ipfs1 . pubsub . unsubscribe ( topic , sub1 )
434
+ ipfs2 . pubsub . unsubscribe ( topic , sub2 )
435
+ } )
436
+
424
437
it ( 'receive messages from different node' , ( done ) => {
425
438
const check = makeCheck ( 3 , done )
426
439
const expectedString = 'hello from the other side'
427
- const topic = getTopic ( )
428
440
429
- const sub1 = ( msg ) => {
441
+ sub1 = ( msg ) => {
430
442
expect ( msg . data . toString ( ) ) . to . be . eql ( expectedString )
431
443
expect ( msg . from ) . to . eql ( ipfs2 . peerId . id )
432
- ipfs1 . pubsub . unsubscribe ( topic , sub1 )
433
444
check ( )
434
445
}
435
446
436
- const sub2 = ( msg ) => {
447
+ sub2 = ( msg ) => {
437
448
expect ( msg . data . toString ( ) ) . to . be . eql ( expectedString )
438
449
expect ( msg . from ) . to . eql ( ipfs2 . peerId . id )
439
- ipfs2 . pubsub . unsubscribe ( topic , sub2 )
440
450
check ( )
441
451
}
442
452
@@ -455,29 +465,24 @@ module.exports = (common) => {
455
465
const check = makeCheck ( 3 , done )
456
466
const expectedHex = 'a36161636179656162830103056164a16466666666f4'
457
467
const buffer = Buffer . from ( expectedHex , 'hex' )
458
- const topic = getTopic ( )
459
468
460
- const sub1 = ( msg ) => {
469
+ sub1 = ( msg ) => {
461
470
try {
462
471
expect ( msg . data . toString ( 'hex' ) ) . to . be . eql ( expectedHex )
463
472
expect ( msg . from ) . to . eql ( ipfs2 . peerId . id )
464
473
check ( )
465
474
} catch ( err ) {
466
475
check ( err )
467
- } finally {
468
- ipfs1 . pubsub . unsubscribe ( topic , sub1 )
469
476
}
470
477
}
471
478
472
- const sub2 = ( msg ) => {
479
+ sub2 = ( msg ) => {
473
480
try {
474
481
expect ( msg . data . toString ( 'hex' ) ) . to . eql ( expectedHex )
475
482
expect ( msg . from ) . to . eql ( ipfs2 . peerId . id )
476
483
check ( )
477
484
} catch ( err ) {
478
485
check ( err )
479
- } finally {
480
- ipfs2 . pubsub . unsubscribe ( topic , sub2 )
481
486
}
482
487
}
483
488
@@ -496,25 +501,21 @@ module.exports = (common) => {
496
501
const inbox1 = [ ]
497
502
const inbox2 = [ ]
498
503
const outbox = [ 'hello' , 'world' , 'this' , 'is' , 'pubsub' ]
499
- const topic = getTopic ( )
500
504
501
505
const check = makeCheck ( outbox . length * 3 , ( err ) => {
502
- ipfs1 . pubsub . unsubscribe ( topic , sub1 )
503
- ipfs2 . pubsub . unsubscribe ( topic , sub2 )
504
-
505
506
expect ( inbox1 . sort ( ) ) . to . eql ( outbox . sort ( ) )
506
507
expect ( inbox2 . sort ( ) ) . to . eql ( outbox . sort ( ) )
507
508
508
509
done ( err )
509
510
} )
510
511
511
- function sub1 ( msg ) {
512
+ sub1 = ( msg ) => {
512
513
inbox1 . push ( msg . data . toString ( ) )
513
514
expect ( msg . from ) . to . eql ( ipfs2 . peerId . id )
514
515
check ( )
515
516
}
516
517
517
- function sub2 ( msg ) {
518
+ sub2 = ( msg ) => {
518
519
inbox2 . push ( msg . data . toString ( ) )
519
520
expect ( msg . from ) . to . be . eql ( ipfs2 . peerId . id )
520
521
check ( )
0 commit comments