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