This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsubscribe.js
377 lines (304 loc) · 11.7 KB
/
subscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')
const { nanoid } = require('nanoid')
const pushable = require('it-pushable')
const all = require('it-all')
const { waitForPeers, getTopic } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
const AbortController = require('abort-controller')
const { isWebWorker, isNode } = require('ipfs-utils/src/env')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.pubsub.subscribe', function () {
this.timeout(80 * 1000)
let ipfs1
let ipfs2
let topic
let subscribedTopics = []
before(async () => {
ipfs1 = (await common.spawn()).api
// TODO 'multiple connected nodes' tests fails with go in Firefox
// and JS is flaky everywhere
// webworkers are not dialable because webrtc is not available
ipfs2 = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
})
beforeEach(() => {
topic = getTopic()
subscribedTopics = [topic]
})
afterEach(async () => {
const nodes = [ipfs1, ipfs2]
for (let i = 0; i < subscribedTopics.length; i++) {
const topic = subscribedTopics[i]
await Promise.all(nodes.map(ipfs => ipfs.pubsub.unsubscribe(topic)))
}
subscribedTopics = []
await delay(100)
})
after(() => common.clean())
describe('single node', () => {
it('should subscribe to one topic', async () => {
const msgStream = pushable()
await ipfs1.pubsub.subscribe(topic, msg => {
msgStream.push(msg)
msgStream.end()
})
await ipfs1.pubsub.publish(topic, Buffer.from('hi'))
for await (const msg of msgStream) {
expect(msg.data.toString()).to.equal('hi')
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
expect(msg.topicIDs[0]).to.eq(topic)
expect(msg).to.have.property('from', ipfs1.peerId.id)
break
}
})
it('should subscribe to one topic with options', async () => {
const msgStream = pushable()
await ipfs1.pubsub.subscribe(topic, msg => {
msgStream.push(msg)
msgStream.end()
}, {})
await ipfs1.pubsub.publish(topic, Buffer.from('hi'))
for await (const msg of msgStream) {
expect(msg.data.toString()).to.equal('hi')
expect(msg).to.have.property('seqno')
expect(Buffer.isBuffer(msg.seqno)).to.eql(true)
expect(msg.topicIDs[0]).to.eq(topic)
expect(msg).to.have.property('from', ipfs1.peerId.id)
}
})
it('should subscribe to topic multiple times with different handlers', async () => {
const msgStream1 = pushable()
const msgStream2 = pushable()
const handler1 = msg => {
msgStream1.push(msg)
msgStream1.end()
}
const handler2 = msg => {
msgStream2.push(msg)
msgStream2.end()
}
await Promise.all([
ipfs1.pubsub.subscribe(topic, handler1),
ipfs1.pubsub.subscribe(topic, handler2)
])
await ipfs1.pubsub.publish(topic, Buffer.from('hello'))
const [handler1Msg] = await all(msgStream1)
expect(handler1Msg.data.toString()).to.eql('hello')
const [handler2Msg] = await all(msgStream2)
expect(handler2Msg.data.toString()).to.eql('hello')
await ipfs1.pubsub.unsubscribe(topic, handler1)
await delay(100)
// Still subscribed as there is one listener left
expect(await ipfs1.pubsub.ls()).to.eql([topic])
await ipfs1.pubsub.unsubscribe(topic, handler2)
await delay(100)
// Now all listeners are gone no subscription anymore
expect(await ipfs1.pubsub.ls()).to.eql([])
})
it('should allow discover option to be passed', async () => {
const msgStream = pushable()
await ipfs1.pubsub.subscribe(topic, msg => {
msgStream.push(msg)
msgStream.end()
}, { discover: true })
await ipfs1.pubsub.publish(topic, Buffer.from('hi'))
for await (const msg of msgStream) {
expect(msg.data.toString()).to.eql('hi')
}
})
})
describe('multiple connected nodes', () => {
before(() => {
if (ipfs1.pubsub.setMaxListeners) {
ipfs1.pubsub.setMaxListeners(100)
}
if (ipfs2.pubsub.setMaxListeners) {
ipfs2.pubsub.setMaxListeners(100)
}
const ipfs2Addr = ipfs2.peerId.addresses
.find(ma => ma.nodeAddress().address === '127.0.0.1')
return ipfs1.swarm.connect(ipfs2Addr)
})
it('should receive messages from a different node with floodsub', async function () {
if (!isNode) {
return this.skip()
}
const expectedString = 'should receive messages from a different node with floodsub'
const topic = `floodsub-${nanoid()}`
const ipfs1 = (await common.spawn({
ipfsOptions: {
config: {
Pubsub: {
Router: 'floodsub'
}
}
}
})).api
const ipfs2 = (await common.spawn({
type: isWebWorker ? 'go' : undefined,
ipfsOptions: {
config: {
Pubsub: {
Router: 'floodsub'
}
}
}
})).api
await ipfs1.swarm.connect(ipfs2.peerId.addresses[0])
const msgStream1 = pushable()
const msgStream2 = pushable()
const sub1 = msg => {
msgStream1.push(msg)
msgStream1.end()
}
const sub2 = msg => {
msgStream2.push(msg)
msgStream2.end()
}
const abort1 = new AbortController()
const abort2 = new AbortController()
await Promise.all([
ipfs1.pubsub.subscribe(topic, sub1, { signal: abort1.signal }),
ipfs2.pubsub.subscribe(topic, sub2, { signal: abort2.signal })
])
await waitForPeers(ipfs2, topic, [ipfs1.peerId.id], 30000)
await ipfs2.pubsub.publish(topic, Buffer.from(expectedString))
const [sub1Msg] = await all(msgStream1)
expect(sub1Msg.data.toString()).to.be.eql(expectedString)
expect(sub1Msg.from).to.eql(ipfs2.peerId.id)
const [sub2Msg] = await all(msgStream2)
expect(sub2Msg.data.toString()).to.be.eql(expectedString)
expect(sub2Msg.from).to.eql(ipfs2.peerId.id)
abort1.abort()
abort2.abort()
})
it('should receive messages from a different node', async () => {
const expectedString = 'hello from the other side'
const msgStream1 = pushable()
const msgStream2 = pushable()
const sub1 = msg => {
msgStream1.push(msg)
msgStream1.end()
}
const sub2 = msg => {
msgStream2.push(msg)
msgStream2.end()
}
await Promise.all([
ipfs1.pubsub.subscribe(topic, sub1),
ipfs2.pubsub.subscribe(topic, sub2)
])
await waitForPeers(ipfs2, topic, [ipfs1.peerId.id], 30000)
await delay(5000) // gossipsub need this delay https://github.com/libp2p/go-libp2p-pubsub/issues/331
await ipfs2.pubsub.publish(topic, Buffer.from(expectedString))
const [sub1Msg] = await all(msgStream1)
expect(sub1Msg.data.toString()).to.be.eql(expectedString)
expect(sub1Msg.from).to.eql(ipfs2.peerId.id)
const [sub2Msg] = await all(msgStream2)
expect(sub2Msg.data.toString()).to.be.eql(expectedString)
expect(sub2Msg.from).to.eql(ipfs2.peerId.id)
})
it('should round trip a non-utf8 binary buffer', async () => {
const expectedHex = 'a36161636179656162830103056164a16466666666f4'
const buffer = Buffer.from(expectedHex, 'hex')
const msgStream1 = pushable()
const msgStream2 = pushable()
const sub1 = msg => {
msgStream1.push(msg)
msgStream1.end()
}
const sub2 = msg => {
msgStream2.push(msg)
msgStream2.end()
}
await Promise.all([
ipfs1.pubsub.subscribe(topic, sub1),
ipfs2.pubsub.subscribe(topic, sub2)
])
await waitForPeers(ipfs2, topic, [ipfs1.peerId.id], 30000)
await delay(5000) // gossipsub need this delay https://github.com/libp2p/go-libp2p-pubsub/issues/331
await ipfs2.pubsub.publish(topic, buffer)
const [sub1Msg] = await all(msgStream1)
expect(sub1Msg.data.toString('hex')).to.be.eql(expectedHex)
expect(sub1Msg.from).to.eql(ipfs2.peerId.id)
const [sub2Msg] = await all(msgStream2)
expect(sub2Msg.data.toString('hex')).to.be.eql(expectedHex)
expect(sub2Msg.from).to.eql(ipfs2.peerId.id)
})
it('should receive multiple messages', async () => {
const outbox = ['hello', 'world', 'this', 'is', 'pubsub']
const msgStream1 = pushable()
const msgStream2 = pushable()
const sub1 = msg => {
msgStream1.push(msg)
sub1.called++
if (sub1.called === outbox.length) msgStream1.end()
}
sub1.called = 0
const sub2 = msg => {
msgStream2.push(msg)
sub2.called++
if (sub2.called === outbox.length) msgStream2.end()
}
sub2.called = 0
await Promise.all([
ipfs1.pubsub.subscribe(topic, sub1),
ipfs2.pubsub.subscribe(topic, sub2)
])
await waitForPeers(ipfs2, topic, [ipfs1.peerId.id], 30000)
await delay(5000) // gossipsub need this delay https://github.com/libp2p/go-libp2p-pubsub/issues/331
outbox.forEach(msg => ipfs2.pubsub.publish(topic, Buffer.from(msg)))
const sub1Msgs = await all(msgStream1)
sub1Msgs.forEach(msg => expect(msg.from).to.eql(ipfs2.peerId.id))
const inbox1 = sub1Msgs.map(msg => msg.data.toString())
expect(inbox1.sort()).to.eql(outbox.sort())
const sub2Msgs = await all(msgStream2)
sub2Msgs.forEach(msg => expect(msg.from).to.eql(ipfs2.peerId.id))
const inbox2 = sub2Msgs.map(msg => msg.data.toString())
expect(inbox2.sort()).to.eql(outbox.sort())
})
it('should send/receive 100 messages', async function () {
this.timeout(2 * 60 * 1000)
const msgBase = 'msg - '
const count = 100
const msgStream = pushable()
const sub = msg => {
msgStream.push(msg)
sub.called++
if (sub.called === count) msgStream.end()
}
sub.called = 0
await Promise.all([
ipfs1.pubsub.subscribe(topic, sub),
ipfs2.pubsub.subscribe(topic, () => {})
])
await waitForPeers(ipfs1, topic, [ipfs2.peerId.id], 30000)
await delay(5000) // gossipsub need this delay https://github.com/libp2p/go-libp2p-pubsub/issues/331
const startTime = new Date().getTime()
for (let i = 0; i < count; i++) {
const msgData = Buffer.from(msgBase + i)
await ipfs2.pubsub.publish(topic, msgData)
}
const msgs = await all(msgStream)
const duration = new Date().getTime() - startTime
const opsPerSec = Math.floor(count / (duration / 1000))
// eslint-disable-next-line
console.log(`Send/Receive 100 messages took: ${duration} ms, ${opsPerSec} ops / s`)
msgs.forEach(msg => {
expect(msg.from).to.eql(ipfs2.peerId.id)
expect(msg.data.toString().startsWith(msgBase)).to.be.true()
})
})
})
})
}