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

Commit 3f62bf4

Browse files
authored
Merge pull request #229 from ipfs/pubsub-isolation
test: Improve pubsub test isolation
2 parents b811519 + b33079e commit 3f62bf4

File tree

1 file changed

+59
-47
lines changed

1 file changed

+59
-47
lines changed

js/src/pubsub.js

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -560,64 +560,76 @@ module.exports = (common) => {
560560
)
561561
})
562562

563-
it('send/receive 10k messages', (done) => {
564-
this.timeout(2 * 60 * 1000)
563+
describe('send/receive', () => {
564+
let topic
565+
let sub1
566+
let sub2
565567

566-
const msgBase = 'msg - '
567-
const count = 10000
568-
let sendCount = 0
569-
let receivedCount = 0
570-
let startTime
571-
let counter = 0
572-
const topic = getTopic()
568+
before(() => {
569+
topic = getTopic()
570+
})
573571

574-
const sub1 = (msg) => {
575-
// go-ipfs can't send messages in order when there are
576-
// only two nodes in the same machine ¯\_(ツ)_/¯
577-
// https://github.com/ipfs/js-ipfs-api/pull/493#issuecomment-289499943
578-
// const expectedMsg = msgBase + receivedCount
579-
// const receivedMsg = msg.data.toString()
580-
// expect(receivedMsg).to.eql(expectedMsg)
572+
after(() => {
573+
ipfs1.pubsub.unsubscribe(topic, sub1)
574+
ipfs2.pubsub.unsubscribe(topic, sub2)
575+
})
581576

582-
receivedCount++
577+
it('send/receive 10k messages', function (done) {
578+
this.timeout(2 * 60 * 1000)
583579

584-
if (receivedCount >= count) {
585-
const duration = new Date().getTime() - startTime
586-
const opsPerSec = Math.floor(count / (duration / 1000))
580+
const msgBase = 'msg - '
581+
const count = 10000
582+
let sendCount = 0
583+
let receivedCount = 0
584+
let startTime
585+
let counter = 0
587586

588-
console.log(`Send/Receive 10k messages took: ${duration} ms, ${opsPerSec} ops / s\n`)
587+
sub1 = (msg) => {
588+
// go-ipfs can't send messages in order when there are
589+
// only two nodes in the same machine ¯\_(ツ)_/¯
590+
// https://github.com/ipfs/js-ipfs-api/pull/493#issuecomment-289499943
591+
// const expectedMsg = msgBase + receivedCount
592+
// const receivedMsg = msg.data.toString()
593+
// expect(receivedMsg).to.eql(expectedMsg)
589594

590-
check()
591-
}
592-
}
595+
receivedCount++
593596

594-
const sub2 = (msg) => {}
597+
if (receivedCount >= count) {
598+
const duration = new Date().getTime() - startTime
599+
const opsPerSec = Math.floor(count / (duration / 1000))
595600

596-
function check () {
597-
if (++counter === 2) {
598-
ipfs1.pubsub.unsubscribe(topic, sub1)
599-
ipfs2.pubsub.unsubscribe(topic, sub2)
600-
done()
601+
console.log(`Send/Receive 10k messages took: ${duration} ms, ${opsPerSec} ops / s\n`)
602+
603+
check()
604+
}
601605
}
602-
}
603606

604-
series([
605-
(cb) => ipfs1.pubsub.subscribe(topic, sub1, cb),
606-
(cb) => ipfs2.pubsub.subscribe(topic, sub2, cb),
607-
(cb) => waitForPeers(ipfs1, topic, [ipfs2.peerId.id], cb)
608-
], (err) => {
609-
expect(err).to.not.exist()
610-
startTime = new Date().getTime()
607+
sub2 = (msg) => {}
611608

612-
whilst(
613-
() => sendCount < count,
614-
(cb) => {
615-
const msgData = Buffer.from(msgBase + sendCount)
616-
sendCount++
617-
ipfs2.pubsub.publish(topic, msgData, cb)
618-
},
619-
check
620-
)
609+
function check () {
610+
if (++counter === 2) {
611+
done()
612+
}
613+
}
614+
615+
series([
616+
(cb) => ipfs1.pubsub.subscribe(topic, sub1, cb),
617+
(cb) => ipfs2.pubsub.subscribe(topic, sub2, cb),
618+
(cb) => waitForPeers(ipfs1, topic, [ipfs2.peerId.id], cb)
619+
], (err) => {
620+
expect(err).to.not.exist()
621+
startTime = new Date().getTime()
622+
623+
whilst(
624+
() => sendCount < count,
625+
(cb) => {
626+
const msgData = Buffer.from(msgBase + sendCount)
627+
sendCount++
628+
ipfs2.pubsub.publish(topic, msgData, cb)
629+
},
630+
check
631+
)
632+
})
621633
})
622634
})
623635

0 commit comments

Comments
 (0)