@libp2p/interface-compliance-tests 1.1.22 → 1.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +10 -8
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +41 -27
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +12 -7
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +5 -4
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +95 -39
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +34 -49
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.js +1 -1
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/pubsub/api.ts +10 -8
- package/src/pubsub/connection-handlers.ts +43 -28
- package/src/pubsub/emit-self.ts +12 -7
- package/src/pubsub/messages.ts +5 -4
- package/src/pubsub/multiple-nodes.ts +96 -41
- package/src/pubsub/two-nodes.ts +37 -55
- package/src/pubsub/utils.ts +1 -1
|
@@ -6,7 +6,6 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
7
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
8
|
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
9
|
import type { TestSetup } from '../index.js'
|
|
11
10
|
import type { Message } from '@libp2p/interfaces/pubsub'
|
|
12
11
|
import type { PubSubArgs } from './index.js'
|
|
@@ -74,10 +73,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
74
73
|
|
|
75
74
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
76
75
|
const subscriptionsChanged = Promise.all([
|
|
77
|
-
new Promise((resolve) => psA.addEventListener('
|
|
76
|
+
new Promise((resolve) => psA.addEventListener('subscription-change', resolve, {
|
|
78
77
|
once: true
|
|
79
78
|
})),
|
|
80
|
-
new Promise((resolve) => psB.addEventListener('
|
|
79
|
+
new Promise((resolve) => psB.addEventListener('subscription-change', resolve, {
|
|
81
80
|
once: true
|
|
82
81
|
}))
|
|
83
82
|
])
|
|
@@ -174,10 +173,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
174
173
|
let subscribedTopics = psA.getTopics()
|
|
175
174
|
expect(subscribedTopics).to.not.include(topic)
|
|
176
175
|
|
|
177
|
-
psA.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
176
|
+
psA.subscribe(topic)
|
|
177
|
+
psA.addEventListener('message', (evt) => {
|
|
178
|
+
if (evt.detail.topic === topic) {
|
|
179
|
+
const msg = evt.detail
|
|
180
|
+
expect(msg.data).to.equalBytes(data)
|
|
181
|
+
defer.resolve()
|
|
182
|
+
}
|
|
181
183
|
})
|
|
182
184
|
psA.subscribe(topic)
|
|
183
185
|
|
|
@@ -189,7 +191,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
189
191
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
190
192
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
191
193
|
})
|
|
192
|
-
|
|
194
|
+
psB.publish(topic, data)
|
|
193
195
|
|
|
194
196
|
await defer.promise
|
|
195
197
|
})
|
|
@@ -275,10 +277,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
275
277
|
let subscribedTopics = psA.getTopics()
|
|
276
278
|
expect(subscribedTopics).to.not.include(topic)
|
|
277
279
|
|
|
278
|
-
psA.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
280
|
+
psA.subscribe(topic)
|
|
281
|
+
psA.addEventListener('message', (evt) => {
|
|
282
|
+
if (evt.detail.topic === topic) {
|
|
283
|
+
const msg = evt.detail
|
|
284
|
+
expect(msg.data).to.equalBytes(data)
|
|
285
|
+
defer.resolve()
|
|
286
|
+
}
|
|
282
287
|
})
|
|
283
288
|
psA.subscribe(topic)
|
|
284
289
|
|
|
@@ -290,7 +295,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
290
295
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
291
296
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
292
297
|
})
|
|
293
|
-
|
|
298
|
+
psB.publish(topic, data)
|
|
294
299
|
|
|
295
300
|
await defer.promise
|
|
296
301
|
})
|
|
@@ -357,11 +362,14 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
357
362
|
let subscribedTopics = psA.getTopics()
|
|
358
363
|
expect(subscribedTopics).to.not.include(topic)
|
|
359
364
|
|
|
360
|
-
psA.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
+
psA.subscribe(topic)
|
|
366
|
+
psA.addEventListener('message', (evt) => {
|
|
367
|
+
if (evt.detail.topic === topic) {
|
|
368
|
+
const msg = evt.detail
|
|
369
|
+
expect(msg.data).to.equalBytes(data)
|
|
370
|
+
counter++
|
|
371
|
+
counter === 1 ? defer1.resolve() : defer2.resolve()
|
|
372
|
+
}
|
|
365
373
|
})
|
|
366
374
|
psA.subscribe(topic)
|
|
367
375
|
|
|
@@ -373,7 +381,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
373
381
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
374
382
|
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
375
383
|
})
|
|
376
|
-
|
|
384
|
+
psB.publish(topic, data)
|
|
377
385
|
|
|
378
386
|
await defer1.promise
|
|
379
387
|
|
|
@@ -406,7 +414,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
406
414
|
return subscribedPeers.toString().includes(peerA.toString())
|
|
407
415
|
})
|
|
408
416
|
|
|
409
|
-
|
|
417
|
+
psB.publish(topic, data)
|
|
410
418
|
|
|
411
419
|
await defer2.promise
|
|
412
420
|
})
|
|
@@ -417,8 +425,13 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
417
425
|
let aReceivedSecondMessageFromB = false
|
|
418
426
|
let bReceivedFirstMessageFromA = false
|
|
419
427
|
let bReceivedSecondMessageFromA = false
|
|
428
|
+
const topic = 'reconnect-channel'
|
|
420
429
|
|
|
421
430
|
const handlerSpyA = (evt: CustomEvent<Message>) => {
|
|
431
|
+
if (evt.detail.topic !== topic) {
|
|
432
|
+
return
|
|
433
|
+
}
|
|
434
|
+
|
|
422
435
|
const message = evt.detail
|
|
423
436
|
const data = uint8ArrayToString(message.data)
|
|
424
437
|
|
|
@@ -431,6 +444,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
431
444
|
}
|
|
432
445
|
}
|
|
433
446
|
const handlerSpyB = (evt: CustomEvent<Message>) => {
|
|
447
|
+
if (evt.detail.topic !== topic) {
|
|
448
|
+
return
|
|
449
|
+
}
|
|
450
|
+
|
|
434
451
|
const message = evt.detail
|
|
435
452
|
const data = uint8ArrayToString(message.data)
|
|
436
453
|
|
|
@@ -443,10 +460,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
443
460
|
}
|
|
444
461
|
}
|
|
445
462
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
psA.addEventListener(topic, handlerSpyA)
|
|
449
|
-
psB.addEventListener(topic, handlerSpyB)
|
|
463
|
+
psA.addEventListener('message', handlerSpyA)
|
|
464
|
+
psB.addEventListener('message', handlerSpyB)
|
|
450
465
|
psA.subscribe(topic)
|
|
451
466
|
psB.subscribe(topic)
|
|
452
467
|
|
|
@@ -470,8 +485,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
470
485
|
})
|
|
471
486
|
|
|
472
487
|
// Verify messages go both ways
|
|
473
|
-
|
|
474
|
-
|
|
488
|
+
psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
|
|
489
|
+
psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
|
|
475
490
|
await pWaitFor(() => {
|
|
476
491
|
return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
|
|
477
492
|
})
|
|
@@ -484,8 +499,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
484
499
|
await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
|
|
485
500
|
|
|
486
501
|
// Verify messages go both ways after the disconnect
|
|
487
|
-
|
|
488
|
-
|
|
502
|
+
psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
|
|
503
|
+
psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
|
|
489
504
|
await pWaitFor(() => {
|
|
490
505
|
return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
|
|
491
506
|
})
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -3,7 +3,6 @@ import sinon from 'sinon'
|
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
5
5
|
import { mockRegistrar } from '../mocks/registrar.js'
|
|
6
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
7
6
|
import type { TestSetup } from '../index.js'
|
|
8
7
|
import type { PubSubArgs } from './index.js'
|
|
9
8
|
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
@@ -43,11 +42,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
43
42
|
})
|
|
44
43
|
|
|
45
44
|
it('should emit to self on publish', async () => {
|
|
46
|
-
const promise = new Promise((resolve) =>
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
const promise = new Promise<void>((resolve) => {
|
|
46
|
+
pubsub.addEventListener('message', (evt) => {
|
|
47
|
+
if (evt.detail.topic === topic) {
|
|
48
|
+
resolve()
|
|
49
|
+
}
|
|
50
|
+
}, {
|
|
51
|
+
once: true
|
|
52
|
+
})
|
|
53
|
+
})
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
pubsub.publish(topic, data)
|
|
51
56
|
|
|
52
57
|
return await promise
|
|
53
58
|
})
|
|
@@ -78,11 +83,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
78
83
|
})
|
|
79
84
|
|
|
80
85
|
it('should not emit to self on publish', async () => {
|
|
81
|
-
pubsub.addEventListener(
|
|
86
|
+
pubsub.addEventListener('message', shouldNotHappen, {
|
|
82
87
|
once: true
|
|
83
88
|
})
|
|
84
89
|
|
|
85
|
-
|
|
90
|
+
pubsub.publish(topic, data)
|
|
86
91
|
|
|
87
92
|
// Wait 1 second to guarantee that self is not noticed
|
|
88
93
|
return await new Promise((resolve) => setTimeout(resolve, 1000))
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -8,7 +8,6 @@ import { mockRegistrar } from '../mocks/registrar.js'
|
|
|
8
8
|
import pDefer from 'p-defer'
|
|
9
9
|
import delay from 'delay'
|
|
10
10
|
import pWaitFor from 'p-wait-for'
|
|
11
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
12
11
|
import type { TestSetup } from '../index.js'
|
|
13
12
|
import type { PubSubRPC } from '@libp2p/interfaces/pubsub'
|
|
14
13
|
import type { PubSubArgs } from './index.js'
|
|
@@ -52,7 +51,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
52
51
|
|
|
53
52
|
const spy = sinon.spy(pubsub, 'publishMessage')
|
|
54
53
|
|
|
55
|
-
await pubsub.
|
|
54
|
+
await pubsub.publish(topic, data)
|
|
56
55
|
|
|
57
56
|
await pWaitFor(async () => {
|
|
58
57
|
return spy.callCount === 1
|
|
@@ -120,8 +119,10 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
120
119
|
|
|
121
120
|
const deferred = pDefer()
|
|
122
121
|
|
|
123
|
-
pubsub.addEventListener(
|
|
124
|
-
|
|
122
|
+
pubsub.addEventListener('message', (evt) => {
|
|
123
|
+
if (evt.detail.topic === topic) {
|
|
124
|
+
deferred.resolve()
|
|
125
|
+
}
|
|
125
126
|
})
|
|
126
127
|
|
|
127
128
|
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
@@ -7,7 +7,6 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
8
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
9
9
|
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
10
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
10
|
import { waitForSubscriptionUpdate } from './utils.js'
|
|
12
11
|
import type { TestSetup } from '../index.js'
|
|
13
12
|
import type { Message } from '@libp2p/interfaces/pubsub'
|
|
@@ -149,7 +148,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
149
148
|
psC.subscribe(topic)
|
|
150
149
|
expect(psC.getTopics()).to.deep.equal([topic])
|
|
151
150
|
|
|
152
|
-
psB.addEventListener('
|
|
151
|
+
psB.addEventListener('subscription-change', () => {
|
|
153
152
|
expect(psA.getPeers().length).to.equal(1)
|
|
154
153
|
expect(psB.getPeers().length).to.equal(2)
|
|
155
154
|
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
|
|
@@ -172,9 +171,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
172
171
|
|
|
173
172
|
let counter = 0
|
|
174
173
|
|
|
175
|
-
psA.addEventListener(
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
psA.addEventListener('message', (evt) => {
|
|
175
|
+
if (evt.detail.topic === topic) {
|
|
176
|
+
incMsg(evt)
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
psB.addEventListener('message', (evt) => {
|
|
180
|
+
if (evt.detail.topic === topic) {
|
|
181
|
+
incMsg(evt)
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
psC.addEventListener('message', (evt) => {
|
|
185
|
+
if (evt.detail.topic === topic) {
|
|
186
|
+
incMsg(evt)
|
|
187
|
+
}
|
|
188
|
+
})
|
|
178
189
|
|
|
179
190
|
await Promise.all([
|
|
180
191
|
waitForSubscriptionUpdate(psA, psB),
|
|
@@ -182,7 +193,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
182
193
|
waitForSubscriptionUpdate(psC, psB)
|
|
183
194
|
])
|
|
184
195
|
|
|
185
|
-
|
|
196
|
+
psA.publish(topic, uint8ArrayFromString('hey'))
|
|
186
197
|
|
|
187
198
|
function incMsg (evt: CustomEvent<Message>) {
|
|
188
199
|
const msg = evt.detail
|
|
@@ -192,9 +203,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
192
203
|
|
|
193
204
|
function check () {
|
|
194
205
|
if (++counter === 3) {
|
|
195
|
-
psA.removeEventListener(
|
|
196
|
-
|
|
197
|
-
|
|
206
|
+
psA.removeEventListener('message', (evt) => {
|
|
207
|
+
if (evt.detail.topic === topic) {
|
|
208
|
+
incMsg(evt)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
psB.removeEventListener('message', (evt) => {
|
|
212
|
+
if (evt.detail.topic === topic) {
|
|
213
|
+
incMsg(evt)
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
psC.removeEventListener('message', (evt) => {
|
|
217
|
+
if (evt.detail.topic === topic) {
|
|
218
|
+
incMsg(evt)
|
|
219
|
+
}
|
|
220
|
+
})
|
|
198
221
|
defer.resolve()
|
|
199
222
|
}
|
|
200
223
|
}
|
|
@@ -216,34 +239,33 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
216
239
|
const defer = pDefer()
|
|
217
240
|
let counter = 0
|
|
218
241
|
|
|
242
|
+
psA.addEventListener('message', (evt) => {
|
|
243
|
+
if (evt.detail.topic === topic) {
|
|
244
|
+
incMsg(evt)
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
psB.addEventListener('message', (evt) => {
|
|
248
|
+
if (evt.detail.topic === topic) {
|
|
249
|
+
incMsg(evt)
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
psC.addEventListener('message', (evt) => {
|
|
253
|
+
if (evt.detail.topic === topic) {
|
|
254
|
+
incMsg(evt)
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
|
|
219
258
|
psA.subscribe(topic)
|
|
220
259
|
psB.subscribe(topic)
|
|
221
260
|
psC.subscribe(topic)
|
|
222
261
|
|
|
223
|
-
// await subscription change
|
|
224
|
-
await Promise.all([
|
|
225
|
-
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
226
|
-
once: true
|
|
227
|
-
})),
|
|
228
|
-
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
229
|
-
once: true
|
|
230
|
-
})),
|
|
231
|
-
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
232
|
-
once: true
|
|
233
|
-
}))
|
|
234
|
-
])
|
|
235
|
-
|
|
236
|
-
psA.addEventListener(topic, incMsg)
|
|
237
|
-
psB.addEventListener(topic, incMsg)
|
|
238
|
-
psC.addEventListener(topic, incMsg)
|
|
239
|
-
|
|
240
262
|
await Promise.all([
|
|
241
263
|
waitForSubscriptionUpdate(psA, psB),
|
|
242
264
|
waitForSubscriptionUpdate(psB, psA),
|
|
243
265
|
waitForSubscriptionUpdate(psC, psB)
|
|
244
266
|
])
|
|
245
267
|
|
|
246
|
-
|
|
268
|
+
psB.publish(topic, uint8ArrayFromString('hey'))
|
|
247
269
|
|
|
248
270
|
function incMsg (evt: CustomEvent<Message>) {
|
|
249
271
|
const msg = evt.detail
|
|
@@ -253,9 +275,21 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
253
275
|
|
|
254
276
|
function check () {
|
|
255
277
|
if (++counter === 3) {
|
|
256
|
-
psA.removeEventListener(
|
|
257
|
-
|
|
258
|
-
|
|
278
|
+
psA.removeEventListener('message', (evt) => {
|
|
279
|
+
if (evt.detail.topic === topic) {
|
|
280
|
+
incMsg(evt)
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
psB.removeEventListener('message', (evt) => {
|
|
284
|
+
if (evt.detail.topic === topic) {
|
|
285
|
+
incMsg(evt)
|
|
286
|
+
}
|
|
287
|
+
})
|
|
288
|
+
psC.removeEventListener('message', (evt) => {
|
|
289
|
+
if (evt.detail.topic === topic) {
|
|
290
|
+
incMsg(evt)
|
|
291
|
+
}
|
|
292
|
+
})
|
|
259
293
|
defer.resolve()
|
|
260
294
|
}
|
|
261
295
|
}
|
|
@@ -415,17 +449,38 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
415
449
|
it('publishes from c', async function () {
|
|
416
450
|
const defer = pDefer()
|
|
417
451
|
let counter = 0
|
|
452
|
+
const topic = 'Z'
|
|
418
453
|
|
|
419
|
-
psA.subscribe(
|
|
420
|
-
psA.addEventListener('
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
454
|
+
psA.subscribe(topic)
|
|
455
|
+
psA.addEventListener('message', (evt) => {
|
|
456
|
+
if (evt.detail.topic === topic) {
|
|
457
|
+
incMsg(evt)
|
|
458
|
+
}
|
|
459
|
+
})
|
|
460
|
+
psB.subscribe(topic)
|
|
461
|
+
psB.addEventListener('message', (evt) => {
|
|
462
|
+
if (evt.detail.topic === topic) {
|
|
463
|
+
incMsg(evt)
|
|
464
|
+
}
|
|
465
|
+
})
|
|
466
|
+
psC.subscribe(topic)
|
|
467
|
+
psC.addEventListener('message', (evt) => {
|
|
468
|
+
if (evt.detail.topic === topic) {
|
|
469
|
+
incMsg(evt)
|
|
470
|
+
}
|
|
471
|
+
})
|
|
472
|
+
psD.subscribe(topic)
|
|
473
|
+
psD.addEventListener('message', (evt) => {
|
|
474
|
+
if (evt.detail.topic === topic) {
|
|
475
|
+
incMsg(evt)
|
|
476
|
+
}
|
|
477
|
+
})
|
|
478
|
+
psE.subscribe(topic)
|
|
479
|
+
psE.addEventListener('message', (evt) => {
|
|
480
|
+
if (evt.detail.topic === topic) {
|
|
481
|
+
incMsg(evt)
|
|
482
|
+
}
|
|
483
|
+
})
|
|
429
484
|
|
|
430
485
|
await Promise.all([
|
|
431
486
|
waitForSubscriptionUpdate(psA, psB),
|
|
@@ -435,7 +490,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
435
490
|
waitForSubscriptionUpdate(psE, psD)
|
|
436
491
|
])
|
|
437
492
|
|
|
438
|
-
|
|
493
|
+
psC.publish('Z', uint8ArrayFromString('hey from c'))
|
|
439
494
|
|
|
440
495
|
function incMsg (evt: CustomEvent<Message>) {
|
|
441
496
|
const msg = evt.detail
|
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -7,7 +7,6 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
8
|
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
9
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
10
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
10
|
import { waitForSubscriptionUpdate } from './utils.js'
|
|
12
11
|
import type { TestSetup } from '../index.js'
|
|
13
12
|
import type { Message } from '@libp2p/interfaces/pubsub'
|
|
@@ -89,7 +88,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
89
88
|
it('Subscribe to a topic in nodeA', async () => {
|
|
90
89
|
const defer = pDefer()
|
|
91
90
|
|
|
92
|
-
psB.addEventListener('
|
|
91
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
93
92
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
94
93
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
95
94
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
@@ -110,25 +109,26 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
110
109
|
it('Publish to a topic in nodeA', async () => {
|
|
111
110
|
const defer = pDefer()
|
|
112
111
|
|
|
113
|
-
psA.addEventListener(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
psA.addEventListener('message', (evt) => {
|
|
113
|
+
if (evt.detail.topic === topic) {
|
|
114
|
+
const msg = evt.detail
|
|
115
|
+
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
116
|
+
psB.removeEventListener('message', shouldNotHappen)
|
|
117
|
+
defer.resolve()
|
|
118
|
+
}
|
|
118
119
|
}, {
|
|
119
120
|
once: true
|
|
120
121
|
})
|
|
121
122
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})
|
|
123
|
+
psA.subscribe(topic)
|
|
124
|
+
psB.subscribe(topic)
|
|
125
125
|
|
|
126
126
|
await Promise.all([
|
|
127
127
|
waitForSubscriptionUpdate(psA, psB),
|
|
128
128
|
waitForSubscriptionUpdate(psB, psA)
|
|
129
129
|
])
|
|
130
130
|
|
|
131
|
-
|
|
131
|
+
psA.publish(topic, uint8ArrayFromString('hey'))
|
|
132
132
|
|
|
133
133
|
return await defer.promise
|
|
134
134
|
})
|
|
@@ -136,16 +136,24 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
136
136
|
it('Publish to a topic in nodeB', async () => {
|
|
137
137
|
const defer = pDefer()
|
|
138
138
|
|
|
139
|
-
psA.addEventListener(
|
|
139
|
+
psA.addEventListener('message', (evt) => {
|
|
140
|
+
if (evt.detail.topic !== topic) {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
|
|
140
144
|
const msg = evt.detail
|
|
141
|
-
psA.addEventListener(
|
|
145
|
+
psA.addEventListener('message', (evt) => {
|
|
146
|
+
if (evt.detail.topic === topic) {
|
|
147
|
+
shouldNotHappen()
|
|
148
|
+
}
|
|
149
|
+
}, {
|
|
142
150
|
once: true
|
|
143
151
|
})
|
|
144
152
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
145
153
|
|
|
146
154
|
setTimeout(() => {
|
|
147
|
-
psA.removeEventListener(
|
|
148
|
-
psB.removeEventListener(
|
|
155
|
+
psA.removeEventListener('message')
|
|
156
|
+
psB.removeEventListener('message')
|
|
149
157
|
|
|
150
158
|
defer.resolve()
|
|
151
159
|
}, 100)
|
|
@@ -153,16 +161,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
153
161
|
once: true
|
|
154
162
|
})
|
|
155
163
|
|
|
156
|
-
psB.addEventListener(
|
|
157
|
-
|
|
158
|
-
|
|
164
|
+
psB.addEventListener('message', shouldNotHappen)
|
|
165
|
+
|
|
166
|
+
psA.subscribe(topic)
|
|
167
|
+
psB.subscribe(topic)
|
|
159
168
|
|
|
160
169
|
await Promise.all([
|
|
161
170
|
waitForSubscriptionUpdate(psA, psB),
|
|
162
171
|
waitForSubscriptionUpdate(psB, psA)
|
|
163
172
|
])
|
|
164
173
|
|
|
165
|
-
|
|
174
|
+
psB.publish(topic, uint8ArrayFromString('banana'))
|
|
166
175
|
|
|
167
176
|
return await defer.promise
|
|
168
177
|
})
|
|
@@ -171,10 +180,8 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
171
180
|
const defer = pDefer()
|
|
172
181
|
let counter = 0
|
|
173
182
|
|
|
174
|
-
psB.addEventListener(
|
|
175
|
-
|
|
176
|
-
})
|
|
177
|
-
psA.addEventListener(topic, receivedMsg)
|
|
183
|
+
psB.addEventListener('message', shouldNotHappen)
|
|
184
|
+
psA.addEventListener('message', receivedMsg)
|
|
178
185
|
|
|
179
186
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
180
187
|
const msg = evt.detail
|
|
@@ -184,19 +191,22 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
184
191
|
expect(msg.topic).to.be.equal(topic)
|
|
185
192
|
|
|
186
193
|
if (++counter === 10) {
|
|
187
|
-
psA.removeEventListener(
|
|
188
|
-
psB.removeEventListener(
|
|
194
|
+
psA.removeEventListener('message', receivedMsg)
|
|
195
|
+
psB.removeEventListener('message', shouldNotHappen)
|
|
189
196
|
|
|
190
197
|
defer.resolve()
|
|
191
198
|
}
|
|
192
199
|
}
|
|
193
200
|
|
|
201
|
+
psA.subscribe(topic)
|
|
202
|
+
psB.subscribe(topic)
|
|
203
|
+
|
|
194
204
|
await Promise.all([
|
|
195
205
|
waitForSubscriptionUpdate(psA, psB),
|
|
196
206
|
waitForSubscriptionUpdate(psB, psA)
|
|
197
207
|
])
|
|
198
208
|
|
|
199
|
-
Array.from({ length: 10 }, (_, i) => psB.
|
|
209
|
+
Array.from({ length: 10 }, (_, i) => psB.publish(topic, uint8ArrayFromString('banana')))
|
|
200
210
|
|
|
201
211
|
return await defer.promise
|
|
202
212
|
})
|
|
@@ -205,7 +215,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
205
215
|
const defer = pDefer()
|
|
206
216
|
let callCount = 0
|
|
207
217
|
|
|
208
|
-
psB.addEventListener('
|
|
218
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
209
219
|
callCount++
|
|
210
220
|
|
|
211
221
|
if (callCount === 1) {
|
|
@@ -239,33 +249,5 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
239
249
|
|
|
240
250
|
return await defer.promise
|
|
241
251
|
})
|
|
242
|
-
|
|
243
|
-
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
244
|
-
const defer = pDefer()
|
|
245
|
-
const topic = 'Z'
|
|
246
|
-
|
|
247
|
-
psA.addEventListener(topic, shouldNotHappen, {
|
|
248
|
-
once: true
|
|
249
|
-
})
|
|
250
|
-
psB.addEventListener(topic, shouldNotHappen, {
|
|
251
|
-
once: true
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
await Promise.all([
|
|
255
|
-
waitForSubscriptionUpdate(psA, psB),
|
|
256
|
-
waitForSubscriptionUpdate(psB, psA)
|
|
257
|
-
])
|
|
258
|
-
|
|
259
|
-
setTimeout(() => {
|
|
260
|
-
psA.removeEventListener(topic, shouldNotHappen)
|
|
261
|
-
psB.removeEventListener(topic, shouldNotHappen)
|
|
262
|
-
defer.resolve()
|
|
263
|
-
}, 100)
|
|
264
|
-
|
|
265
|
-
void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
|
|
266
|
-
void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
|
|
267
|
-
|
|
268
|
-
return await defer.promise
|
|
269
|
-
})
|
|
270
252
|
})
|
|
271
253
|
}
|
package/src/pubsub/utils.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
|
5
5
|
|
|
6
6
|
export async function waitForSubscriptionUpdate (a: PubSubBaseProtocol, b: PubSubBaseProtocol) {
|
|
7
7
|
await pWaitFor(async () => {
|
|
8
|
-
const event = await pEvent<'
|
|
8
|
+
const event = await pEvent<'subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'subscription-change')
|
|
9
9
|
|
|
10
10
|
return event.detail.peerId.equals(b.components.getPeerId())
|
|
11
11
|
})
|