@libp2p/interface-compliance-tests 6.4.16 → 6.5.0-8484de8a2

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.
Files changed (132) hide show
  1. package/dist/src/connection-encryption/index.d.ts.map +1 -1
  2. package/dist/src/connection-encryption/index.js +24 -15
  3. package/dist/src/connection-encryption/index.js.map +1 -1
  4. package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
  5. package/dist/src/stream-muxer/base-test.js +62 -341
  6. package/dist/src/stream-muxer/base-test.js.map +1 -1
  7. package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
  8. package/dist/src/stream-muxer/close-test.js +254 -305
  9. package/dist/src/stream-muxer/close-test.js.map +1 -1
  10. package/dist/src/stream-muxer/index.js +2 -2
  11. package/dist/src/stream-muxer/index.js.map +1 -1
  12. package/dist/src/stream-muxer/{mega-stress-test.d.ts → stream-test.d.ts} +2 -2
  13. package/dist/src/stream-muxer/stream-test.d.ts.map +1 -0
  14. package/dist/src/stream-muxer/stream-test.js +289 -0
  15. package/dist/src/stream-muxer/stream-test.js.map +1 -0
  16. package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
  17. package/dist/src/stream-muxer/stress-test.js +70 -16
  18. package/dist/src/stream-muxer/stress-test.js.map +1 -1
  19. package/dist/src/transport/index.d.ts.map +1 -1
  20. package/dist/src/transport/index.js +232 -203
  21. package/dist/src/transport/index.js.map +1 -1
  22. package/dist/src/transport/utils.js +2 -2
  23. package/dist/src/transport/utils.js.map +1 -1
  24. package/package.json +23 -51
  25. package/src/connection-encryption/index.ts +27 -20
  26. package/src/stream-muxer/base-test.ts +75 -409
  27. package/src/stream-muxer/close-test.ts +304 -327
  28. package/src/stream-muxer/index.ts +2 -2
  29. package/src/stream-muxer/stream-test.ts +380 -0
  30. package/src/stream-muxer/stress-test.ts +92 -18
  31. package/src/transport/index.ts +280 -241
  32. package/src/transport/utils.ts +2 -2
  33. package/dist/src/connection-encryption/utils/index.d.ts +0 -3
  34. package/dist/src/connection-encryption/utils/index.d.ts.map +0 -1
  35. package/dist/src/connection-encryption/utils/index.js +0 -21
  36. package/dist/src/connection-encryption/utils/index.js.map +0 -1
  37. package/dist/src/matchers.d.ts +0 -12
  38. package/dist/src/matchers.d.ts.map +0 -1
  39. package/dist/src/matchers.js +0 -14
  40. package/dist/src/matchers.js.map +0 -1
  41. package/dist/src/mocks/connection-manager.d.ts +0 -27
  42. package/dist/src/mocks/connection-manager.d.ts.map +0 -1
  43. package/dist/src/mocks/connection-manager.js +0 -147
  44. package/dist/src/mocks/connection-manager.js.map +0 -1
  45. package/dist/src/mocks/connection.d.ts +0 -41
  46. package/dist/src/mocks/connection.d.ts.map +0 -1
  47. package/dist/src/mocks/connection.js +0 -234
  48. package/dist/src/mocks/connection.js.map +0 -1
  49. package/dist/src/mocks/duplex.d.ts +0 -4
  50. package/dist/src/mocks/duplex.d.ts.map +0 -1
  51. package/dist/src/mocks/duplex.js +0 -9
  52. package/dist/src/mocks/duplex.js.map +0 -1
  53. package/dist/src/mocks/index.d.ts +0 -12
  54. package/dist/src/mocks/index.d.ts.map +0 -1
  55. package/dist/src/mocks/index.js +0 -8
  56. package/dist/src/mocks/index.js.map +0 -1
  57. package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
  58. package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
  59. package/dist/src/mocks/multiaddr-connection.js +0 -64
  60. package/dist/src/mocks/multiaddr-connection.js.map +0 -1
  61. package/dist/src/mocks/muxer.d.ts +0 -36
  62. package/dist/src/mocks/muxer.d.ts.map +0 -1
  63. package/dist/src/mocks/muxer.js +0 -234
  64. package/dist/src/mocks/muxer.js.map +0 -1
  65. package/dist/src/mocks/registrar.d.ts +0 -16
  66. package/dist/src/mocks/registrar.d.ts.map +0 -1
  67. package/dist/src/mocks/registrar.js +0 -66
  68. package/dist/src/mocks/registrar.js.map +0 -1
  69. package/dist/src/mocks/upgrader.d.ts +0 -9
  70. package/dist/src/mocks/upgrader.d.ts.map +0 -1
  71. package/dist/src/mocks/upgrader.js +0 -46
  72. package/dist/src/mocks/upgrader.js.map +0 -1
  73. package/dist/src/pubsub/api.d.ts +0 -6
  74. package/dist/src/pubsub/api.d.ts.map +0 -1
  75. package/dist/src/pubsub/api.js +0 -88
  76. package/dist/src/pubsub/api.js.map +0 -1
  77. package/dist/src/pubsub/connection-handlers.d.ts +0 -6
  78. package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
  79. package/dist/src/pubsub/connection-handlers.js +0 -329
  80. package/dist/src/pubsub/connection-handlers.js.map +0 -1
  81. package/dist/src/pubsub/emit-self.d.ts +0 -6
  82. package/dist/src/pubsub/emit-self.d.ts.map +0 -1
  83. package/dist/src/pubsub/emit-self.js +0 -80
  84. package/dist/src/pubsub/emit-self.js.map +0 -1
  85. package/dist/src/pubsub/index.d.ts +0 -18
  86. package/dist/src/pubsub/index.d.ts.map +0 -1
  87. package/dist/src/pubsub/index.js +0 -17
  88. package/dist/src/pubsub/index.js.map +0 -1
  89. package/dist/src/pubsub/messages.d.ts +0 -6
  90. package/dist/src/pubsub/messages.d.ts.map +0 -1
  91. package/dist/src/pubsub/messages.js +0 -48
  92. package/dist/src/pubsub/messages.js.map +0 -1
  93. package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
  94. package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
  95. package/dist/src/pubsub/multiple-nodes.js +0 -350
  96. package/dist/src/pubsub/multiple-nodes.js.map +0 -1
  97. package/dist/src/pubsub/two-nodes.d.ts +0 -6
  98. package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
  99. package/dist/src/pubsub/two-nodes.js +0 -216
  100. package/dist/src/pubsub/two-nodes.js.map +0 -1
  101. package/dist/src/pubsub/utils.d.ts +0 -5
  102. package/dist/src/pubsub/utils.d.ts.map +0 -1
  103. package/dist/src/pubsub/utils.js +0 -27
  104. package/dist/src/pubsub/utils.js.map +0 -1
  105. package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
  106. package/dist/src/stream-muxer/mega-stress-test.js +0 -11
  107. package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
  108. package/dist/src/stream-muxer/spawner.d.ts +0 -4
  109. package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
  110. package/dist/src/stream-muxer/spawner.js +0 -37
  111. package/dist/src/stream-muxer/spawner.js.map +0 -1
  112. package/dist/typedoc-urls.json +0 -44
  113. package/src/connection-encryption/utils/index.ts +0 -27
  114. package/src/matchers.ts +0 -18
  115. package/src/mocks/connection-manager.ts +0 -216
  116. package/src/mocks/connection.ts +0 -307
  117. package/src/mocks/duplex.ts +0 -11
  118. package/src/mocks/index.ts +0 -11
  119. package/src/mocks/multiaddr-connection.ts +0 -80
  120. package/src/mocks/muxer.ts +0 -331
  121. package/src/mocks/registrar.ts +0 -86
  122. package/src/mocks/upgrader.ts +0 -65
  123. package/src/pubsub/api.ts +0 -116
  124. package/src/pubsub/connection-handlers.ts +0 -413
  125. package/src/pubsub/emit-self.ts +0 -99
  126. package/src/pubsub/index.ts +0 -34
  127. package/src/pubsub/messages.ts +0 -59
  128. package/src/pubsub/multiple-nodes.ts +0 -440
  129. package/src/pubsub/two-nodes.ts +0 -272
  130. package/src/pubsub/utils.ts +0 -34
  131. package/src/stream-muxer/mega-stress-test.ts +0 -14
  132. package/src/stream-muxer/spawner.ts +0 -55
