@libp2p/interface-compliance-tests 1.1.24 → 1.1.27
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-manager.d.ts +0 -8
- package/dist/src/mocks/connection-manager.d.ts.map +1 -1
- package/dist/src/mocks/connection-manager.js +4 -16
- package/dist/src/mocks/connection-manager.js.map +1 -1
- package/dist/src/mocks/connection.d.ts +2 -1
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +4 -4
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +2 -5
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +4 -4
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +10 -15
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +58 -148
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +8 -7
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +9 -12
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +88 -217
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +20 -38
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +2 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +10 -0
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection-manager.ts +4 -20
- package/src/mocks/connection.ts +5 -5
- package/src/mocks/registrar.ts +6 -10
- package/src/pubsub/api.ts +11 -16
- package/src/pubsub/connection-handlers.ts +59 -159
- package/src/pubsub/emit-self.ts +10 -7
- package/src/pubsub/messages.ts +11 -13
- package/src/pubsub/multiple-nodes.ts +101 -223
- package/src/pubsub/two-nodes.ts +23 -41
- package/src/pubsub/utils.ts +11 -0
|
@@ -4,46 +4,34 @@ import pDefer from 'p-defer'
|
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
-
import {
|
|
8
|
-
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
7
|
+
import { connectPeers } from '../mocks/registrar.js'
|
|
9
8
|
import type { TestSetup } from '../index.js'
|
|
10
9
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
11
10
|
import type { PubSubArgs } from './index.js'
|
|
12
|
-
import type {
|
|
13
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
14
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
11
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
15
12
|
import { start, stop } from '../index.js'
|
|
13
|
+
import { createComponents } from './utils.js'
|
|
14
|
+
import { pEvent } from 'p-event'
|
|
16
15
|
|
|
17
16
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
18
17
|
describe('pubsub connection handlers', () => {
|
|
19
18
|
let psA: PubSub
|
|
20
19
|
let psB: PubSub
|
|
21
|
-
let
|
|
22
|
-
let
|
|
23
|
-
let registrarA: Registrar
|
|
24
|
-
let registrarB: Registrar
|
|
20
|
+
let componentsA: Components
|
|
21
|
+
let componentsB: Components
|
|
25
22
|
|
|
26
23
|
describe('nodes send state on connection', () => {
|
|
27
24
|
// Create pubsub nodes and connect them
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
registrarA = mockRegistrar()
|
|
33
|
-
registrarB = mockRegistrar()
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
componentsA = await createComponents()
|
|
27
|
+
componentsB = await createComponents()
|
|
34
28
|
|
|
35
29
|
psA = await common.setup({
|
|
36
|
-
components:
|
|
37
|
-
peerId: peerA,
|
|
38
|
-
registrar: registrarA
|
|
39
|
-
}),
|
|
30
|
+
components: componentsA,
|
|
40
31
|
init: {}
|
|
41
32
|
})
|
|
42
33
|
psB = await common.setup({
|
|
43
|
-
components:
|
|
44
|
-
peerId: peerB,
|
|
45
|
-
registrar: registrarB
|
|
46
|
-
}),
|
|
34
|
+
components: componentsB,
|
|
47
35
|
init: {}
|
|
48
36
|
})
|
|
49
37
|
|
|
@@ -55,7 +43,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
55
43
|
})
|
|
56
44
|
|
|
57
45
|
// Make subscriptions prior to nodes connected
|
|
58
|
-
|
|
46
|
+
beforeEach(() => {
|
|
59
47
|
psA.subscribe('Za')
|
|
60
48
|
psB.subscribe('Zb')
|
|
61
49
|
|
|
@@ -65,28 +53,19 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
65
53
|
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
66
54
|
})
|
|
67
55
|
|
|
68
|
-
|
|
56
|
+
afterEach(async () => {
|
|
69
57
|
sinon.restore()
|
|
58
|
+
await stop(psA, psB)
|
|
70
59
|
await common.teardown()
|
|
71
60
|
})
|
|
72
61
|
|
|
73
62
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
74
63
|
const subscriptionsChanged = Promise.all([
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
})),
|
|
78
|
-
new Promise((resolve) => psB.addEventListener('subscription-change', resolve, {
|
|
79
|
-
once: true
|
|
80
|
-
}))
|
|
64
|
+
pEvent(psA, 'subscription-change'),
|
|
65
|
+
pEvent(psB, 'subscription-change')
|
|
81
66
|
])
|
|
82
67
|
|
|
83
|
-
await connectPeers(psA.multicodecs[0],
|
|
84
|
-
peerId: peerA,
|
|
85
|
-
registrar: registrarA
|
|
86
|
-
}, {
|
|
87
|
-
peerId: peerB,
|
|
88
|
-
registrar: registrarB
|
|
89
|
-
})
|
|
68
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
90
69
|
|
|
91
70
|
await subscriptionsChanged
|
|
92
71
|
|
|
@@ -96,39 +75,28 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
96
75
|
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
97
76
|
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
98
77
|
|
|
99
|
-
expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([
|
|
100
|
-
expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([
|
|
78
|
+
expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([componentsB.getPeerId().toString()])
|
|
79
|
+
expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
|
|
101
80
|
})
|
|
102
81
|
})
|
|
103
82
|
|
|
104
83
|
describe('pubsub started before connect', () => {
|
|
105
84
|
let psA: PubSub
|
|
106
85
|
let psB: PubSub
|
|
107
|
-
let
|
|
108
|
-
let
|
|
109
|
-
let registrarA: Registrar
|
|
110
|
-
let registrarB: Registrar
|
|
86
|
+
let componentsA: Components
|
|
87
|
+
let componentsB: Components
|
|
111
88
|
|
|
112
89
|
// Create pubsub nodes and start them
|
|
113
90
|
beforeEach(async () => {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
registrarA = mockRegistrar()
|
|
118
|
-
registrarB = mockRegistrar()
|
|
91
|
+
componentsA = await createComponents()
|
|
92
|
+
componentsB = await createComponents()
|
|
119
93
|
|
|
120
94
|
psA = await common.setup({
|
|
121
|
-
components:
|
|
122
|
-
peerId: peerA,
|
|
123
|
-
registrar: registrarA
|
|
124
|
-
}),
|
|
95
|
+
components: componentsA,
|
|
125
96
|
init: {}
|
|
126
97
|
})
|
|
127
98
|
psB = await common.setup({
|
|
128
|
-
components:
|
|
129
|
-
peerId: peerB,
|
|
130
|
-
registrar: registrarB
|
|
131
|
-
}),
|
|
99
|
+
components: componentsB,
|
|
132
100
|
init: {}
|
|
133
101
|
})
|
|
134
102
|
|
|
@@ -137,18 +105,12 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
137
105
|
|
|
138
106
|
afterEach(async () => {
|
|
139
107
|
sinon.restore()
|
|
140
|
-
|
|
108
|
+
await stop(psA, psB)
|
|
141
109
|
await common.teardown()
|
|
142
110
|
})
|
|
143
111
|
|
|
144
112
|
it('should get notified of connected peers on dial', async () => {
|
|
145
|
-
await connectPeers(psA.multicodecs[0],
|
|
146
|
-
peerId: peerA,
|
|
147
|
-
registrar: registrarA
|
|
148
|
-
}, {
|
|
149
|
-
peerId: peerB,
|
|
150
|
-
registrar: registrarB
|
|
151
|
-
})
|
|
113
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
152
114
|
|
|
153
115
|
return await Promise.all([
|
|
154
116
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -161,13 +123,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
161
123
|
const topic = 'test-topic'
|
|
162
124
|
const data = uint8ArrayFromString('hey!')
|
|
163
125
|
|
|
164
|
-
await connectPeers(psA.multicodecs[0],
|
|
165
|
-
peerId: peerA,
|
|
166
|
-
registrar: registrarA
|
|
167
|
-
}, {
|
|
168
|
-
peerId: peerB,
|
|
169
|
-
registrar: registrarB
|
|
170
|
-
})
|
|
126
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
171
127
|
|
|
172
128
|
let subscribedTopics = psA.getTopics()
|
|
173
129
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -188,9 +144,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
188
144
|
// wait for psB to know about psA subscription
|
|
189
145
|
await pWaitFor(() => {
|
|
190
146
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
191
|
-
return subscribedPeers.map(p => p.toString()).includes(
|
|
147
|
+
return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
|
|
192
148
|
})
|
|
193
|
-
psB.publish(topic, data)
|
|
149
|
+
await psB.publish(topic, data)
|
|
194
150
|
|
|
195
151
|
await defer.promise
|
|
196
152
|
})
|
|
@@ -199,53 +155,34 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
199
155
|
describe('pubsub started after connect', () => {
|
|
200
156
|
let psA: PubSub
|
|
201
157
|
let psB: PubSub
|
|
202
|
-
let
|
|
203
|
-
let
|
|
204
|
-
let registrarA: Registrar
|
|
205
|
-
let registrarB: Registrar
|
|
158
|
+
let componentsA: Components
|
|
159
|
+
let componentsB: Components
|
|
206
160
|
|
|
207
161
|
// Create pubsub nodes
|
|
208
162
|
beforeEach(async () => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
registrarA = mockRegistrar()
|
|
213
|
-
registrarB = mockRegistrar()
|
|
163
|
+
componentsA = await createComponents()
|
|
164
|
+
componentsB = await createComponents()
|
|
214
165
|
|
|
215
166
|
psA = await common.setup({
|
|
216
|
-
components:
|
|
217
|
-
peerId: peerA,
|
|
218
|
-
registrar: registrarA
|
|
219
|
-
}),
|
|
167
|
+
components: componentsA,
|
|
220
168
|
init: {}
|
|
221
169
|
})
|
|
222
170
|
psB = await common.setup({
|
|
223
|
-
components:
|
|
224
|
-
peerId: peerB,
|
|
225
|
-
registrar: registrarB
|
|
226
|
-
}),
|
|
171
|
+
components: componentsB,
|
|
227
172
|
init: {}
|
|
228
173
|
})
|
|
229
174
|
})
|
|
230
175
|
|
|
231
176
|
afterEach(async () => {
|
|
232
177
|
sinon.restore()
|
|
233
|
-
|
|
234
178
|
await stop(psA, psB)
|
|
235
|
-
|
|
236
179
|
await common.teardown()
|
|
237
180
|
})
|
|
238
181
|
|
|
239
182
|
it('should get notified of connected peers after starting', async () => {
|
|
240
183
|
await start(psA, psB)
|
|
241
184
|
|
|
242
|
-
await connectPeers(psA.multicodecs[0],
|
|
243
|
-
peerId: peerA,
|
|
244
|
-
registrar: registrarA
|
|
245
|
-
}, {
|
|
246
|
-
peerId: peerB,
|
|
247
|
-
registrar: registrarB
|
|
248
|
-
})
|
|
185
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
249
186
|
|
|
250
187
|
return await Promise.all([
|
|
251
188
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -260,13 +197,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
260
197
|
|
|
261
198
|
await start(psA, psB)
|
|
262
199
|
|
|
263
|
-
await connectPeers(psA.multicodecs[0],
|
|
264
|
-
peerId: peerA,
|
|
265
|
-
registrar: registrarA
|
|
266
|
-
}, {
|
|
267
|
-
peerId: peerB,
|
|
268
|
-
registrar: registrarB
|
|
269
|
-
})
|
|
200
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
270
201
|
|
|
271
202
|
await Promise.all([
|
|
272
203
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -292,9 +223,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
292
223
|
// wait for psB to know about psA subscription
|
|
293
224
|
await pWaitFor(() => {
|
|
294
225
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
295
|
-
return subscribedPeers.map(p => p.toString()).includes(
|
|
226
|
+
return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
|
|
296
227
|
})
|
|
297
|
-
psB.publish(topic, data)
|
|
228
|
+
await psB.publish(topic, data)
|
|
298
229
|
|
|
299
230
|
await defer.promise
|
|
300
231
|
})
|
|
@@ -303,31 +234,20 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
303
234
|
describe('pubsub with intermittent connections', () => {
|
|
304
235
|
let psA: PubSub
|
|
305
236
|
let psB: PubSub
|
|
306
|
-
let
|
|
307
|
-
let
|
|
308
|
-
let registrarA: Registrar
|
|
309
|
-
let registrarB: Registrar
|
|
237
|
+
let componentsA: Components
|
|
238
|
+
let componentsB: Components
|
|
310
239
|
|
|
311
240
|
// Create pubsub nodes and start them
|
|
312
241
|
beforeEach(async () => {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
registrarA = mockRegistrar()
|
|
317
|
-
registrarB = mockRegistrar()
|
|
242
|
+
componentsA = await createComponents()
|
|
243
|
+
componentsB = await createComponents()
|
|
318
244
|
|
|
319
245
|
psA = await common.setup({
|
|
320
|
-
components:
|
|
321
|
-
peerId: peerA,
|
|
322
|
-
registrar: registrarA
|
|
323
|
-
}),
|
|
246
|
+
components: componentsA,
|
|
324
247
|
init: {}
|
|
325
248
|
})
|
|
326
249
|
psB = await common.setup({
|
|
327
|
-
components:
|
|
328
|
-
peerId: peerB,
|
|
329
|
-
registrar: registrarB
|
|
330
|
-
}),
|
|
250
|
+
components: componentsB,
|
|
331
251
|
init: {}
|
|
332
252
|
})
|
|
333
253
|
|
|
@@ -336,9 +256,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
336
256
|
|
|
337
257
|
afterEach(async () => {
|
|
338
258
|
sinon.restore()
|
|
339
|
-
|
|
340
259
|
await stop(psA, psB)
|
|
341
|
-
|
|
342
260
|
await common.teardown()
|
|
343
261
|
})
|
|
344
262
|
|
|
@@ -350,13 +268,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
350
268
|
const defer1 = pDefer()
|
|
351
269
|
const defer2 = pDefer()
|
|
352
270
|
|
|
353
|
-
await connectPeers(psA.multicodecs[0],
|
|
354
|
-
peerId: peerA,
|
|
355
|
-
registrar: registrarA
|
|
356
|
-
}, {
|
|
357
|
-
peerId: peerB,
|
|
358
|
-
registrar: registrarB
|
|
359
|
-
})
|
|
271
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
360
272
|
|
|
361
273
|
let subscribedTopics = psA.getTopics()
|
|
362
274
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -378,9 +290,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
378
290
|
// wait for psB to know about psA subscription
|
|
379
291
|
await pWaitFor(() => {
|
|
380
292
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
381
|
-
return subscribedPeers.map(p => p.toString()).includes(
|
|
293
|
+
return subscribedPeers.map(p => p.toString()).includes(componentsA.getPeerId().toString()) // eslint-disable-line max-nested-callbacks
|
|
382
294
|
})
|
|
383
|
-
psB.publish(topic, data)
|
|
295
|
+
await psB.publish(topic, data)
|
|
384
296
|
|
|
385
297
|
await defer1.promise
|
|
386
298
|
|
|
@@ -399,21 +311,15 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
399
311
|
await psB._libp2p.start()
|
|
400
312
|
await psB.start()
|
|
401
313
|
|
|
402
|
-
await connectPeers(psA.multicodecs[0],
|
|
403
|
-
peerId: peerA,
|
|
404
|
-
registrar: registrarA
|
|
405
|
-
}, {
|
|
406
|
-
peerId: peerB,
|
|
407
|
-
registrar: registrarB
|
|
408
|
-
})
|
|
314
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
409
315
|
|
|
410
316
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
411
317
|
await pWaitFor(() => {
|
|
412
318
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
413
|
-
return subscribedPeers.toString().includes(
|
|
319
|
+
return subscribedPeers.toString().includes(componentsA.getPeerId().toString())
|
|
414
320
|
})
|
|
415
321
|
|
|
416
|
-
psB.publish(topic, data)
|
|
322
|
+
await psB.publish(topic, data)
|
|
417
323
|
|
|
418
324
|
await defer2.promise
|
|
419
325
|
})
|
|
@@ -469,23 +375,17 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
469
375
|
const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
470
376
|
|
|
471
377
|
// second connection
|
|
472
|
-
await connectPeers(psA.multicodecs[0],
|
|
473
|
-
peerId: peerA,
|
|
474
|
-
registrar: registrarA
|
|
475
|
-
}, {
|
|
476
|
-
peerId: peerB,
|
|
477
|
-
registrar: registrarB
|
|
478
|
-
})
|
|
378
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
479
379
|
|
|
480
380
|
// Wait for subscriptions to occur
|
|
481
381
|
await pWaitFor(() => {
|
|
482
|
-
return psA.getSubscribers(topic).includes(
|
|
483
|
-
psB.getSubscribers(topic).map(p => p.toString()).includes(
|
|
382
|
+
return psA.getSubscribers(topic).map(p => p.toString()).includes(componentsB.getPeerId().toString()) &&
|
|
383
|
+
psB.getSubscribers(topic).map(p => p.toString()).includes(componentsA.getPeerId().toString())
|
|
484
384
|
})
|
|
485
385
|
|
|
486
386
|
// Verify messages go both ways
|
|
487
|
-
psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
|
|
488
|
-
psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
|
|
387
|
+
await psA.publish(topic, uint8ArrayFromString('message-from-a-1'))
|
|
388
|
+
await psB.publish(topic, uint8ArrayFromString('message-from-b-1'))
|
|
489
389
|
await pWaitFor(() => {
|
|
490
390
|
return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
|
|
491
391
|
})
|
|
@@ -498,8 +398,8 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
498
398
|
await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
|
|
499
399
|
|
|
500
400
|
// Verify messages go both ways after the disconnect
|
|
501
|
-
psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
|
|
502
|
-
psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
|
|
401
|
+
await psA.publish(topic, uint8ArrayFromString('message-from-a-2'))
|
|
402
|
+
await psB.publish(topic, uint8ArrayFromString('message-from-b-2'))
|
|
503
403
|
await pWaitFor(() => {
|
|
504
404
|
return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
|
|
505
405
|
})
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { PubSubArgs } from './index.js'
|
|
|
8
8
|
import { Components } from '@libp2p/interfaces/components'
|
|
9
9
|
import { start, stop } from '../index.js'
|
|
10
10
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
11
|
+
import { createComponents } from './utils.js'
|
|
11
12
|
|
|
12
13
|
const topic = 'foo'
|
|
13
14
|
const data = uint8ArrayFromString('bar')
|
|
@@ -16,14 +17,14 @@ const shouldNotHappen = () => expect.fail()
|
|
|
16
17
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
17
18
|
describe('emit self', () => {
|
|
18
19
|
let pubsub: PubSub
|
|
20
|
+
let components: Components
|
|
19
21
|
|
|
20
22
|
describe('enabled', () => {
|
|
21
23
|
before(async () => {
|
|
24
|
+
components = await createComponents()
|
|
25
|
+
|
|
22
26
|
pubsub = await common.setup({
|
|
23
|
-
components
|
|
24
|
-
peerId: await createEd25519PeerId(),
|
|
25
|
-
registrar: mockRegistrar()
|
|
26
|
-
}),
|
|
27
|
+
components,
|
|
27
28
|
init: {
|
|
28
29
|
emitSelf: true
|
|
29
30
|
}
|
|
@@ -52,9 +53,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
52
53
|
})
|
|
53
54
|
})
|
|
54
55
|
|
|
55
|
-
pubsub.publish(topic, data)
|
|
56
|
+
const result = await pubsub.publish(topic, data)
|
|
57
|
+
|
|
58
|
+
await promise
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
expect(result).to.have.property('recipients').with.lengthOf(1)
|
|
58
61
|
})
|
|
59
62
|
})
|
|
60
63
|
|
|
@@ -87,7 +90,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
87
90
|
once: true
|
|
88
91
|
})
|
|
89
92
|
|
|
90
|
-
pubsub.publish(topic, data)
|
|
93
|
+
await pubsub.publish(topic, data)
|
|
91
94
|
|
|
92
95
|
// Wait 1 second to guarantee that self is not noticed
|
|
93
96
|
return await new Promise((resolve) => setTimeout(resolve, 1000))
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
import { expect } from 'aegir/chai'
|
|
2
2
|
import sinon from 'sinon'
|
|
3
|
-
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
4
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
|
-
import { mockRegistrar } from '../mocks/registrar.js'
|
|
6
4
|
import type { TestSetup } from '../index.js'
|
|
7
5
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
8
6
|
import type { PubSubArgs } from './index.js'
|
|
9
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
10
|
-
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
7
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
11
8
|
import { start, stop } from '../index.js'
|
|
12
9
|
import { pEvent } from 'p-event'
|
|
10
|
+
import { createComponents } from './utils.js'
|
|
13
11
|
|
|
14
12
|
const topic = 'foo'
|
|
15
13
|
const data = uint8ArrayFromString('bar')
|
|
16
14
|
|
|
17
15
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
18
16
|
describe('messages', () => {
|
|
19
|
-
let peerId: PeerId
|
|
20
17
|
let pubsub: PubSub
|
|
18
|
+
let components: Components
|
|
21
19
|
|
|
22
20
|
// Create pubsub router
|
|
23
21
|
beforeEach(async () => {
|
|
24
|
-
|
|
22
|
+
components = await createComponents()
|
|
25
23
|
|
|
26
24
|
pubsub = await common.setup({
|
|
27
|
-
components
|
|
28
|
-
peerId,
|
|
29
|
-
registrar: mockRegistrar()
|
|
30
|
-
}),
|
|
25
|
+
components,
|
|
31
26
|
init: {
|
|
32
27
|
emitSelf: true
|
|
33
28
|
}
|
|
@@ -42,13 +37,16 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
42
37
|
})
|
|
43
38
|
|
|
44
39
|
it('should emit normalized signed messages on publish', async () => {
|
|
40
|
+
const eventPromise = pEvent<'message', CustomEvent<Message>>(pubsub, 'message')
|
|
41
|
+
|
|
45
42
|
pubsub.globalSignaturePolicy = 'StrictSign'
|
|
46
|
-
pubsub.
|
|
43
|
+
pubsub.subscribe(topic)
|
|
44
|
+
await pubsub.publish(topic, data)
|
|
47
45
|
|
|
48
|
-
const event = await
|
|
46
|
+
const event = await eventPromise
|
|
49
47
|
const message = event.detail
|
|
50
48
|
|
|
51
|
-
expect(message.from.toString()).to.equal(
|
|
49
|
+
expect(message.from.toString()).to.equal(components.getPeerId().toString())
|
|
52
50
|
expect(message.sequenceNumber).to.not.eql(undefined)
|
|
53
51
|
expect(message.key).to.not.eql(undefined)
|
|
54
52
|
expect(message.signature).to.not.eql(undefined)
|