@libp2p/interface-compliance-tests 2.0.2 → 3.0.1

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