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

Commit e449029

Browse files
committed
test: make sure load tests clean after themselves
1 parent 06c32f2 commit e449029

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

src/pubsub.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -426,30 +426,52 @@ 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 msgData = new Buffer('hello')
437451
const count = 10000
438452
let sendCount = 0
439453
let receivedCount = 0
440454
let startTime
455+
let counter = 0
456+
457+
function check () {
458+
if (++counter === 2) {
459+
ipfs1.pubsub.unsubscribe(topic, sub1)
460+
ipfs2.pubsub.unsubscribe(topic, sub2)
461+
done()
462+
}
463+
}
441464

442465
const sub1 = (msg) => {
443-
expect(msg.data.toString()).to.equal(expectedString)
466+
expect(msg.data).to.eql(msgData)
444467

445468
receivedCount++
446469

447470
if (receivedCount >= count) {
448471
const duration = new Date().getTime() - startTime
449472
console.log(`Send/Receive 10k messages took: ${duration} ms, ${Math.floor(count / (duration / 1000))} ops / s\n`)
450473

451-
ipfs1.pubsub.unsubscribe(topic, sub1)
452-
ipfs2.pubsub.unsubscribe(topic, sub2)
474+
check()
453475
}
454476
}
455477

@@ -467,50 +489,37 @@ module.exports = (common) => {
467489
() => sendCount < count,
468490
(cb) => {
469491
sendCount++
470-
ipfs2.pubsub.publish(topic, new Buffer(expectedString), cb)
492+
ipfs2.pubsub.publish(topic, msgData, cb)
471493
},
472-
done
494+
check
473495
)
474496
})
475497
})
476498

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-
492499
it('call subscribe/unsubscribe 1k times', (done) => {
493500
const count = 1000
494501
let sendCount = 0
495502
const handlers = []
496503

504+
const someTopic = 'some-other-topic'
505+
497506
whilst(
498507
() => sendCount < count,
499508
(cb) => {
500509
sendCount++
501510
const handler = (msg) => {}
502511
handlers.push(handler)
503-
ipfs1.pubsub.subscribe(topic, handler, cb)
512+
ipfs1.pubsub.subscribe(someTopic, handler, cb)
504513
},
505514
(err) => {
506515
expect(err).to.not.exist
507516
handlers.forEach((handler) => {
508-
ipfs1.pubsub.unsubscribe(topic, handler)
517+
ipfs1.pubsub.unsubscribe(someTopic, handler)
509518
})
510519

511520
ipfs1.pubsub.ls((err, topics) => {
512521
expect(err).to.not.exist
513-
expect(topics).to.be.eql([])
522+
expect(topics).to.eql([])
514523
done()
515524
})
516525
}

0 commit comments

Comments
 (0)