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

Commit 2124f27

Browse files
authored
Merge pull request #109 from ipfs/fix/pubsub-tests
tests: make sure load tests clean after themselves
2 parents 06c32f2 + ad952e2 commit 2124f27

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

src/pubsub.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -426,35 +426,61 @@ module.exports = (common) => {
426426
})
427427

428428
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+
)
431445
})
432446

433447
it('send/receive 10k messages', function (done) {
434448
this.timeout(2 * 60 * 1000)
435449

436-
const expectedString = 'hello'
450+
const msgBase = 'msg - '
437451
const count = 10000
438452
let sendCount = 0
439453
let receivedCount = 0
440454
let startTime
455+
let counter = 0
441456

442457
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)
444461

445462
receivedCount++
446463

447464
if (receivedCount >= count) {
448465
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))
450467

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()
453471
}
454472
}
455473

456474
const sub2 = (msg) => {}
457475

476+
function check () {
477+
if (++counter === 2) {
478+
ipfs1.pubsub.unsubscribe(topic, sub1)
479+
ipfs2.pubsub.unsubscribe(topic, sub2)
480+
done()
481+
}
482+
}
483+
458484
series([
459485
(cb) => ipfs1.pubsub.subscribe(topic, sub1, cb),
460486
(cb) => ipfs2.pubsub.subscribe(topic, sub2, cb),
@@ -466,51 +492,39 @@ module.exports = (common) => {
466492
whilst(
467493
() => sendCount < count,
468494
(cb) => {
495+
const msgData = new Buffer(msgBase + sendCount)
469496
sendCount++
470-
ipfs2.pubsub.publish(topic, new Buffer(expectedString), cb)
497+
ipfs2.pubsub.publish(topic, msgData, cb)
471498
},
472-
done
499+
check
473500
)
474501
})
475502
})
476503

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-
492504
it('call subscribe/unsubscribe 1k times', (done) => {
493505
const count = 1000
494506
let sendCount = 0
495507
const handlers = []
496508

509+
const someTopic = 'some-other-topic'
510+
497511
whilst(
498512
() => sendCount < count,
499513
(cb) => {
500514
sendCount++
501515
const handler = (msg) => {}
502516
handlers.push(handler)
503-
ipfs1.pubsub.subscribe(topic, handler, cb)
517+
ipfs1.pubsub.subscribe(someTopic, handler, cb)
504518
},
505519
(err) => {
506520
expect(err).to.not.exist
507521
handlers.forEach((handler) => {
508-
ipfs1.pubsub.unsubscribe(topic, handler)
522+
ipfs1.pubsub.unsubscribe(someTopic, handler)
509523
})
510524

511525
ipfs1.pubsub.ls((err, topics) => {
512526
expect(err).to.not.exist
513-
expect(topics).to.be.eql([])
527+
expect(topics).to.eql([])
514528
done()
515529
})
516530
}

0 commit comments

Comments
 (0)