Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit af6f13c

Browse files
vmxdaviddias
authored andcommitted
test: more pubsub test isolation improvements
Always unsubscribe from feeds at the end of the test, even if there was a failure. This prevents subsequent tests to fail.
1 parent 32b648a commit af6f13c

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

js/src/pubsub.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -421,22 +421,32 @@ module.exports = (common) => {
421421
})
422422

423423
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+
424437
it('receive messages from different node', (done) => {
425438
const check = makeCheck(3, done)
426439
const expectedString = 'hello from the other side'
427-
const topic = getTopic()
428440

429-
const sub1 = (msg) => {
441+
sub1 = (msg) => {
430442
expect(msg.data.toString()).to.be.eql(expectedString)
431443
expect(msg.from).to.eql(ipfs2.peerId.id)
432-
ipfs1.pubsub.unsubscribe(topic, sub1)
433444
check()
434445
}
435446

436-
const sub2 = (msg) => {
447+
sub2 = (msg) => {
437448
expect(msg.data.toString()).to.be.eql(expectedString)
438449
expect(msg.from).to.eql(ipfs2.peerId.id)
439-
ipfs2.pubsub.unsubscribe(topic, sub2)
440450
check()
441451
}
442452

@@ -455,29 +465,24 @@ module.exports = (common) => {
455465
const check = makeCheck(3, done)
456466
const expectedHex = 'a36161636179656162830103056164a16466666666f4'
457467
const buffer = Buffer.from(expectedHex, 'hex')
458-
const topic = getTopic()
459468

460-
const sub1 = (msg) => {
469+
sub1 = (msg) => {
461470
try {
462471
expect(msg.data.toString('hex')).to.be.eql(expectedHex)
463472
expect(msg.from).to.eql(ipfs2.peerId.id)
464473
check()
465474
} catch (err) {
466475
check(err)
467-
} finally {
468-
ipfs1.pubsub.unsubscribe(topic, sub1)
469476
}
470477
}
471478

472-
const sub2 = (msg) => {
479+
sub2 = (msg) => {
473480
try {
474481
expect(msg.data.toString('hex')).to.eql(expectedHex)
475482
expect(msg.from).to.eql(ipfs2.peerId.id)
476483
check()
477484
} catch (err) {
478485
check(err)
479-
} finally {
480-
ipfs2.pubsub.unsubscribe(topic, sub2)
481486
}
482487
}
483488

@@ -496,25 +501,21 @@ module.exports = (common) => {
496501
const inbox1 = []
497502
const inbox2 = []
498503
const outbox = ['hello', 'world', 'this', 'is', 'pubsub']
499-
const topic = getTopic()
500504

501505
const check = makeCheck(outbox.length * 3, (err) => {
502-
ipfs1.pubsub.unsubscribe(topic, sub1)
503-
ipfs2.pubsub.unsubscribe(topic, sub2)
504-
505506
expect(inbox1.sort()).to.eql(outbox.sort())
506507
expect(inbox2.sort()).to.eql(outbox.sort())
507508

508509
done(err)
509510
})
510511

511-
function sub1 (msg) {
512+
sub1 = (msg) => {
512513
inbox1.push(msg.data.toString())
513514
expect(msg.from).to.eql(ipfs2.peerId.id)
514515
check()
515516
}
516517

517-
function sub2 (msg) {
518+
sub2 = (msg) => {
518519
inbox2.push(msg.data.toString())
519520
expect(msg.from).to.be.eql(ipfs2.peerId.id)
520521
check()

0 commit comments

Comments
 (0)