@libp2p/interface-compliance-tests 1.1.9 → 1.1.13
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/mocks/connection.d.ts +5 -3
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +21 -13
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/muxer.d.ts.map +1 -1
- package/dist/src/mocks/muxer.js +201 -131
- package/dist/src/mocks/muxer.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +5 -1
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +8 -10
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/mocks/upgrader.d.ts +2 -2
- package/dist/src/mocks/upgrader.d.ts.map +1 -1
- package/dist/src/mocks/upgrader.js +2 -2
- package/dist/src/mocks/upgrader.js.map +1 -1
- package/dist/src/pubsub/api.d.ts +3 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +8 -6
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +3 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +128 -34
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +3 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +3 -2
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +5 -2
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts +3 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +16 -17
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +89 -58
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +3 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +74 -32
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +3 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -0
- package/dist/src/pubsub/utils.js +9 -0
- package/dist/src/pubsub/utils.js.map +1 -0
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/base-test.js +27 -21
- package/dist/src/stream-muxer/base-test.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +25 -28
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/dist/src/stream-muxer/spawner.d.ts.map +1 -1
- package/dist/src/stream-muxer/spawner.js +2 -3
- package/dist/src/stream-muxer/spawner.js.map +1 -1
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/stress-test.js +2 -1
- package/dist/src/stream-muxer/stress-test.js.map +1 -1
- package/dist/src/transport/dial-test.d.ts.map +1 -1
- package/dist/src/transport/dial-test.js +16 -5
- package/dist/src/transport/dial-test.js.map +1 -1
- package/dist/src/transport/listen-test.d.ts.map +1 -1
- package/dist/src/transport/listen-test.js +13 -2
- package/dist/src/transport/listen-test.js.map +1 -1
- package/package.json +5 -1
- package/src/mocks/connection.ts +27 -15
- package/src/mocks/muxer.ts +271 -147
- package/src/mocks/registrar.ts +12 -10
- package/src/mocks/upgrader.ts +4 -4
- package/src/pubsub/api.ts +13 -9
- package/src/pubsub/connection-handlers.ts +148 -43
- package/src/pubsub/emit-self.ts +9 -7
- package/src/pubsub/index.ts +5 -2
- package/src/pubsub/messages.ts +24 -23
- package/src/pubsub/multiple-nodes.ts +104 -74
- package/src/pubsub/two-nodes.ts +87 -39
- package/src/pubsub/utils.ts +12 -0
- package/src/stream-muxer/base-test.ts +35 -24
- package/src/stream-muxer/close-test.ts +32 -29
- package/src/stream-muxer/spawner.ts +3 -6
- package/src/stream-muxer/stress-test.ts +2 -1
- package/src/transport/dial-test.ts +23 -6
- package/src/transport/listen-test.ts +16 -2
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
/* eslint max-nested-callbacks: ["error", 6] */
|
|
2
2
|
import { expect } from 'aegir/utils/chai.js'
|
|
3
3
|
import sinon from 'sinon'
|
|
4
|
-
import delay from 'delay'
|
|
5
4
|
import pDefer from 'p-defer'
|
|
6
5
|
import pWaitFor from 'p-wait-for'
|
|
7
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
9
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
10
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
|
+
import { waitForSubscriptionUpdate } from './utils.js'
|
|
9
12
|
import type { TestSetup } from '../index.js'
|
|
10
|
-
import type {
|
|
13
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
11
14
|
import type { EventMap } from './index.js'
|
|
12
15
|
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
13
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
14
16
|
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
15
|
-
import {
|
|
17
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
16
18
|
|
|
17
|
-
export default (common: TestSetup<
|
|
19
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
18
20
|
describe('pubsub with multiple nodes', function () {
|
|
19
21
|
describe('every peer subscribes to the topic', () => {
|
|
20
22
|
describe('line', () => {
|
|
21
23
|
// line
|
|
22
24
|
// ◉────◉────◉
|
|
23
25
|
// a b c
|
|
24
|
-
let psA:
|
|
25
|
-
let psB:
|
|
26
|
-
let psC:
|
|
26
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
27
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
28
|
+
let psC: PubsubBaseProtocol<EventMap>
|
|
27
29
|
let peerIdA: PeerId
|
|
28
30
|
let peerIdB: PeerId
|
|
29
31
|
let peerIdC: PeerId
|
|
@@ -43,27 +45,42 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
43
45
|
|
|
44
46
|
psA = await common.setup({
|
|
45
47
|
peerId: peerIdA,
|
|
46
|
-
registrar: registrarA
|
|
48
|
+
registrar: registrarA,
|
|
49
|
+
emitSelf: true
|
|
47
50
|
})
|
|
48
51
|
psB = await common.setup({
|
|
49
52
|
peerId: peerIdB,
|
|
50
|
-
registrar: registrarB
|
|
53
|
+
registrar: registrarB,
|
|
54
|
+
emitSelf: true
|
|
51
55
|
})
|
|
52
56
|
psC = await common.setup({
|
|
53
57
|
peerId: peerIdC,
|
|
54
|
-
registrar: registrarC
|
|
58
|
+
registrar: registrarC,
|
|
59
|
+
emitSelf: true
|
|
55
60
|
})
|
|
56
61
|
|
|
57
62
|
// Start pubsub modes
|
|
58
63
|
await Promise.all(
|
|
59
|
-
[psA, psB, psC].map((p) => p.start())
|
|
64
|
+
[psA, psB, psC].map(async (p) => await p.start())
|
|
60
65
|
)
|
|
61
66
|
})
|
|
62
67
|
|
|
63
68
|
// Connect nodes
|
|
64
69
|
beforeEach(async () => {
|
|
65
|
-
await connectPeers(psA.multicodecs[0],
|
|
66
|
-
|
|
70
|
+
await connectPeers(psA.multicodecs[0], {
|
|
71
|
+
peerId: peerIdA,
|
|
72
|
+
registrar: registrarA
|
|
73
|
+
}, {
|
|
74
|
+
peerId: peerIdB,
|
|
75
|
+
registrar: registrarB
|
|
76
|
+
})
|
|
77
|
+
await connectPeers(psA.multicodecs[0], {
|
|
78
|
+
peerId: peerIdB,
|
|
79
|
+
registrar: registrarB
|
|
80
|
+
}, {
|
|
81
|
+
peerId: peerIdC,
|
|
82
|
+
registrar: registrarC
|
|
83
|
+
})
|
|
67
84
|
|
|
68
85
|
// Wait for peers to be ready in pubsub
|
|
69
86
|
await pWaitFor(() =>
|
|
@@ -77,7 +94,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
77
94
|
sinon.restore()
|
|
78
95
|
|
|
79
96
|
await Promise.all(
|
|
80
|
-
[psA, psB, psC].map((p) => p.stop())
|
|
97
|
+
[psA, psB, psC].map(async (p) => await p.stop())
|
|
81
98
|
)
|
|
82
99
|
|
|
83
100
|
await common.teardown()
|
|
@@ -89,12 +106,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
89
106
|
psA.subscribe(topic)
|
|
90
107
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
91
108
|
|
|
92
|
-
await
|
|
93
|
-
once: true
|
|
94
|
-
}))
|
|
95
|
-
expect(psB.getPeers().length).to.equal(2)
|
|
109
|
+
await waitForSubscriptionUpdate(psB, psA)
|
|
96
110
|
|
|
97
|
-
expect(psB.
|
|
111
|
+
expect(psB.getPeers().length).to.equal(2)
|
|
112
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
98
113
|
|
|
99
114
|
expect(psC.getPeers().length).to.equal(1)
|
|
100
115
|
expect(psC.getSubscribers(topic)).to.be.empty()
|
|
@@ -106,19 +121,15 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
106
121
|
expect(psB.getTopics()).to.deep.equal([topic])
|
|
107
122
|
|
|
108
123
|
await Promise.all([
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
})),
|
|
112
|
-
new Promise((resolve) => psC.addEventListener('pubsub:subscription-change', resolve, {
|
|
113
|
-
once: true
|
|
114
|
-
}))
|
|
124
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
125
|
+
waitForSubscriptionUpdate(psC, psB)
|
|
115
126
|
])
|
|
116
127
|
|
|
117
128
|
expect(psA.getPeers().length).to.equal(1)
|
|
118
|
-
expect(psA.getSubscribers(topic)).to.deep.equal([peerIdB])
|
|
129
|
+
expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
119
130
|
|
|
120
131
|
expect(psC.getPeers().length).to.equal(1)
|
|
121
|
-
expect(psC.getSubscribers(topic)).to.deep.equal([peerIdB])
|
|
132
|
+
expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
122
133
|
})
|
|
123
134
|
|
|
124
135
|
it('subscribe to the topic on node c', async () => {
|
|
@@ -131,7 +142,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
131
142
|
psB.addEventListener('pubsub:subscription-change', () => {
|
|
132
143
|
expect(psA.getPeers().length).to.equal(1)
|
|
133
144
|
expect(psB.getPeers().length).to.equal(2)
|
|
134
|
-
expect(psB.getSubscribers(topic)).to.deep.equal([peerIdC])
|
|
145
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
|
|
135
146
|
|
|
136
147
|
defer.resolve()
|
|
137
148
|
}, {
|
|
@@ -149,29 +160,19 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
149
160
|
psB.subscribe(topic)
|
|
150
161
|
psC.subscribe(topic)
|
|
151
162
|
|
|
152
|
-
// await subscription change
|
|
153
|
-
await Promise.all([
|
|
154
|
-
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
155
|
-
once: true
|
|
156
|
-
})),
|
|
157
|
-
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
158
|
-
once: true
|
|
159
|
-
})),
|
|
160
|
-
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
161
|
-
once: true
|
|
162
|
-
}))
|
|
163
|
-
])
|
|
164
|
-
|
|
165
|
-
// await a cycle
|
|
166
|
-
await delay(1000)
|
|
167
|
-
|
|
168
163
|
let counter = 0
|
|
169
164
|
|
|
170
165
|
psA.addEventListener(topic, incMsg)
|
|
171
166
|
psB.addEventListener(topic, incMsg)
|
|
172
167
|
psC.addEventListener(topic, incMsg)
|
|
173
168
|
|
|
174
|
-
|
|
169
|
+
await Promise.all([
|
|
170
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
171
|
+
waitForSubscriptionUpdate(psB, psA),
|
|
172
|
+
waitForSubscriptionUpdate(psC, psB)
|
|
173
|
+
])
|
|
174
|
+
|
|
175
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
175
176
|
|
|
176
177
|
function incMsg (evt: CustomEvent<Message>) {
|
|
177
178
|
const msg = evt.detail
|
|
@@ -226,10 +227,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
226
227
|
psB.addEventListener(topic, incMsg)
|
|
227
228
|
psC.addEventListener(topic, incMsg)
|
|
228
229
|
|
|
229
|
-
|
|
230
|
-
|
|
230
|
+
await Promise.all([
|
|
231
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
232
|
+
waitForSubscriptionUpdate(psB, psA),
|
|
233
|
+
waitForSubscriptionUpdate(psC, psB)
|
|
234
|
+
])
|
|
231
235
|
|
|
232
|
-
void psB.
|
|
236
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
233
237
|
|
|
234
238
|
function incMsg (evt: CustomEvent<Message>) {
|
|
235
239
|
const msg = evt.detail
|
|
@@ -259,11 +263,11 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
259
263
|
// │b d│
|
|
260
264
|
// ◉─┘ └─◉
|
|
261
265
|
// a
|
|
262
|
-
let psA:
|
|
263
|
-
let psB:
|
|
264
|
-
let psC:
|
|
265
|
-
let psD:
|
|
266
|
-
let psE:
|
|
266
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
267
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
268
|
+
let psC: PubsubBaseProtocol<EventMap>
|
|
269
|
+
let psD: PubsubBaseProtocol<EventMap>
|
|
270
|
+
let psE: PubsubBaseProtocol<EventMap>
|
|
267
271
|
let peerIdA: PeerId
|
|
268
272
|
let peerIdB: PeerId
|
|
269
273
|
let peerIdC: PeerId
|
|
@@ -291,37 +295,66 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
291
295
|
|
|
292
296
|
psA = await common.setup({
|
|
293
297
|
peerId: peerIdA,
|
|
294
|
-
registrar: registrarA
|
|
298
|
+
registrar: registrarA,
|
|
299
|
+
emitSelf: true
|
|
295
300
|
})
|
|
296
301
|
psB = await common.setup({
|
|
297
302
|
peerId: peerIdB,
|
|
298
|
-
registrar: registrarB
|
|
303
|
+
registrar: registrarB,
|
|
304
|
+
emitSelf: true
|
|
299
305
|
})
|
|
300
306
|
psC = await common.setup({
|
|
301
307
|
peerId: peerIdC,
|
|
302
|
-
registrar: registrarC
|
|
308
|
+
registrar: registrarC,
|
|
309
|
+
emitSelf: true
|
|
303
310
|
})
|
|
304
311
|
psD = await common.setup({
|
|
305
312
|
peerId: peerIdD,
|
|
306
|
-
registrar: registrarD
|
|
313
|
+
registrar: registrarD,
|
|
314
|
+
emitSelf: true
|
|
307
315
|
})
|
|
308
316
|
psE = await common.setup({
|
|
309
317
|
peerId: peerIdE,
|
|
310
|
-
registrar: registrarE
|
|
318
|
+
registrar: registrarE,
|
|
319
|
+
emitSelf: true
|
|
311
320
|
})
|
|
312
321
|
|
|
313
322
|
// Start pubsub nodes
|
|
314
323
|
await Promise.all(
|
|
315
|
-
[psA, psB, psC, psD, psE].map((p) => p.start())
|
|
324
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.start())
|
|
316
325
|
)
|
|
317
326
|
})
|
|
318
327
|
|
|
319
328
|
// connect nodes
|
|
320
329
|
beforeEach(async () => {
|
|
321
|
-
await connectPeers(psA.multicodecs[0],
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
330
|
+
await connectPeers(psA.multicodecs[0], {
|
|
331
|
+
peerId: peerIdA,
|
|
332
|
+
registrar: registrarA
|
|
333
|
+
}, {
|
|
334
|
+
peerId: peerIdB,
|
|
335
|
+
registrar: registrarB
|
|
336
|
+
})
|
|
337
|
+
await connectPeers(psA.multicodecs[0], {
|
|
338
|
+
peerId: peerIdB,
|
|
339
|
+
registrar: registrarB
|
|
340
|
+
}, {
|
|
341
|
+
peerId: peerIdC,
|
|
342
|
+
registrar: registrarC
|
|
343
|
+
})
|
|
344
|
+
await connectPeers(psA.multicodecs[0], {
|
|
345
|
+
peerId: peerIdC,
|
|
346
|
+
registrar: registrarC
|
|
347
|
+
}, {
|
|
348
|
+
peerId: peerIdD,
|
|
349
|
+
registrar: registrarD
|
|
350
|
+
})
|
|
351
|
+
await connectPeers(psA.multicodecs[0], {
|
|
352
|
+
peerId: peerIdD,
|
|
353
|
+
registrar: registrarD
|
|
354
|
+
}, {
|
|
355
|
+
peerId: peerIdE,
|
|
356
|
+
registrar: registrarE
|
|
357
|
+
})
|
|
325
358
|
|
|
326
359
|
// Wait for peers to be ready in pubsub
|
|
327
360
|
await pWaitFor(() =>
|
|
@@ -335,7 +368,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
335
368
|
|
|
336
369
|
afterEach(async () => {
|
|
337
370
|
await Promise.all(
|
|
338
|
-
[psA, psB, psC, psD, psE].map((p) => p.stop())
|
|
371
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.stop())
|
|
339
372
|
)
|
|
340
373
|
await common.teardown()
|
|
341
374
|
})
|
|
@@ -369,17 +402,14 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
369
402
|
psE.addEventListener('Z', incMsg)
|
|
370
403
|
|
|
371
404
|
await Promise.all([
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
405
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
406
|
+
waitForSubscriptionUpdate(psB, psA),
|
|
407
|
+
waitForSubscriptionUpdate(psC, psB),
|
|
408
|
+
waitForSubscriptionUpdate(psD, psC),
|
|
409
|
+
waitForSubscriptionUpdate(psE, psD)
|
|
377
410
|
])
|
|
378
411
|
|
|
379
|
-
|
|
380
|
-
await delay(1000)
|
|
381
|
-
|
|
382
|
-
void psC.publish('Z', uint8ArrayFromString('hey from c'))
|
|
412
|
+
void psC.dispatchEvent(new CustomEvent('Z', { detail: uint8ArrayFromString('hey from c') }))
|
|
383
413
|
|
|
384
414
|
function incMsg (evt: CustomEvent<Message>) {
|
|
385
415
|
const msg = evt.detail
|
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -5,13 +5,16 @@ import pDefer from 'p-defer'
|
|
|
5
5
|
import pWaitFor from 'p-wait-for'
|
|
6
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
10
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
|
+
import { waitForSubscriptionUpdate } from './utils.js'
|
|
8
12
|
import type { TestSetup } from '../index.js'
|
|
9
|
-
import type {
|
|
13
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
10
14
|
import type { EventMap } from './index.js'
|
|
11
|
-
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
12
15
|
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
13
16
|
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
14
|
-
import {
|
|
17
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
15
18
|
|
|
16
19
|
const topic = 'foo'
|
|
17
20
|
|
|
@@ -19,17 +22,17 @@ function shouldNotHappen () {
|
|
|
19
22
|
expect.fail()
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
export default (common: TestSetup<
|
|
25
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
23
26
|
describe('pubsub with two nodes', () => {
|
|
24
|
-
let psA:
|
|
25
|
-
let psB:
|
|
27
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
28
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
26
29
|
let peerIdA: PeerId
|
|
27
30
|
let peerIdB: PeerId
|
|
28
31
|
let registrarA: Registrar
|
|
29
32
|
let registrarB: Registrar
|
|
30
33
|
|
|
31
34
|
// Create pubsub nodes and connect them
|
|
32
|
-
|
|
35
|
+
beforeEach(async () => {
|
|
33
36
|
peerIdA = await createEd25519PeerId()
|
|
34
37
|
peerIdB = await createEd25519PeerId()
|
|
35
38
|
|
|
@@ -38,11 +41,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
38
41
|
|
|
39
42
|
psA = await common.setup({
|
|
40
43
|
peerId: peerIdA,
|
|
41
|
-
registrar: registrarA
|
|
44
|
+
registrar: registrarA,
|
|
45
|
+
emitSelf: true
|
|
42
46
|
})
|
|
43
47
|
psB = await common.setup({
|
|
44
48
|
peerId: peerIdB,
|
|
45
|
-
registrar: registrarB
|
|
49
|
+
registrar: registrarB,
|
|
50
|
+
emitSelf: false
|
|
46
51
|
})
|
|
47
52
|
|
|
48
53
|
// Start pubsub and connect nodes
|
|
@@ -52,13 +57,19 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
52
57
|
expect(psA.getPeers()).to.be.empty()
|
|
53
58
|
expect(psB.getPeers()).to.be.empty()
|
|
54
59
|
|
|
55
|
-
await connectPeers(psA.multicodecs[0],
|
|
60
|
+
await connectPeers(psA.multicodecs[0], {
|
|
61
|
+
peerId: peerIdA,
|
|
62
|
+
registrar: registrarA
|
|
63
|
+
}, {
|
|
64
|
+
peerId: peerIdB,
|
|
65
|
+
registrar: registrarB
|
|
66
|
+
})
|
|
56
67
|
|
|
57
68
|
// Wait for peers to be ready in pubsub
|
|
58
69
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
59
70
|
})
|
|
60
71
|
|
|
61
|
-
|
|
72
|
+
afterEach(async () => {
|
|
62
73
|
sinon.restore()
|
|
63
74
|
|
|
64
75
|
await psA.stop()
|
|
@@ -74,10 +85,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
74
85
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
75
86
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
76
87
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
77
|
-
expect(psB.getSubscribers(topic)).to.deep.equal([peerIdA])
|
|
88
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
78
89
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
79
90
|
expect(changedSubs).to.have.lengthOf(1)
|
|
80
|
-
expect(changedSubs[0].
|
|
91
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
81
92
|
expect(changedSubs[0].subscribe).to.equal(true)
|
|
82
93
|
defer.resolve()
|
|
83
94
|
}, {
|
|
@@ -104,7 +115,12 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
104
115
|
once: true
|
|
105
116
|
})
|
|
106
117
|
|
|
107
|
-
|
|
118
|
+
await Promise.all([
|
|
119
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
120
|
+
waitForSubscriptionUpdate(psB, psA)
|
|
121
|
+
])
|
|
122
|
+
|
|
123
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
108
124
|
|
|
109
125
|
return await defer.promise
|
|
110
126
|
})
|
|
@@ -133,7 +149,12 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
133
149
|
once: true
|
|
134
150
|
})
|
|
135
151
|
|
|
136
|
-
|
|
152
|
+
await Promise.all([
|
|
153
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
154
|
+
waitForSubscriptionUpdate(psB, psA)
|
|
155
|
+
])
|
|
156
|
+
|
|
157
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
137
158
|
|
|
138
159
|
return await defer.promise
|
|
139
160
|
})
|
|
@@ -150,9 +171,9 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
150
171
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
151
172
|
const msg = evt.detail
|
|
152
173
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
153
|
-
expect(msg.from).to.
|
|
154
|
-
expect(msg.seqno).to.be.a('
|
|
155
|
-
expect(msg.
|
|
174
|
+
expect(msg.from.toString()).to.equal(peerIdB.toString())
|
|
175
|
+
expect(msg.seqno).to.be.a('BigInt')
|
|
176
|
+
expect(msg.topic).to.be.equal(topic)
|
|
156
177
|
|
|
157
178
|
if (++counter === 10) {
|
|
158
179
|
psA.removeEventListener(topic, receivedMsg)
|
|
@@ -162,52 +183,79 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
162
183
|
}
|
|
163
184
|
}
|
|
164
185
|
|
|
165
|
-
|
|
186
|
+
await Promise.all([
|
|
187
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
188
|
+
waitForSubscriptionUpdate(psB, psA)
|
|
189
|
+
])
|
|
190
|
+
|
|
191
|
+
Array.from({ length: 10 }, (_, i) => psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') })))
|
|
166
192
|
|
|
167
193
|
return await defer.promise
|
|
168
194
|
})
|
|
169
195
|
|
|
170
196
|
it('Unsubscribe from topic in nodeA', async () => {
|
|
171
197
|
const defer = pDefer()
|
|
172
|
-
|
|
173
|
-
psA.unsubscribe(topic)
|
|
174
|
-
expect(psA.getTopics()).to.be.empty()
|
|
198
|
+
let callCount = 0
|
|
175
199
|
|
|
176
200
|
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
201
|
+
callCount++
|
|
202
|
+
|
|
203
|
+
if (callCount === 1) {
|
|
204
|
+
// notice subscribe
|
|
205
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
206
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
207
|
+
expect(psB.getTopics()).to.be.empty()
|
|
208
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
209
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
210
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
211
|
+
expect(changedSubs[0].subscribe).to.equal(true)
|
|
212
|
+
} else {
|
|
213
|
+
// notice unsubscribe
|
|
214
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
215
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
216
|
+
expect(psB.getTopics()).to.be.empty()
|
|
217
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
218
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
219
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
220
|
+
expect(changedSubs[0].subscribe).to.equal(false)
|
|
184
221
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
once: true
|
|
222
|
+
defer.resolve()
|
|
223
|
+
}
|
|
188
224
|
})
|
|
189
225
|
|
|
226
|
+
psA.subscribe(topic)
|
|
227
|
+
expect(psA.getTopics()).to.not.be.empty()
|
|
228
|
+
|
|
229
|
+
psA.unsubscribe(topic)
|
|
230
|
+
expect(psA.getTopics()).to.be.empty()
|
|
231
|
+
|
|
190
232
|
return await defer.promise
|
|
191
233
|
})
|
|
192
234
|
|
|
193
|
-
it('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
235
|
+
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
194
236
|
const defer = pDefer()
|
|
237
|
+
const topic = 'Z'
|
|
195
238
|
|
|
196
|
-
psA.addEventListener(
|
|
239
|
+
psA.addEventListener(topic, shouldNotHappen, {
|
|
197
240
|
once: true
|
|
198
241
|
})
|
|
199
|
-
psB.addEventListener(
|
|
242
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
200
243
|
once: true
|
|
201
244
|
})
|
|
202
245
|
|
|
246
|
+
await Promise.all([
|
|
247
|
+
waitForSubscriptionUpdate(psA, psB),
|
|
248
|
+
waitForSubscriptionUpdate(psB, psA)
|
|
249
|
+
])
|
|
250
|
+
|
|
203
251
|
setTimeout(() => {
|
|
204
|
-
psA.removeEventListener(
|
|
205
|
-
psB.removeEventListener(
|
|
252
|
+
psA.removeEventListener(topic, shouldNotHappen)
|
|
253
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
206
254
|
defer.resolve()
|
|
207
255
|
}, 100)
|
|
208
256
|
|
|
209
|
-
void psB.
|
|
210
|
-
void psA.
|
|
257
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
258
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
211
259
|
|
|
212
260
|
return await defer.promise
|
|
213
261
|
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { pEvent } from 'p-event'
|
|
2
|
+
import pWaitFor from 'p-wait-for'
|
|
3
|
+
import type { SubscriptionChangeData } from '@libp2p/interfaces/src/pubsub'
|
|
4
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
5
|
+
|
|
6
|
+
export async function waitForSubscriptionUpdate (a: PubsubBaseProtocol, b: PubsubBaseProtocol) {
|
|
7
|
+
await pWaitFor(async () => {
|
|
8
|
+
const event = await pEvent<'pubsub:subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'pubsub:subscription-change')
|
|
9
|
+
|
|
10
|
+
return event.detail.peerId.equals(b.peerId)
|
|
11
|
+
})
|
|
12
|
+
}
|