Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7fc1cfb

Browse files
dignifiedquiredaviddias
authored andcommitted
fix(pubsub): dynamic topics to avoid race conditions (#151)
1 parent c45a3af commit 7fc1cfb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/pubsub.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = (common) => {
6161
describe('.pubsub', function () {
6262
this.timeout(20 * 1000)
6363

64-
const topic = 'pubsub-tests'
64+
const getTopic = () => 'pubsub-tests-' + Math.random()
6565

6666
describe('callback API', () => {
6767
let ipfs1
@@ -98,20 +98,23 @@ module.exports = (common) => {
9898
describe('single node', () => {
9999
describe('.publish', () => {
100100
it('errors on string messags', (done) => {
101+
const topic = getTopic()
101102
ipfs1.pubsub.publish(topic, 'hello friend', (err) => {
102103
expect(err).to.exist()
103104
done()
104105
})
105106
})
106107

107108
it('message from buffer', (done) => {
109+
const topic = getTopic()
108110
ipfs1.pubsub.publish(topic, Buffer.from('hello friend'), done)
109111
})
110112
})
111113

112114
describe('.subscribe', () => {
113115
it('to one topic', (done) => {
114116
const check = makeCheck(2, done)
117+
const topic = getTopic()
115118

116119
const handler = (msg) => {
117120
expect(msg.data.toString()).to.equal('hi')
@@ -136,6 +139,8 @@ module.exports = (common) => {
136139
})
137140

138141
it('attaches multiple event listeners', (done) => {
142+
const topic = getTopic()
143+
139144
const check = makeCheck(3, done)
140145
const handler1 = (msg) => {
141146
expect(msg.data.toString()).to.eql('hello')
@@ -176,6 +181,7 @@ module.exports = (common) => {
176181

177182
it('discover options', (done) => {
178183
const check = makeCheck(2, done)
184+
const topic = getTopic()
179185

180186
const handler = (msg) => {
181187
expect(msg.data.toString()).to.be.eql('hi')
@@ -210,6 +216,7 @@ module.exports = (common) => {
210216

211217
describe('.peers', () => {
212218
it('does not error when not subscribed to a topic', (done) => {
219+
const topic = getTopic()
213220
ipfs1.pubsub.peers(topic, (err, peers) => {
214221
expect(err).to.not.exist()
215222
// Should be empty() but as mentioned below go-ipfs returns more than it should
@@ -226,6 +233,7 @@ module.exports = (common) => {
226233
const sub2 = (msg) => {}
227234
const sub3 = (msg) => {}
228235

236+
const topic = getTopic()
229237
const topicOther = topic + 'different topic'
230238

231239
series([
@@ -254,6 +262,7 @@ module.exports = (common) => {
254262
const sub1 = (msg) => {}
255263
const sub2 = (msg) => {}
256264
const sub3 = (msg) => {}
265+
const topic = getTopic()
257266

258267
series([
259268
(cb) => ipfs1.pubsub.subscribe(topic, sub1, cb),
@@ -274,6 +283,7 @@ module.exports = (common) => {
274283
const sub1 = (msg) => {}
275284
const sub2 = (msg) => {}
276285
const sub3 = (msg) => {}
286+
const topic = getTopic()
277287

278288
series([
279289
(cb) => ipfs1.pubsub.subscribe(topic, sub1, cb),
@@ -305,6 +315,7 @@ module.exports = (common) => {
305315

306316
it('list with 1 subscribed topic', (done) => {
307317
const sub1 = (msg) => {}
318+
const topic = getTopic()
308319

309320
ipfs1.pubsub.subscribe(topic, sub1, (err) => {
310321
expect(err).to.not.exist()
@@ -358,6 +369,7 @@ module.exports = (common) => {
358369
it('receive messages from different node', (done) => {
359370
const check = makeCheck(3, done)
360371
const expectedString = 'hello from the other side'
372+
const topic = getTopic()
361373

362374
const sub1 = (msg) => {
363375
expect(msg.data.toString()).to.be.eql(expectedString)
@@ -388,6 +400,7 @@ module.exports = (common) => {
388400
const check = makeCheck(3, done)
389401
const expectedHex = 'a36161636179656162830103056164a16466666666f4'
390402
const buffer = Buffer.from(expectedHex, 'hex')
403+
const topic = getTopic()
391404

392405
const sub1 = (msg) => {
393406
try {
@@ -428,6 +441,7 @@ module.exports = (common) => {
428441
const inbox1 = []
429442
const inbox2 = []
430443
const outbox = ['hello', 'world', 'this', 'is', 'pubsub']
444+
const topic = getTopic()
431445

432446
const check = makeCheck(outbox.length * 3, (err) => {
433447
ipfs1.pubsub.unsubscribe(topic, sub1)
@@ -479,6 +493,7 @@ module.exports = (common) => {
479493
it('call publish 1k times', (done) => {
480494
const count = 1000
481495
let sendCount = 0
496+
const topic = getTopic()
482497

483498
whilst(
484499
() => sendCount < count,
@@ -499,6 +514,7 @@ module.exports = (common) => {
499514
let receivedCount = 0
500515
let startTime
501516
let counter = 0
517+
const topic = getTopic()
502518

503519
const sub1 = (msg) => {
504520
// go-ipfs can't send messages in order when there are
@@ -555,7 +571,7 @@ module.exports = (common) => {
555571
let sendCount = 0
556572
const handlers = []
557573

558-
const someTopic = 'some-other-topic'
574+
const someTopic = getTopic()
559575

560576
whilst(
561577
() => sendCount < count,
@@ -608,6 +624,8 @@ module.exports = (common) => {
608624
})
609625

610626
it('.subscribe and .publish', () => {
627+
const topic = getTopic()
628+
611629
const sub = (msg) => {
612630
expect(msg.data.toString()).to.be.eql('hi')
613631
ipfs1.pubsub.unsubscribe(topic, sub)
@@ -619,6 +637,7 @@ module.exports = (common) => {
619637

620638
it('.peers', () => {
621639
const sub = (msg) => {}
640+
const topic = getTopic()
622641

623642
return ipfs1.pubsub.subscribe(topic, sub)
624643
.then(() => ipfs1.pubsub.peers(topic))

0 commit comments

Comments
 (0)