@libp2p/interface-compliance-tests 1.1.22 → 1.1.25
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 +2 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +18 -21
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +2 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +84 -162
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +2 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +18 -12
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +2 -3
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts +2 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +13 -74
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +87 -167
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +2 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +51 -84
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +5 -2
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +12 -2
- 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 +20 -24
- package/src/pubsub/connection-handlers.ts +97 -180
- package/src/pubsub/emit-self.ts +23 -15
- package/src/pubsub/index.ts +2 -3
- package/src/pubsub/messages.ts +18 -99
- package/src/pubsub/multiple-nodes.ts +106 -184
- package/src/pubsub/two-nodes.ts +61 -98
- package/src/pubsub/utils.ts +16 -5
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -5,17 +5,12 @@ import pDefer from 'p-defer'
|
|
|
5
5
|
import pWaitFor from 'p-wait-for'
|
|
6
6
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
|
-
import { connectPeers
|
|
9
|
-
import {
|
|
10
|
-
import { CustomEvent } from '@libp2p/interfaces'
|
|
11
|
-
import { waitForSubscriptionUpdate } from './utils.js'
|
|
8
|
+
import { connectPeers } from '../mocks/registrar.js'
|
|
9
|
+
import { createComponents, waitForSubscriptionUpdate } from './utils.js'
|
|
12
10
|
import type { TestSetup } from '../index.js'
|
|
13
|
-
import type { Message } from '@libp2p/interfaces/pubsub'
|
|
11
|
+
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
14
12
|
import type { PubSubArgs } from './index.js'
|
|
15
|
-
import type {
|
|
16
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
17
|
-
import type { PubSubBaseProtocol } from '@libp2p/pubsub'
|
|
18
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
13
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
19
14
|
import { start, stop } from '../index.js'
|
|
20
15
|
|
|
21
16
|
const topic = 'foo'
|
|
@@ -24,37 +19,26 @@ function shouldNotHappen () {
|
|
|
24
19
|
expect.fail()
|
|
25
20
|
}
|
|
26
21
|
|
|
27
|
-
export default (common: TestSetup<
|
|
22
|
+
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
28
23
|
describe('pubsub with two nodes', () => {
|
|
29
|
-
let psA:
|
|
30
|
-
let psB:
|
|
31
|
-
let
|
|
32
|
-
let
|
|
33
|
-
let registrarA: Registrar
|
|
34
|
-
let registrarB: Registrar
|
|
24
|
+
let psA: PubSub
|
|
25
|
+
let psB: PubSub
|
|
26
|
+
let componentsA: Components
|
|
27
|
+
let componentsB: Components
|
|
35
28
|
|
|
36
29
|
// Create pubsub nodes and connect them
|
|
37
30
|
beforeEach(async () => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
registrarA = mockRegistrar()
|
|
42
|
-
registrarB = mockRegistrar()
|
|
31
|
+
componentsA = await createComponents()
|
|
32
|
+
componentsB = await createComponents()
|
|
43
33
|
|
|
44
34
|
psA = await common.setup({
|
|
45
|
-
components:
|
|
46
|
-
peerId: peerIdA,
|
|
47
|
-
registrar: registrarA
|
|
48
|
-
}),
|
|
35
|
+
components: componentsA,
|
|
49
36
|
init: {
|
|
50
37
|
emitSelf: true
|
|
51
38
|
}
|
|
52
39
|
})
|
|
53
40
|
psB = await common.setup({
|
|
54
|
-
components:
|
|
55
|
-
peerId: peerIdB,
|
|
56
|
-
registrar: registrarB
|
|
57
|
-
}),
|
|
41
|
+
components: componentsB,
|
|
58
42
|
init: {
|
|
59
43
|
emitSelf: false
|
|
60
44
|
}
|
|
@@ -66,13 +50,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
66
50
|
expect(psA.getPeers()).to.be.empty()
|
|
67
51
|
expect(psB.getPeers()).to.be.empty()
|
|
68
52
|
|
|
69
|
-
await connectPeers(psA.multicodecs[0],
|
|
70
|
-
peerId: peerIdA,
|
|
71
|
-
registrar: registrarA
|
|
72
|
-
}, {
|
|
73
|
-
peerId: peerIdB,
|
|
74
|
-
registrar: registrarB
|
|
75
|
-
})
|
|
53
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
76
54
|
|
|
77
55
|
// Wait for peers to be ready in pubsub
|
|
78
56
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
@@ -89,11 +67,11 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
89
67
|
it('Subscribe to a topic in nodeA', async () => {
|
|
90
68
|
const defer = pDefer()
|
|
91
69
|
|
|
92
|
-
psB.addEventListener('
|
|
70
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
93
71
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
94
72
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
95
73
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
96
|
-
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([
|
|
74
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([componentsA.getPeerId().toString()])
|
|
97
75
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
98
76
|
expect(changedSubs).to.have.lengthOf(1)
|
|
99
77
|
expect(changedSubs[0].topic).to.equal(topic)
|
|
@@ -110,25 +88,26 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
110
88
|
it('Publish to a topic in nodeA', async () => {
|
|
111
89
|
const defer = pDefer()
|
|
112
90
|
|
|
113
|
-
psA.addEventListener(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
}
|
|
118
98
|
}, {
|
|
119
99
|
once: true
|
|
120
100
|
})
|
|
121
101
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})
|
|
102
|
+
psA.subscribe(topic)
|
|
103
|
+
psB.subscribe(topic)
|
|
125
104
|
|
|
126
105
|
await Promise.all([
|
|
127
|
-
waitForSubscriptionUpdate(psA,
|
|
128
|
-
waitForSubscriptionUpdate(psB,
|
|
106
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
107
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
129
108
|
])
|
|
130
109
|
|
|
131
|
-
|
|
110
|
+
await psA.publish(topic, uint8ArrayFromString('hey'))
|
|
132
111
|
|
|
133
112
|
return await defer.promise
|
|
134
113
|
})
|
|
@@ -136,16 +115,24 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
136
115
|
it('Publish to a topic in nodeB', async () => {
|
|
137
116
|
const defer = pDefer()
|
|
138
117
|
|
|
139
|
-
psA.addEventListener(
|
|
118
|
+
psA.addEventListener('message', (evt) => {
|
|
119
|
+
if (evt.detail.topic !== topic) {
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
140
123
|
const msg = evt.detail
|
|
141
|
-
psA.addEventListener(
|
|
124
|
+
psA.addEventListener('message', (evt) => {
|
|
125
|
+
if (evt.detail.topic === topic) {
|
|
126
|
+
shouldNotHappen()
|
|
127
|
+
}
|
|
128
|
+
}, {
|
|
142
129
|
once: true
|
|
143
130
|
})
|
|
144
131
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
145
132
|
|
|
146
133
|
setTimeout(() => {
|
|
147
|
-
psA.removeEventListener(
|
|
148
|
-
psB.removeEventListener(
|
|
134
|
+
psA.removeEventListener('message')
|
|
135
|
+
psB.removeEventListener('message')
|
|
149
136
|
|
|
150
137
|
defer.resolve()
|
|
151
138
|
}, 100)
|
|
@@ -153,16 +140,17 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
153
140
|
once: true
|
|
154
141
|
})
|
|
155
142
|
|
|
156
|
-
psB.addEventListener(
|
|
157
|
-
|
|
158
|
-
|
|
143
|
+
psB.addEventListener('message', shouldNotHappen)
|
|
144
|
+
|
|
145
|
+
psA.subscribe(topic)
|
|
146
|
+
psB.subscribe(topic)
|
|
159
147
|
|
|
160
148
|
await Promise.all([
|
|
161
|
-
waitForSubscriptionUpdate(psA,
|
|
162
|
-
waitForSubscriptionUpdate(psB,
|
|
149
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
150
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
163
151
|
])
|
|
164
152
|
|
|
165
|
-
|
|
153
|
+
await psB.publish(topic, uint8ArrayFromString('banana'))
|
|
166
154
|
|
|
167
155
|
return await defer.promise
|
|
168
156
|
})
|
|
@@ -171,32 +159,35 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
171
159
|
const defer = pDefer()
|
|
172
160
|
let counter = 0
|
|
173
161
|
|
|
174
|
-
psB.addEventListener(
|
|
175
|
-
|
|
176
|
-
})
|
|
177
|
-
psA.addEventListener(topic, receivedMsg)
|
|
162
|
+
psB.addEventListener('message', shouldNotHappen)
|
|
163
|
+
psA.addEventListener('message', receivedMsg)
|
|
178
164
|
|
|
179
165
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
180
166
|
const msg = evt.detail
|
|
181
167
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
182
|
-
expect(msg.from.toString()).to.equal(
|
|
168
|
+
expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
|
|
183
169
|
expect(msg.sequenceNumber).to.be.a('BigInt')
|
|
184
170
|
expect(msg.topic).to.be.equal(topic)
|
|
185
171
|
|
|
186
172
|
if (++counter === 10) {
|
|
187
|
-
psA.removeEventListener(
|
|
188
|
-
psB.removeEventListener(
|
|
173
|
+
psA.removeEventListener('message', receivedMsg)
|
|
174
|
+
psB.removeEventListener('message', shouldNotHappen)
|
|
189
175
|
|
|
190
176
|
defer.resolve()
|
|
191
177
|
}
|
|
192
178
|
}
|
|
193
179
|
|
|
180
|
+
psA.subscribe(topic)
|
|
181
|
+
psB.subscribe(topic)
|
|
182
|
+
|
|
194
183
|
await Promise.all([
|
|
195
|
-
waitForSubscriptionUpdate(psA,
|
|
196
|
-
waitForSubscriptionUpdate(psB,
|
|
184
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
185
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
197
186
|
])
|
|
198
187
|
|
|
199
|
-
|
|
188
|
+
await Promise.all(
|
|
189
|
+
Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
|
|
190
|
+
)
|
|
200
191
|
|
|
201
192
|
return await defer.promise
|
|
202
193
|
})
|
|
@@ -205,7 +196,7 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
205
196
|
const defer = pDefer()
|
|
206
197
|
let callCount = 0
|
|
207
198
|
|
|
208
|
-
psB.addEventListener('
|
|
199
|
+
psB.addEventListener('subscription-change', (evt) => {
|
|
209
200
|
callCount++
|
|
210
201
|
|
|
211
202
|
if (callCount === 1) {
|
|
@@ -239,33 +230,5 @@ export default (common: TestSetup<PubSubBaseProtocol, PubSubArgs>) => {
|
|
|
239
230
|
|
|
240
231
|
return await defer.promise
|
|
241
232
|
})
|
|
242
|
-
|
|
243
|
-
it.skip('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
244
|
-
const defer = pDefer()
|
|
245
|
-
const topic = 'Z'
|
|
246
|
-
|
|
247
|
-
psA.addEventListener(topic, shouldNotHappen, {
|
|
248
|
-
once: true
|
|
249
|
-
})
|
|
250
|
-
psB.addEventListener(topic, shouldNotHappen, {
|
|
251
|
-
once: true
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
await Promise.all([
|
|
255
|
-
waitForSubscriptionUpdate(psA, psB),
|
|
256
|
-
waitForSubscriptionUpdate(psB, psA)
|
|
257
|
-
])
|
|
258
|
-
|
|
259
|
-
setTimeout(() => {
|
|
260
|
-
psA.removeEventListener(topic, shouldNotHappen)
|
|
261
|
-
psB.removeEventListener(topic, shouldNotHappen)
|
|
262
|
-
defer.resolve()
|
|
263
|
-
}, 100)
|
|
264
|
-
|
|
265
|
-
void psB.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
|
|
266
|
-
void psA.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString('banana') }))
|
|
267
|
-
|
|
268
|
-
return await defer.promise
|
|
269
|
-
})
|
|
270
233
|
})
|
|
271
234
|
}
|
package/src/pubsub/utils.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import { pEvent } from 'p-event'
|
|
2
2
|
import pWaitFor from 'p-wait-for'
|
|
3
|
-
import
|
|
4
|
-
import type {
|
|
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'
|
|
5
8
|
|
|
6
|
-
export async function waitForSubscriptionUpdate (a:
|
|
9
|
+
export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
|
|
7
10
|
await pWaitFor(async () => {
|
|
8
|
-
const event = await pEvent<'
|
|
11
|
+
const event = await pEvent<'subscription-change', CustomEvent<SubscriptionChangeData>>(a, 'subscription-change')
|
|
9
12
|
|
|
10
|
-
return event.detail.peerId.equals(b
|
|
13
|
+
return event.detail.peerId.equals(b)
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function createComponents (): Promise<Components> {
|
|
18
|
+
return new Components({
|
|
19
|
+
peerId: await createEd25519PeerId(),
|
|
20
|
+
registrar: mockRegistrar(),
|
|
21
|
+
connectionManager: mockConnectionManager()
|
|
11
22
|
})
|
|
12
23
|
}
|