@@ -1,440 +0,0 @@
1
- /* eslint max-nested-callbacks: ["error", 6] */
2
- import { start, stop } from '@libp2p/interface'
3
- import { expect } from 'aegir/chai'
4
- import delay from 'delay'
5
- import pDefer from 'p-defer'
6
- import pWaitFor from 'p-wait-for'
7
- import sinon from 'sinon'
8
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10
- import { mockNetwork } from '../mocks/index.js'
11
- import { createComponents, waitForSubscriptionUpdate } from './utils.js'
12
- import type { PubSubArgs, PubSubComponents } from './index.js'
13
- import type { TestSetup } from '../index.js'
14
- import type { Message, PubSub } from '@libp2p/interface'
15
-
16
- export default (common: TestSetup<PubSub, PubSubArgs>): void => {
17
- describe('pubsub with multiple nodes', function () {
18
- describe('every peer subscribes to the topic', () => {
19
- describe('line', () => {
20
- // line
21
- // ◉────◉────◉
22
- // a b c
23
- let psA: PubSub
24
- let psB: PubSub
25
- let psC: PubSub
26
- let componentsA: PubSubComponents
27
- let componentsB: PubSubComponents
28
- let componentsC: PubSubComponents
29
-
30
- // Create and start pubsub nodes
31
- beforeEach(async () => {
32
- mockNetwork.reset()
33
-
34
- componentsA = await createComponents()
35
- componentsB = await createComponents()
36
- componentsC = await createComponents()
37
-
38
- psA = componentsA.pubsub = await common.setup({
39
- components: componentsA,
40
- init: {
41
- emitSelf: true
42
- }
43
- })
44
- psB = componentsB.pubsub = await common.setup({
45
- components: componentsB,
46
- init: {
47
- emitSelf: true
48
- }
49
- })
50
- psC = componentsC.pubsub = await common.setup({
51
- components: componentsC,
52
- init: {
53
- emitSelf: true
54
- }
55
- })
56
-
57
- // Start pubsub modes
58
- await start(...Object.values(componentsA), ...Object.values(componentsB), ...Object.values(componentsC))
59
-
60
- // Connect nodes
61
- await componentsA.connectionManager.openConnection(componentsB.peerId)
62
- await componentsB.connectionManager.openConnection(componentsC.peerId)
63
-
64
- // Wait for peers to be ready in pubsub
65
- await pWaitFor(() =>
66
- psA.getPeers().length === 1 &&
67
- psC.getPeers().length === 1 &&
68
- psA.getPeers().length === 1
69
- )
70
- })
71
-
72
- afterEach(async () => {
73
- sinon.restore()
74
- await stop(...Object.values(componentsA), ...Object.values(componentsB), ...Object.values(componentsC))
75
- await common.teardown()
76
- mockNetwork.reset()
77
- })
78
-
79
- it('subscribe to the topic on node a', async () => {
80
- const topic = 'Z'
81
-
82
- psA.subscribe(topic)
83
- expect(psA.getTopics()).to.deep.equal([topic])
84
-
85
- await waitForSubscriptionUpdate(psB, componentsA.peerId)
86
-
87
- expect(psB.getPeers().length).to.equal(2)
88
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.peerId.toString()])
89
-
90
- expect(psC.getPeers().length).to.equal(1)
91
- expect(psC.getSubscribers(topic)).to.be.empty()
92
- })
93
-
94
- it('subscribe to the topic on node b', async () => {
95
- const topic = 'Z'
96
- psB.subscribe(topic)
97
- expect(psB.getTopics()).to.deep.equal([topic])
98
-
99
- await Promise.all([
100
- waitForSubscriptionUpdate(psA, componentsB.peerId),
101
- waitForSubscriptionUpdate(psC, componentsB.peerId)
102
- ])
103
-
104
- expect(psA.getPeers().length).to.equal(1)
105
- expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.peerId.toString()])
106
-
107
- expect(psC.getPeers().length).to.equal(1)
108
- expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsB.peerId.toString()])
109
- })
110
-
111
- it('subscribe to the topic on node c', async () => {
112
- const topic = 'Z'
113
- const defer = pDefer()
114
-
115
- psC.subscribe(topic)
116
- expect(psC.getTopics()).to.deep.equal([topic])
117
-
118
- psB.addEventListener('subscription-change', () => {
119
- expect(psA.getPeers().length).to.equal(1)
120
- expect(psB.getPeers().length).to.equal(2)
121
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsC.peerId.toString()])
122
-
123
- defer.resolve()
124
- }, {
125
- once: true
126
- })
127
-
128
- return defer.promise
129
- })
130
-
131
- it('publish on node a', async () => {
132
- const topic = 'Z'
133
- const defer = pDefer()
134
-
135
- psA.subscribe(topic)
136
- psB.subscribe(topic)
137
- psC.subscribe(topic)
138
-
139
- await Promise.all([
140
- waitForSubscriptionUpdate(psA, componentsB.peerId),
141
- waitForSubscriptionUpdate(psB, componentsA.peerId),
142
- waitForSubscriptionUpdate(psC, componentsB.peerId)
143
- ])
144
-
145
- // GossipSub needs time to build the mesh overlay
146
- await delay(1000)
147
-
148
- let counter = 0
149
-
150
- psA.addEventListener('message', incMsg)
151
- psB.addEventListener('message', incMsg)
152
- psC.addEventListener('message', incMsg)
153
-
154
- const result = await psA.publish(topic, uint8ArrayFromString('hey'))
155
-
156
- expect(result).to.have.property('recipients').with.property('length').greaterThanOrEqual(1)
157
-
158
- function incMsg (evt: CustomEvent<Message>): void {
159
- const msg = evt.detail
160
-
161
- if (msg.topic !== topic) {
162
- return
163
- }
164
-
165
- expect(uint8ArrayToString(msg.data)).to.equal('hey')
166
- check()
167
- }
168
-
169
- function check (): void {
170
- if (++counter === 3) {
171
- psA.removeEventListener('message', incMsg)
172
- psB.removeEventListener('message', incMsg)
173
- psC.removeEventListener('message', incMsg)
174
- defer.resolve()
175
- }
176
- }
177
-
178
- return defer.promise
179
- })
180
-
181
- // since the topology is the same, just the publish
182
- // gets sent by other peer, we reused the same peers
183
- describe('1 level tree', () => {
184
- // 1 level tree
185
- // ┌◉┐
186
- // │b│
187
- // ◉─┘ └─◉
188
- // a c
189
-
190
- it('publish on node b', async () => {
191
- const topic = 'Z'
192
- const defer = pDefer()
193
- let counter = 0
194
-
195
- psA.subscribe(topic)
196
- psB.subscribe(topic)
197
- psC.subscribe(topic)
198
-
199
- await Promise.all([
200
- waitForSubscriptionUpdate(psA, componentsB.peerId),
201
- waitForSubscriptionUpdate(psB, componentsA.peerId),
202
- waitForSubscriptionUpdate(psC, componentsB.peerId)
203
- ])
204
-
205
- // GossipSub needs time to build the mesh overlay
206
- await delay(1000)
207
-
208
- psA.addEventListener('message', incMsg)
209
- psB.addEventListener('message', incMsg)
210
- psC.addEventListener('message', incMsg)
211
-
212
- await psB.publish(topic, uint8ArrayFromString('hey'))
213
-
214
- function incMsg (evt: CustomEvent<Message>): void {
215
- const msg = evt.detail
216
-
217
- if (msg.topic !== topic) {
218
- return
219
- }
220
-
221
- expect(uint8ArrayToString(msg.data)).to.equal('hey')
222
- check()
223
- }
224
-
225
- function check (): void {
226
- if (++counter === 3) {
227
- psA.removeEventListener('message', incMsg)
228
- psB.removeEventListener('message', incMsg)
229
- psC.removeEventListener('message', incMsg)
230
- defer.resolve()
231
- }
232
- }
233
-
234
- return defer.promise
235
- })
236
- })
237
- })
238
-
239
- describe('2 level tree', () => {
240
- // 2 levels tree
241
- // ┌◉┐
242
- // │c│
243
- // ┌◉─┘ └─◉┐
244
- // │b d│
245
- // ◉─┘ └─◉
246
- // a
247
- let psA: PubSub
248
- let psB: PubSub
249
- let psC: PubSub
250
- let psD: PubSub
251
- let psE: PubSub
252
- let componentsA: PubSubComponents
253
- let componentsB: PubSubComponents
254
- let componentsC: PubSubComponents
255
- let componentsD: PubSubComponents
256
- let componentsE: PubSubComponents
257
-
258
- // Create and start pubsub nodes
259
- beforeEach(async () => {
260
- mockNetwork.reset()
261
-
262
- componentsA = await createComponents()
263
- componentsB = await createComponents()
264
- componentsC = await createComponents()
265
- componentsD = await createComponents()
266
- componentsE = await createComponents()
267
-
268
- psA = componentsA.pubsub = await common.setup({
269
- components: componentsA,
270
- init: {
271
- emitSelf: true
272
- }
273
- })
274
- psB = componentsB.pubsub = await common.setup({
275
- components: componentsB,
276
- init: {
277
- emitSelf: true
278
- }
279
- })
280
- psC = componentsC.pubsub = await common.setup({
281
- components: componentsC,
282
- init: {
283
- emitSelf: true
284
- }
285
- })
286
- psD = componentsD.pubsub = await common.setup({
287
- components: componentsD,
288
- init: {
289
- emitSelf: true
290
- }
291
- })
292
- psE = componentsE.pubsub = await common.setup({
293
- components: componentsE,
294
- init: {
295
- emitSelf: true
296
- }
297
- })
298
-
299
- // Start pubsub nodes
300
- await start(
301
- ...Object.values(componentsA),
302
- ...Object.values(componentsB),
303
- ...Object.values(componentsC),
304
- ...Object.values(componentsD),
305
- ...Object.values(componentsE)
306
- )
307
-
308
- // connect nodes
309
- await componentsA.connectionManager.openConnection(componentsB.peerId)
310
- await componentsB.connectionManager.openConnection(componentsC.peerId)
311
- await componentsC.connectionManager.openConnection(componentsD.peerId)
312
- await componentsD.connectionManager.openConnection(componentsE.peerId)
313
-
314
- // Wait for peers to be ready in pubsub
315
- await pWaitFor(() =>
316
- psA.getPeers().length === 1 &&
317
- psB.getPeers().length === 2 &&
318
- psC.getPeers().length === 2 &&
319
- psD.getPeers().length === 2 &&
320
- psE.getPeers().length === 1
321
- )
322
- })
323
-
324
- afterEach(async () => {
325
- await stop(
326
- ...Object.values(componentsA),
327
- ...Object.values(componentsB),
328
- ...Object.values(componentsC),
329
- ...Object.values(componentsD),
330
- ...Object.values(componentsE)
331
- )
332
- await common.teardown()
333
- mockNetwork.reset()
334
- })
335
-
336
- it('subscribes', () => {
337
- psA.subscribe('Z')
338
- expect(psA.getTopics()).to.deep.equal(['Z'])
339
- psB.subscribe('Z')
340
- expect(psB.getTopics()).to.deep.equal(['Z'])
341
- psC.subscribe('Z')
342
- expect(psC.getTopics()).to.deep.equal(['Z'])
343
- psD.subscribe('Z')
344
- expect(psD.getTopics()).to.deep.equal(['Z'])
345
- psE.subscribe('Z')
346
- expect(psE.getTopics()).to.deep.equal(['Z'])
347
- })
348
-
349
- it('publishes from c', async function () {
350
- const defer = pDefer()
351
- let counter = 0
352
- const topic = 'Z'
353
-
354
- psA.subscribe(topic)
355
- psA.addEventListener('message', incMsg)
356
- psB.subscribe(topic)
357
- psB.addEventListener('message', incMsg)
358
- psC.subscribe(topic)
359
- psC.addEventListener('message', incMsg)
360
- psD.subscribe(topic)
361
- psD.addEventListener('message', incMsg)
362
- psE.subscribe(topic)
363
- psE.addEventListener('message', incMsg)
364
-
365
- await Promise.all([
366
- waitForSubscriptionUpdate(psA, componentsB.peerId),
367
- waitForSubscriptionUpdate(psB, componentsA.peerId),
368
- waitForSubscriptionUpdate(psC, componentsB.peerId),
369
- waitForSubscriptionUpdate(psD, componentsC.peerId),
370
- waitForSubscriptionUpdate(psE, componentsD.peerId)
371
- ])
372
-
373
- // GossipSub needs time to build the mesh overlay
374
- await delay(1000)
375
-
376
- await psC.publish('Z', uint8ArrayFromString('hey from c'))
377
-
378
- function incMsg (evt: CustomEvent<Message>): void {
379
- const msg = evt.detail
380
-
381
- if (msg.topic !== topic) {
382
- return
383
- }
384
-
385
- expect(uint8ArrayToString(msg.data)).to.equal('hey from c')
386
- check()
387
- }
388
-
389
- function check (): void {
390
- if (++counter === 5) {
391
- psA.unsubscribe('Z')
392
- psB.unsubscribe('Z')
393
- psC.unsubscribe('Z')
394
- psD.unsubscribe('Z')
395
- psE.unsubscribe('Z')
396
- defer.resolve()
397
- }
398
- }
399
-
400
- return defer.promise
401
- })
402
- })
403
- })
404
-
405
- describe('only some nodes subscribe the networks', () => {
406
- describe('line', () => {
407
- // line
408
- // ◉────◎────◉
409
- // a b c
410
-
411
- before(() => { })
412
- after(() => { })
413
- })
414
-
415
- describe('1 level tree', () => {
416
- // 1 level tree
417
- // ┌◉┐
418
- // │b│
419
- // ◎─┘ └─◉
420
- // a c
421
-
422
- before(() => { })
423
- after(() => { })
424
- })
425
-
426
- describe('2 level tree', () => {
427
- // 2 levels tree
428
- // ┌◉┐
429
- // │c│
430
- // ┌◎─┘ └─◉┐
431
- // │b d│
432
- // ◉─┘ └─◎
433
- // a e
434
-
435
- before(() => { })
436
- after(() => { })
437
- })
438
- })
439
- })
440
- }
@@ -1,272 +0,0 @@
1
- /* eslint max-nested-callbacks: ["error", 6] */
2
- import { TopicValidatorResult, start, stop } from '@libp2p/interface'
3
- import { expect } from 'aegir/chai'
4
- import pDefer from 'p-defer'
5
- import pWaitFor from 'p-wait-for'
6
- import sinon from 'sinon'
7
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
9
- import { mockNetwork } from '../mocks/index.js'
10
- import { createComponents, waitForSubscriptionUpdate } from './utils.js'
11
- import type { PubSubArgs, PubSubComponents } from './index.js'
12
- import type { TestSetup } from '../index.js'
13
- import type { Message, PubSub } from '@libp2p/interface'
14
-
15
- const topic = 'foo'
16
-
17
- function shouldNotHappen (): void {
18
- expect.fail()
19
- }
20
-
21
- export default (common: TestSetup<PubSub, PubSubArgs>): void => {
22
- describe('pubsub with two nodes', () => {
23
- let psA: PubSub
24
- let psB: PubSub
25
- let componentsA: PubSubComponents
26
- let componentsB: PubSubComponents
27
-
28
- // Create pubsub nodes and connect them
29
- beforeEach(async () => {
30
- mockNetwork.reset()
31
-
32
- componentsA = await createComponents()
33
- componentsB = await createComponents()
34
-
35
- psA = componentsA.pubsub = await common.setup({
36
- components: componentsA,
37
- init: {
38
- emitSelf: true
39
- }
40
- })
41
- psB = componentsB.pubsub = await common.setup({
42
- components: componentsB,
43
- init: {
44
- emitSelf: false
45
- }
46
- })
47
-
48
- // Start pubsub and connect nodes
49
- await start(...Object.values(componentsA), ...Object.values(componentsB))
50
-
51
- expect(psA.getPeers()).to.be.empty()
52
- expect(psB.getPeers()).to.be.empty()
53
-
54
- await componentsA.connectionManager.openConnection(componentsB.peerId)
55
-
56
- // Wait for peers to be ready in pubsub
57
- await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
58
- })
59
-
60
- afterEach(async () => {
61
- sinon.restore()
62
- await stop(...Object.values(componentsA), ...Object.values(componentsB))
63
- await common.teardown()
64
- mockNetwork.reset()
65
- })
66
-
67
- it('Subscribe to a topic in nodeA', async () => {
68
- const defer = pDefer()
69
-
70
- psB.addEventListener('subscription-change', (evt) => {
71
- const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
72
- expect(psA.getTopics()).to.deep.equal([topic])
73
- expect(psB.getPeers()).to.have.lengthOf(1)
74
- expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.peerId.toString()])
75
- expect(changedPeerId.toString()).to.equal(psB.getPeers()[0].toString())
76
- expect(changedSubs).to.have.lengthOf(1)
77
- expect(changedSubs[0].topic).to.equal(topic)
78
- expect(changedSubs[0].subscribe).to.equal(true)
79
- defer.resolve()
80
- }, {
81
- once: true
82
- })
83
- psA.subscribe(topic)
84
-
85
- return defer.promise
86
- })
87
-
88
- it('Publish to a topic in nodeA', async () => {
89
- const defer = pDefer()
90
-
91
- psA.addEventListener('message', (evt) => {
92
- if (evt.detail.topic === topic) {
93
- const msg = evt.detail
94
- expect(uint8ArrayToString(msg.data)).to.equal('hey')
95
- psB.removeEventListener('message', shouldNotHappen)
96
- defer.resolve()
97
- }
98
- }, {
99
- once: true
100
- })
101
-
102
- psA.subscribe(topic)
103
- psB.subscribe(topic)
104
-
105
- await Promise.all([
106
- waitForSubscriptionUpdate(psA, componentsB.peerId),
107
- waitForSubscriptionUpdate(psB, componentsA.peerId)
108
- ])
109
-
110
- await psA.publish(topic, uint8ArrayFromString('hey'))
111
-
112
- return defer.promise
113
- })
114
-
115
- it('Publish to a topic in nodeB', async () => {
116
- const defer = pDefer()
117
-
118
- psA.addEventListener('message', (evt) => {
119
- if (evt.detail.topic !== topic) {
120
- return
121
- }
122
-
123
- const msg = evt.detail
124
- psA.addEventListener('message', (evt) => {
125
- if (evt.detail.topic === topic) {
126
- shouldNotHappen()
127
- }
128
- }, {
129
- once: true
130
- })
131
- expect(uint8ArrayToString(msg.data)).to.equal('banana')
132
-
133
- setTimeout(() => {
134
- psA.removeEventListener('message')
135
- psB.removeEventListener('message')
136
-
137
- defer.resolve()
138
- }, 100)
139
- }, {
140
- once: true
141
- })
142
-
143
- psB.addEventListener('message', shouldNotHappen)
144
-
145
- psA.subscribe(topic)
146
- psB.subscribe(topic)
147
-
148
- await Promise.all([
149
- waitForSubscriptionUpdate(psA, componentsB.peerId),
150
- waitForSubscriptionUpdate(psB, componentsA.peerId)
151
- ])
152
-
153
- await psB.publish(topic, uint8ArrayFromString('banana'))
154
-
155
- return defer.promise
156
- })
157
-
158
- it('validate topic message', async () => {
159
- const defer = pDefer()
160
-
161
- psA.subscribe(topic)
162
-
163
- psB.topicValidators.set(topic, (peer, message) => {
164
- if (!peer.equals(componentsA.peerId)) {
165
- defer.reject(new Error('Invalid peer id in topic validator fn'))
166
- return TopicValidatorResult.Reject
167
- }
168
-
169
- if (uint8ArrayToString(message.data) !== 'hey') {
170
- defer.reject(new Error('Invalid message in topic validator fn'))
171
- return TopicValidatorResult.Reject
172
- }
173
-
174
- defer.resolve()
175
- return TopicValidatorResult.Accept
176
- })
177
- psB.subscribe(topic)
178
-
179
- await Promise.all([
180
- waitForSubscriptionUpdate(psA, componentsB.peerId),
181
- waitForSubscriptionUpdate(psB, componentsA.peerId)
182
- ])
183
-
184
- await psA.publish(topic, uint8ArrayFromString('hey'))
185
-
186
- return defer.promise
187
- })
188
-
189
- it('Publish 10 msg to a topic in nodeB', async () => {
190
- const defer = pDefer()
191
- let counter = 0
192
-
193
- psB.addEventListener('message', shouldNotHappen)
194
- psA.addEventListener('message', receivedMsg)
195
-
196
- function receivedMsg (evt: CustomEvent<Message>): void {
197
- const msg = evt.detail
198
- if (msg.type === 'unsigned') {
199
- expect(uint8ArrayToString(msg.data)).to.equal('banana')
200
- expect(msg.topic).to.be.equal(topic)
201
- } else {
202
- expect(uint8ArrayToString(msg.data)).to.equal('banana')
203
- expect(msg.from.toString()).to.equal(componentsB.peerId.toString())
204
- expect(msg.sequenceNumber).to.be.a('BigInt')
205
- expect(msg.topic).to.be.equal(topic)
206
- }
207
-
208
- if (++counter === 10) {
209
- psA.removeEventListener('message', receivedMsg)
210
- psB.removeEventListener('message', shouldNotHappen)
211
-
212
- defer.resolve()
213
- }
214
- }
215
-
216
- psA.subscribe(topic)
217
- psB.subscribe(topic)
218
-
219
- await Promise.all([
220
- waitForSubscriptionUpdate(psA, componentsB.peerId),
221
- waitForSubscriptionUpdate(psB, componentsA.peerId)
222
- ])
223
-
224
- await Promise.all(
225
- Array.from({ length: 10 }, async (_, i) => {
226
- await psB.publish(topic, uint8ArrayFromString('banana'))
227
- })
228
- )
229
-
230
- return defer.promise
231
- })
232
-
233
- it('Unsubscribe from topic in nodeA', async () => {
234
- const defer = pDefer()
235
- let callCount = 0
236
-
237
- psB.addEventListener('subscription-change', (evt) => {
238
- callCount++
239
-
240
- if (callCount === 1) {
241
- // notice subscribe
242
- const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
243
- expect(psB.getPeers()).to.have.lengthOf(1)
244
- expect(psB.getTopics()).to.be.empty()
245
- expect(changedPeerId.toString()).to.equal(psB.getPeers()[0].toString())
246
- expect(changedSubs).to.have.lengthOf(1)
247
- expect(changedSubs[0].topic).to.equal(topic)
248
- expect(changedSubs[0].subscribe).to.equal(true)
249
- } else {
250
- // notice unsubscribe
251
- const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
252
- expect(psB.getPeers()).to.have.lengthOf(1)
253
- expect(psB.getTopics()).to.be.empty()
254
- expect(changedPeerId.toString()).to.equal(psB.getPeers()[0].toString())
255
- expect(changedSubs).to.have.lengthOf(1)
256
- expect(changedSubs[0].topic).to.equal(topic)
257
- expect(changedSubs[0].subscribe).to.equal(false)
258
-
259
- defer.resolve()
260
- }
261
- })
262
-
263
- psA.subscribe(topic)
264
- expect(psA.getTopics()).to.not.be.empty()
265
-
266
- psA.unsubscribe(topic)
267
- expect(psA.getTopics()).to.be.empty()
268
-
269
- return defer.promise
270
- })
271
- })
272
- }