@libp2p/interface-compliance-tests 1.1.9 → 1.1.10
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 -1
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +16 -8
- package/dist/src/mocks/connection.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/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 +80 -34
- 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 +57 -29
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection.ts +22 -8
- package/src/mocks/registrar.ts +12 -10
- 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 +95 -47
- package/src/pubsub/two-nodes.ts +69 -36
|
@@ -6,24 +6,26 @@ import pDefer from 'p-defer'
|
|
|
6
6
|
import pWaitFor from 'p-wait-for'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
9
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
10
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
11
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
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()
|
|
@@ -94,7 +111,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
94
111
|
}))
|
|
95
112
|
expect(psB.getPeers().length).to.equal(2)
|
|
96
113
|
|
|
97
|
-
expect(psB.getSubscribers(topic)).to.deep.equal([peerIdA])
|
|
114
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
98
115
|
|
|
99
116
|
expect(psC.getPeers().length).to.equal(1)
|
|
100
117
|
expect(psC.getSubscribers(topic)).to.be.empty()
|
|
@@ -115,10 +132,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
115
132
|
])
|
|
116
133
|
|
|
117
134
|
expect(psA.getPeers().length).to.equal(1)
|
|
118
|
-
expect(psA.getSubscribers(topic)).to.deep.equal([peerIdB])
|
|
135
|
+
expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
119
136
|
|
|
120
137
|
expect(psC.getPeers().length).to.equal(1)
|
|
121
|
-
expect(psC.getSubscribers(topic)).to.deep.equal([peerIdB])
|
|
138
|
+
expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
122
139
|
})
|
|
123
140
|
|
|
124
141
|
it('subscribe to the topic on node c', async () => {
|
|
@@ -131,7 +148,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
131
148
|
psB.addEventListener('pubsub:subscription-change', () => {
|
|
132
149
|
expect(psA.getPeers().length).to.equal(1)
|
|
133
150
|
expect(psB.getPeers().length).to.equal(2)
|
|
134
|
-
expect(psB.getSubscribers(topic)).to.deep.equal([peerIdC])
|
|
151
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
|
|
135
152
|
|
|
136
153
|
defer.resolve()
|
|
137
154
|
}, {
|
|
@@ -145,25 +162,24 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
145
162
|
const topic = 'Z'
|
|
146
163
|
const defer = pDefer()
|
|
147
164
|
|
|
148
|
-
psA.subscribe(topic)
|
|
149
|
-
psB.subscribe(topic)
|
|
150
|
-
psC.subscribe(topic)
|
|
151
|
-
|
|
152
165
|
// await subscription change
|
|
153
|
-
|
|
154
|
-
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(
|
|
166
|
+
const p = Promise.all([
|
|
167
|
+
new Promise<void>(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
155
168
|
once: true
|
|
156
169
|
})),
|
|
157
|
-
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(
|
|
170
|
+
new Promise<void>(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
158
171
|
once: true
|
|
159
172
|
})),
|
|
160
|
-
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(
|
|
173
|
+
new Promise<void>(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
161
174
|
once: true
|
|
162
175
|
}))
|
|
163
176
|
])
|
|
164
177
|
|
|
165
|
-
|
|
166
|
-
|
|
178
|
+
psA.subscribe(topic)
|
|
179
|
+
psB.subscribe(topic)
|
|
180
|
+
psC.subscribe(topic)
|
|
181
|
+
|
|
182
|
+
await p
|
|
167
183
|
|
|
168
184
|
let counter = 0
|
|
169
185
|
|
|
@@ -171,7 +187,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
171
187
|
psB.addEventListener(topic, incMsg)
|
|
172
188
|
psC.addEventListener(topic, incMsg)
|
|
173
189
|
|
|
174
|
-
|
|
190
|
+
// await a cycle
|
|
191
|
+
await delay(1000)
|
|
192
|
+
|
|
193
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
175
194
|
|
|
176
195
|
function incMsg (evt: CustomEvent<Message>) {
|
|
177
196
|
const msg = evt.detail
|
|
@@ -229,7 +248,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
229
248
|
// await a cycle
|
|
230
249
|
await delay(1000)
|
|
231
250
|
|
|
232
|
-
void psB.
|
|
251
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
233
252
|
|
|
234
253
|
function incMsg (evt: CustomEvent<Message>) {
|
|
235
254
|
const msg = evt.detail
|
|
@@ -259,11 +278,11 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
259
278
|
// │b d│
|
|
260
279
|
// ◉─┘ └─◉
|
|
261
280
|
// a
|
|
262
|
-
let psA:
|
|
263
|
-
let psB:
|
|
264
|
-
let psC:
|
|
265
|
-
let psD:
|
|
266
|
-
let psE:
|
|
281
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
282
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
283
|
+
let psC: PubsubBaseProtocol<EventMap>
|
|
284
|
+
let psD: PubsubBaseProtocol<EventMap>
|
|
285
|
+
let psE: PubsubBaseProtocol<EventMap>
|
|
267
286
|
let peerIdA: PeerId
|
|
268
287
|
let peerIdB: PeerId
|
|
269
288
|
let peerIdC: PeerId
|
|
@@ -291,37 +310,66 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
291
310
|
|
|
292
311
|
psA = await common.setup({
|
|
293
312
|
peerId: peerIdA,
|
|
294
|
-
registrar: registrarA
|
|
313
|
+
registrar: registrarA,
|
|
314
|
+
emitSelf: true
|
|
295
315
|
})
|
|
296
316
|
psB = await common.setup({
|
|
297
317
|
peerId: peerIdB,
|
|
298
|
-
registrar: registrarB
|
|
318
|
+
registrar: registrarB,
|
|
319
|
+
emitSelf: true
|
|
299
320
|
})
|
|
300
321
|
psC = await common.setup({
|
|
301
322
|
peerId: peerIdC,
|
|
302
|
-
registrar: registrarC
|
|
323
|
+
registrar: registrarC,
|
|
324
|
+
emitSelf: true
|
|
303
325
|
})
|
|
304
326
|
psD = await common.setup({
|
|
305
327
|
peerId: peerIdD,
|
|
306
|
-
registrar: registrarD
|
|
328
|
+
registrar: registrarD,
|
|
329
|
+
emitSelf: true
|
|
307
330
|
})
|
|
308
331
|
psE = await common.setup({
|
|
309
332
|
peerId: peerIdE,
|
|
310
|
-
registrar: registrarE
|
|
333
|
+
registrar: registrarE,
|
|
334
|
+
emitSelf: true
|
|
311
335
|
})
|
|
312
336
|
|
|
313
337
|
// Start pubsub nodes
|
|
314
338
|
await Promise.all(
|
|
315
|
-
[psA, psB, psC, psD, psE].map((p) => p.start())
|
|
339
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.start())
|
|
316
340
|
)
|
|
317
341
|
})
|
|
318
342
|
|
|
319
343
|
// connect nodes
|
|
320
344
|
beforeEach(async () => {
|
|
321
|
-
await connectPeers(psA.multicodecs[0],
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
345
|
+
await connectPeers(psA.multicodecs[0], {
|
|
346
|
+
peerId: peerIdA,
|
|
347
|
+
registrar: registrarA
|
|
348
|
+
}, {
|
|
349
|
+
peerId: peerIdB,
|
|
350
|
+
registrar: registrarB
|
|
351
|
+
})
|
|
352
|
+
await connectPeers(psA.multicodecs[0], {
|
|
353
|
+
peerId: peerIdB,
|
|
354
|
+
registrar: registrarB
|
|
355
|
+
}, {
|
|
356
|
+
peerId: peerIdC,
|
|
357
|
+
registrar: registrarC
|
|
358
|
+
})
|
|
359
|
+
await connectPeers(psA.multicodecs[0], {
|
|
360
|
+
peerId: peerIdC,
|
|
361
|
+
registrar: registrarC
|
|
362
|
+
}, {
|
|
363
|
+
peerId: peerIdD,
|
|
364
|
+
registrar: registrarD
|
|
365
|
+
})
|
|
366
|
+
await connectPeers(psA.multicodecs[0], {
|
|
367
|
+
peerId: peerIdD,
|
|
368
|
+
registrar: registrarD
|
|
369
|
+
}, {
|
|
370
|
+
peerId: peerIdE,
|
|
371
|
+
registrar: registrarE
|
|
372
|
+
})
|
|
325
373
|
|
|
326
374
|
// Wait for peers to be ready in pubsub
|
|
327
375
|
await pWaitFor(() =>
|
|
@@ -335,7 +383,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
335
383
|
|
|
336
384
|
afterEach(async () => {
|
|
337
385
|
await Promise.all(
|
|
338
|
-
[psA, psB, psC, psD, psE].map((p) => p.stop())
|
|
386
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.stop())
|
|
339
387
|
)
|
|
340
388
|
await common.teardown()
|
|
341
389
|
})
|
|
@@ -379,7 +427,7 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
379
427
|
// await a cycle
|
|
380
428
|
await delay(1000)
|
|
381
429
|
|
|
382
|
-
void psC.
|
|
430
|
+
void psC.dispatchEvent(new CustomEvent('Z', { detail: uint8ArrayFromString('hey from c') }))
|
|
383
431
|
|
|
384
432
|
function incMsg (evt: CustomEvent<Message>) {
|
|
385
433
|
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 delay from 'delay'
|
|
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,10 +22,10 @@ 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
|
|
@@ -38,7 +41,8 @@ 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,
|
|
@@ -52,7 +56,13 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
52
56
|
expect(psA.getPeers()).to.be.empty()
|
|
53
57
|
expect(psB.getPeers()).to.be.empty()
|
|
54
58
|
|
|
55
|
-
await connectPeers(psA.multicodecs[0],
|
|
59
|
+
await connectPeers(psA.multicodecs[0], {
|
|
60
|
+
peerId: peerIdA,
|
|
61
|
+
registrar: registrarA
|
|
62
|
+
}, {
|
|
63
|
+
peerId: peerIdB,
|
|
64
|
+
registrar: registrarB
|
|
65
|
+
})
|
|
56
66
|
|
|
57
67
|
// Wait for peers to be ready in pubsub
|
|
58
68
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
@@ -74,10 +84,10 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
74
84
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
75
85
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
76
86
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
77
|
-
expect(psB.getSubscribers(topic)).to.deep.equal([peerIdA])
|
|
87
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
78
88
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
79
89
|
expect(changedSubs).to.have.lengthOf(1)
|
|
80
|
-
expect(changedSubs[0].
|
|
90
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
81
91
|
expect(changedSubs[0].subscribe).to.equal(true)
|
|
82
92
|
defer.resolve()
|
|
83
93
|
}, {
|
|
@@ -104,7 +114,9 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
104
114
|
once: true
|
|
105
115
|
})
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
await delay(100)
|
|
118
|
+
|
|
119
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
108
120
|
|
|
109
121
|
return await defer.promise
|
|
110
122
|
})
|
|
@@ -133,7 +145,9 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
133
145
|
once: true
|
|
134
146
|
})
|
|
135
147
|
|
|
136
|
-
|
|
148
|
+
await delay(100)
|
|
149
|
+
|
|
150
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
137
151
|
|
|
138
152
|
return await defer.promise
|
|
139
153
|
})
|
|
@@ -150,9 +164,9 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
150
164
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
151
165
|
const msg = evt.detail
|
|
152
166
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
153
|
-
expect(msg.from).to.
|
|
154
|
-
expect(msg.seqno).to.be.a('
|
|
155
|
-
expect(msg.
|
|
167
|
+
expect(msg.from.toString()).to.equal(peerIdB.toString())
|
|
168
|
+
expect(msg.seqno).to.be.a('BigInt')
|
|
169
|
+
expect(msg.topic).to.be.equal(topic)
|
|
156
170
|
|
|
157
171
|
if (++counter === 10) {
|
|
158
172
|
psA.removeEventListener(topic, receivedMsg)
|
|
@@ -162,52 +176,71 @@ export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
|
162
176
|
}
|
|
163
177
|
}
|
|
164
178
|
|
|
165
|
-
|
|
179
|
+
await delay(100)
|
|
180
|
+
|
|
181
|
+
Array.from({ length: 10 }, (_, i) => psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') })))
|
|
166
182
|
|
|
167
183
|
return await defer.promise
|
|
168
184
|
})
|
|
169
185
|
|
|
170
186
|
it('Unsubscribe from topic in nodeA', async () => {
|
|
171
187
|
const defer = pDefer()
|
|
172
|
-
|
|
173
|
-
psA.unsubscribe(topic)
|
|
174
|
-
expect(psA.getTopics()).to.be.empty()
|
|
188
|
+
let callCount = 0
|
|
175
189
|
|
|
176
190
|
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
callCount++
|
|
192
|
+
|
|
193
|
+
if (callCount === 1) {
|
|
194
|
+
// notice subscribe
|
|
195
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
196
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
197
|
+
expect(psB.getTopics()).to.be.empty()
|
|
198
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
199
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
200
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
201
|
+
expect(changedSubs[0].subscribe).to.equal(true)
|
|
202
|
+
} else {
|
|
203
|
+
// notice unsubscribe
|
|
204
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
205
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
206
|
+
expect(psB.getTopics()).to.be.empty()
|
|
207
|
+
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
208
|
+
expect(changedSubs).to.have.lengthOf(1)
|
|
209
|
+
expect(changedSubs[0].topic).to.equal(topic)
|
|
210
|
+
expect(changedSubs[0].subscribe).to.equal(false)
|
|
184
211
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
once: true
|
|
212
|
+
defer.resolve()
|
|
213
|
+
}
|
|
188
214
|
})
|
|
189
215
|
|
|
216
|
+
psA.subscribe(topic)
|
|
217
|
+
expect(psA.getTopics()).to.not.be.empty()
|
|
218
|
+
|
|
219
|
+
psA.unsubscribe(topic)
|
|
220
|
+
expect(psA.getTopics()).to.be.empty()
|
|
221
|
+
|
|
190
222
|
return await defer.promise
|
|
191
223
|
})
|
|
192
224
|
|
|
193
|
-
it('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
225
|
+
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
194
226
|
const defer = pDefer()
|
|
227
|
+
const topic = 'Z'
|
|
195
228
|
|
|
196
|
-
psA.addEventListener(
|
|
229
|
+
psA.addEventListener(topic, shouldNotHappen, {
|
|
197
230
|
once: true
|
|
198
231
|
})
|
|
199
|
-
psB.addEventListener(
|
|
232
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
200
233
|
once: true
|
|
201
234
|
})
|
|
202
235
|
|
|
203
236
|
setTimeout(() => {
|
|
204
|
-
psA.removeEventListener(
|
|
205
|
-
psB.removeEventListener(
|
|
237
|
+
psA.removeEventListener(topic, shouldNotHappen)
|
|
238
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
206
239
|
defer.resolve()
|
|
207
240
|
}, 100)
|
|
208
241
|
|
|
209
|
-
void psB.
|
|
210
|
-
void psA.
|
|
242
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
243
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('banana') }))
|
|
211
244
|
|
|
212
245
|
return await defer.promise
|
|
213
246
|
})
|