@libp2p/interface-compliance-tests 2.0.1 → 3.0.0

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 +19 -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 -88
  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 -277
  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 -204
  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 -393
  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,413 +0,0 @@
1
- import { expect } from 'aegir/chai'
2
- import sinon from 'sinon'
3
- import pDefer from 'p-defer'
4
- import pWaitFor from 'p-wait-for'
5
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
- import type { TestSetup } from '../index.js'
8
- import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
9
- import type { PubSubArgs } from './index.js'
10
- import type { Components } from '@libp2p/interfaces/components'
11
- import { start, stop } from '@libp2p/interfaces/startable'
12
- import { createComponents } from './utils.js'
13
- import { pEvent } from 'p-event'
14
- import { mockNetwork } from '../mocks/connection-manager.js'
15
-
16
- export default (common: TestSetup<PubSub, PubSubArgs>) => {
17
- describe('pubsub connection handlers', () => {
18
- let psA: PubSub
19
- let psB: PubSub
20
- let componentsA: Components
21
- let componentsB: Components
22
-
23
- describe('nodes send state on connection', () => {
24
- // Create pubsub nodes and connect them
25
- beforeEach(async () => {
26
- mockNetwork.reset()
27
-
28
- componentsA = await createComponents()
29
- componentsB = await createComponents()
30
-
31
- psA = componentsA.setPubSub(await common.setup({
32
- components: componentsA,
33
- init: {}
34
- }))
35
-
36
- psB = componentsB.setPubSub(await common.setup({
37
- components: componentsB,
38
- init: {}
39
- }))
40
-
41
- // Start pubsub
42
- await start(componentsA, componentsB)
43
-
44
- expect(psA.getPeers()).to.be.empty()
45
- expect(psB.getPeers()).to.be.empty()
46
-
47
- // Make subscriptions prior to nodes connected
48
- psA.subscribe('Za')
49
- psB.subscribe('Zb')
50
-
51
- expect(psA.getPeers()).to.be.empty()
52
- expect(psA.getTopics()).to.deep.equal(['Za'])
53
- expect(psB.getPeers()).to.be.empty()
54
- expect(psB.getTopics()).to.deep.equal(['Zb'])
55
- })
56
-
57
- afterEach(async () => {
58
- sinon.restore()
59
- await stop(componentsA, componentsB)
60
- await common.teardown()
61
- mockNetwork.reset()
62
- })
63
-
64
- it('existing subscriptions are sent upon peer connection', async function () {
65
- const subscriptionsChanged = Promise.all([
66
- pEvent(psA, 'subscription-change'),
67
- pEvent(psB, 'subscription-change')
68
- ])
69
-
70
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
71
-
72
- await subscriptionsChanged
73
-
74
- expect(psA.getPeers()).to.have.lengthOf(1)
75
- expect(psB.getPeers()).to.have.lengthOf(1)
76
-
77
- expect(psA.getTopics()).to.deep.equal(['Za'])
78
- expect(psB.getTopics()).to.deep.equal(['Zb'])
79
-
80
- expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
81
- expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
82
- })
83
- })
84
-
85
- describe('pubsub started before connect', () => {
86
- let psA: PubSub
87
- let psB: PubSub
88
- let componentsA: Components
89
- let componentsB: Components
90
-
91
- // Create pubsub nodes and start them
92
- beforeEach(async () => {
93
- mockNetwork.reset()
94
- componentsA = await createComponents()
95
- componentsB = await createComponents()
96
-
97
- psA = componentsA.setPubSub(await common.setup({
98
- components: componentsA,
99
- init: {}
100
- }))
101
- psB = componentsB.setPubSub(await common.setup({
102
- components: componentsB,
103
- init: {}
104
- }))
105
-
106
- await start(componentsA, componentsB)
107
- })
108
-
109
- afterEach(async () => {
110
- sinon.restore()
111
- await stop(componentsA, componentsB)
112
- await common.teardown()
113
- mockNetwork.reset()
114
- })
115
-
116
- it('should get notified of connected peers on dial', async () => {
117
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
118
-
119
- return await Promise.all([
120
- pWaitFor(() => psA.getPeers().length === 1),
121
- pWaitFor(() => psB.getPeers().length === 1)
122
- ])
123
- })
124
-
125
- it('should receive pubsub messages', async () => {
126
- const defer = pDefer()
127
- const topic = 'test-topic'
128
- const data = uint8ArrayFromString('hey!')
129
-
130
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
131
-
132
- let subscribedTopics = psA.getTopics()
133
- expect(subscribedTopics).to.not.include(topic)
134
-
135
- psA.subscribe(topic)
136
- psA.addEventListener('message', (evt) => {
137
- if (evt.detail.topic === topic) {
138
- const msg = evt.detail
139
- expect(msg.data).to.equalBytes(data)
140
- defer.resolve()
141
- }
142
- })
143
- psA.subscribe(topic)
144
-
145
- subscribedTopics = psA.getTopics()
146
- expect(subscribedTopics).to.include(topic)
147
-
148
- // wait for psB to know about psA subscription
149
- await pWaitFor(() => {
150
- const subscribedPeers = psB.getSubscribers(topic)
151
- return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
152
- })
153
- await psB.publish(topic, data)
154
-
155
- await defer.promise
156
- })
157
- })
158
-
159
- describe('pubsub started after connect', () => {
160
- let psA: PubSub
161
- let psB: PubSub
162
- let componentsA: Components
163
- let componentsB: Components
164
-
165
- // Create pubsub nodes
166
- beforeEach(async () => {
167
- mockNetwork.reset()
168
- componentsA = await createComponents()
169
- componentsB = await createComponents()
170
-
171
- psA = componentsA.setPubSub(await common.setup({
172
- components: componentsA,
173
- init: {}
174
- }))
175
- psB = componentsB.setPubSub(await common.setup({
176
- components: componentsB,
177
- init: {}
178
- }))
179
- })
180
-
181
- afterEach(async () => {
182
- sinon.restore()
183
- await stop(componentsA, componentsB)
184
- await common.teardown()
185
- mockNetwork.reset()
186
- })
187
-
188
- it('should get notified of connected peers after starting', async () => {
189
- await start(componentsA, componentsB)
190
-
191
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
192
-
193
- return await Promise.all([
194
- pWaitFor(() => psA.getPeers().length === 1),
195
- pWaitFor(() => psB.getPeers().length === 1)
196
- ])
197
- })
198
-
199
- it('should receive pubsub messages', async () => {
200
- const defer = pDefer()
201
- const topic = 'test-topic'
202
- const data = uint8ArrayFromString('hey!')
203
-
204
- await start(componentsA, componentsB)
205
-
206
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
207
-
208
- await Promise.all([
209
- pWaitFor(() => psA.getPeers().length === 1),
210
- pWaitFor(() => psB.getPeers().length === 1)
211
- ])
212
-
213
- let subscribedTopics = psA.getTopics()
214
- expect(subscribedTopics).to.not.include(topic)
215
-
216
- psA.subscribe(topic)
217
- psA.addEventListener('message', (evt) => {
218
- if (evt.detail.topic === topic) {
219
- const msg = evt.detail
220
- expect(msg.data).to.equalBytes(data)
221
- defer.resolve()
222
- }
223
- })
224
- psA.subscribe(topic)
225
-
226
- subscribedTopics = psA.getTopics()
227
- expect(subscribedTopics).to.include(topic)
228
-
229
- // wait for psB to know about psA subscription
230
- await pWaitFor(() => {
231
- const subscribedPeers = psB.getSubscribers(topic)
232
- return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
233
- })
234
- await psB.publish(topic, data)
235
-
236
- await defer.promise
237
- })
238
- })
239
-
240
- describe('pubsub with intermittent connections', () => {
241
- let psA: PubSub
242
- let psB: PubSub
243
- let componentsA: Components
244
- let componentsB: Components
245
-
246
- // Create pubsub nodes and start them
247
- beforeEach(async () => {
248
- mockNetwork.reset()
249
- componentsA = await createComponents()
250
- componentsB = await createComponents()
251
-
252
- psA = componentsA.setPubSub(await common.setup({
253
- components: componentsA,
254
- init: {}
255
- }))
256
- psB = componentsB.setPubSub(await common.setup({
257
- components: componentsB,
258
- init: {}
259
- }))
260
-
261
- await start(componentsA, componentsB)
262
- })
263
-
264
- afterEach(async () => {
265
- sinon.restore()
266
- await stop(componentsA, componentsB)
267
- await common.teardown()
268
- mockNetwork.reset()
269
- })
270
-
271
- it.skip('should receive pubsub messages after a node restart', async function () {
272
- const topic = 'test-topic'
273
- const data = uint8ArrayFromString('hey!')
274
-
275
- let counter = 0
276
- const defer1 = pDefer()
277
- const defer2 = pDefer()
278
-
279
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
280
-
281
- let subscribedTopics = psA.getTopics()
282
- expect(subscribedTopics).to.not.include(topic)
283
-
284
- psA.subscribe(topic)
285
- psA.addEventListener('message', (evt) => {
286
- if (evt.detail.topic === topic) {
287
- const msg = evt.detail
288
- expect(msg.data).to.equalBytes(data)
289
- counter++
290
- counter === 1 ? defer1.resolve() : defer2.resolve()
291
- }
292
- })
293
- psA.subscribe(topic)
294
-
295
- subscribedTopics = psA.getTopics()
296
- expect(subscribedTopics).to.include(topic)
297
-
298
- // wait for psB to know about psA subscription
299
- await pWaitFor(() => {
300
- const subscribedPeers = psB.getSubscribers(topic)
301
- return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
302
- })
303
- await psB.publish(topic, data)
304
-
305
- await defer1.promise
306
-
307
- await stop(psB)
308
- await pWaitFor(() => {
309
- // @ts-expect-error protected fields
310
- const aHasConnectionToB = psA._libp2p.connectionManager.get(psB.peerId)
311
- // @ts-expect-error protected fields
312
- const bHasConnectionToA = psB._libp2p.connectionManager.get(psA.peerId)
313
-
314
- return aHasConnectionToB != null && bHasConnectionToA != null
315
- })
316
- await start(psB)
317
-
318
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
319
-
320
- // wait for remoteLibp2p to know about libp2p subscription
321
- await pWaitFor(() => {
322
- const subscribedPeers = psB.getSubscribers(topic)
323
- return subscribedPeers.toString().includes(componentsA.getPeerId().toString())
324
- })
325
-
326
- await psB.publish(topic, data)
327
-
328
- await defer2.promise
329
- })
330
-
331
- it.skip('should handle quick reconnects with a delayed disconnect', async () => {
332
- // Subscribe on both
333
- let aReceivedFirstMessageFromB = false
334
- let aReceivedSecondMessageFromB = false
335
- let bReceivedFirstMessageFromA = false
336
- let bReceivedSecondMessageFromA = false
337
- const topic = 'reconnect-channel'
338
-
339
- const handlerSpyA = (evt: CustomEvent<Message>) => {
340
- if (evt.detail.topic !== topic) {
341
- return
342
- }
343
-
344
- const message = evt.detail
345
- const data = uint8ArrayToString(message.data)
346
-
347
- if (data === 'message-from-b-1') {
348
- aReceivedFirstMessageFromB = true
349
- }
350
-
351
- if (data === 'message-from-b-2') {
352
- aReceivedSecondMessageFromB = true
353
- }
354
- }
355
- const handlerSpyB = (evt: CustomEvent<Message>) => {
356
- if (evt.detail.topic !== topic) {
357
- return
358
- }
359
-
360
- const message = evt.detail
361
- const data = uint8ArrayToString(message.data)
362
-
363
- if (data === 'message-from-a-1') {
364
- bReceivedFirstMessageFromA = true
365
- }
366
-
367
- if (data === 'message-from-a-2') {
368
- bReceivedSecondMessageFromA = true
369
- }
370
- }
371
-
372
- psA.addEventListener('message', handlerSpyA)
373
- psB.addEventListener('message', handlerSpyB)
374
- psA.subscribe(topic)
375
- psB.subscribe(topic)
376
-
377
- // Create two connections to the remote peer
378
- // @ts-expect-error protected fields
379
- const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
380
-
381
- // second connection
382
- await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
383
-
384
- // Wait for subscriptions to occur
385
- await pWaitFor(() => {
386
- return psA.getSubscribers(topic).map(p => p.toString()).includes(componentsB.getPeerId().toString()) &&
387
- psB.getSubscribers(topic).map(p => p.toString()).includes(componentsA.getPeerId().toString())
388
- })
389
-
390
- // Verify messages go both ways
391
- await psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
392
- await psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
393
- await pWaitFor(() => {
394
- return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
395
- })
396
-
397
- // Disconnect the first connection (this acts as a delayed reconnect)
398
- // @ts-expect-error protected fields
399
- const psAConnUpdateSpy = sinon.spy(psA._libp2p.connectionManager.connections, 'set')
400
-
401
- await originalConnection.close()
402
- await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
403
-
404
- // Verify messages go both ways after the disconnect
405
- await psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
406
- await psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
407
- await pWaitFor(() => {
408
- return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
409
- })
410
- })
411
- })
412
- })
413
- }
@@ -1,105 +0,0 @@
1
- import { expect } from 'aegir/chai'
2
- import sinon from 'sinon'
3
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
- import { createEd25519PeerId } from '@libp2p/peer-id-factory'
5
- import { mockRegistrar } from '../mocks/registrar.js'
6
- import type { TestSetup } from '../index.js'
7
- import type { PubSubArgs } from './index.js'
8
- import { Components } from '@libp2p/interfaces/components'
9
- import { start, stop } from '@libp2p/interfaces/startable'
10
- import type { PubSub } from '@libp2p/interfaces/pubsub'
11
- import { createComponents } from './utils.js'
12
- import { mockNetwork } from '../mocks/connection-manager.js'
13
-
14
- const topic = 'foo'
15
- const data = uint8ArrayFromString('bar')
16
- const shouldNotHappen = () => expect.fail()
17
-
18
- export default (common: TestSetup<PubSub, PubSubArgs>) => {
19
- describe('emit self', () => {
20
- describe('enabled', () => {
21
- let pubsub: PubSub
22
- let components: Components
23
-
24
- before(async () => {
25
- mockNetwork.reset()
26
- components = await createComponents()
27
-
28
- pubsub = components.setPubSub(await common.setup({
29
- components,
30
- init: {
31
- emitSelf: true
32
- }
33
- }))
34
-
35
- await start(components)
36
- pubsub.subscribe(topic)
37
- })
38
-
39
- after(async () => {
40
- sinon.restore()
41
- await stop(components)
42
- await common.teardown()
43
- mockNetwork.reset()
44
- })
45
-
46
- it('should emit to self on publish', async () => {
47
- const promise = new Promise<void>((resolve) => {
48
- pubsub.addEventListener('message', (evt) => {
49
- if (evt.detail.topic === topic) {
50
- resolve()
51
- }
52
- }, {
53
- once: true
54
- })
55
- })
56
-
57
- const result = await pubsub.publish(topic, data)
58
-
59
- await promise
60
-
61
- expect(result).to.have.property('recipients').with.lengthOf(1)
62
- })
63
- })
64
-
65
- describe('disabled', () => {
66
- let pubsub: PubSub
67
- let components: Components
68
-
69
- before(async () => {
70
- mockNetwork.reset()
71
- components = new Components({
72
- peerId: await createEd25519PeerId(),
73
- registrar: mockRegistrar()
74
- })
75
- pubsub = components.setPubSub(await common.setup({
76
- components,
77
- init: {
78
- emitSelf: false
79
- }
80
- }))
81
-
82
- await start(components)
83
- pubsub.subscribe(topic)
84
- })
85
-
86
- after(async () => {
87
- sinon.restore()
88
- await stop(components)
89
- await common.teardown()
90
- mockNetwork.reset()
91
- })
92
-
93
- it('should not emit to self on publish', async () => {
94
- pubsub.addEventListener('message', shouldNotHappen, {
95
- once: true
96
- })
97
-
98
- await pubsub.publish(topic, data)
99
-
100
- // Wait 1 second to guarantee that self is not noticed
101
- return await new Promise((resolve) => setTimeout(resolve, 1000))
102
- })
103
- })
104
- })
105
- }
@@ -1,25 +0,0 @@
1
- import apiTest from './api.js'
2
- import emitSelfTest from './emit-self.js'
3
- import messagesTest from './messages.js'
4
- import connectionHandlersTest from './connection-handlers.js'
5
- import twoNodesTest from './two-nodes.js'
6
- import multipleNodesTest from './multiple-nodes.js'
7
- import type { TestSetup } from '../index.js'
8
- import type { PubSub, PubSubInit } from '@libp2p/interfaces/pubsub'
9
- import type { Components } from '@libp2p/interfaces/components'
10
-
11
- export interface PubSubArgs {
12
- components: Components
13
- init: PubSubInit
14
- }
15
-
16
- export default (common: TestSetup<PubSub, PubSubArgs>) => {
17
- describe('interface-pubsub compliance tests', () => {
18
- apiTest(common)
19
- emitSelfTest(common)
20
- messagesTest(common)
21
- connectionHandlersTest(common)
22
- twoNodesTest(common)
23
- multipleNodesTest(common)
24
- })
25
- }
@@ -1,58 +0,0 @@
1
- import { expect } from 'aegir/chai'
2
- import sinon from 'sinon'
3
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
- import type { TestSetup } from '../index.js'
5
- import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
6
- import type { PubSubArgs } from './index.js'
7
- import type { Components } from '@libp2p/interfaces/components'
8
- import { start, stop } from '@libp2p/interfaces/startable'
9
- import { pEvent } from 'p-event'
10
- import { createComponents } from './utils.js'
11
- import { mockNetwork } from '../mocks/connection-manager.js'
12
-
13
- const topic = 'foo'
14
- const data = uint8ArrayFromString('bar')
15
-
16
- export default (common: TestSetup<PubSub, PubSubArgs>) => {
17
- describe('messages', () => {
18
- let pubsub: PubSub
19
- let components: Components
20
-
21
- // Create pubsub router
22
- beforeEach(async () => {
23
- mockNetwork.reset()
24
- components = await createComponents()
25
-
26
- pubsub = components.setPubSub(await common.setup({
27
- components,
28
- init: {
29
- emitSelf: true
30
- }
31
- }))
32
- await start(components)
33
- })
34
-
35
- afterEach(async () => {
36
- sinon.restore()
37
- await stop(components)
38
- await common.teardown()
39
- mockNetwork.reset()
40
- })
41
-
42
- it('should emit normalized signed messages on publish', async () => {
43
- const eventPromise = pEvent<'message', CustomEvent<Message>>(pubsub, 'message')
44
-
45
- pubsub.globalSignaturePolicy = 'StrictSign'
46
- pubsub.subscribe(topic)
47
- await pubsub.publish(topic, data)
48
-
49
- const event = await eventPromise
50
- const message = event.detail
51
-
52
- expect(message.from.toString()).to.equal(components.getPeerId().toString())
53
- expect(message.sequenceNumber).to.not.eql(undefined)
54
- expect(message.key).to.not.eql(undefined)
55
- expect(message.signature).to.not.eql(undefined)
56
- })
57
- })
58
- }