@libp2p/interface-compliance-tests 1.1.24 → 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.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 +53 -145
- 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 +80 -216
- 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 +54 -151
- package/src/pubsub/emit-self.ts +10 -7
- package/src/pubsub/messages.ts +11 -13
- package/src/pubsub/multiple-nodes.ts +88 -220
- package/src/pubsub/two-nodes.ts +23 -41
- package/src/pubsub/utils.ts +11 -0
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -5,15 +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 { waitForSubscriptionUpdate } from './utils.js'
|
|
8
|
+
import { connectPeers } from '../mocks/registrar.js'
|
|
9
|
+
import { createComponents, waitForSubscriptionUpdate } from './utils.js'
|
|
11
10
|
import type { TestSetup } from '../index.js'
|
|
12
11
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
13
12
|
import type { PubSubArgs } from './index.js'
|
|
14
|
-
import type {
|
|
15
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
16
|
-
import { Components } from '@libp2p/interfaces/components'
|
|
13
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
17
14
|
import { start, stop } from '../index.js'
|
|
18
15
|
|
|
19
16
|
const topic = 'foo'
|
|
@@ -26,33 +23,22 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
26
23
|
describe('pubsub with two nodes', () => {
|
|
27
24
|
let psA: PubSub
|
|
28
25
|
let psB: PubSub
|
|
29
|
-
let
|
|
30
|
-
let
|
|
31
|
-
let registrarA: Registrar
|
|
32
|
-
let registrarB: Registrar
|
|
26
|
+
let componentsA: Components
|
|
27
|
+
let componentsB: Components
|
|
33
28
|
|
|
34
29
|
// Create pubsub nodes and connect them
|
|
35
30
|
beforeEach(async () => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
registrarA = mockRegistrar()
|
|
40
|
-
registrarB = mockRegistrar()
|
|
31
|
+
componentsA = await createComponents()
|
|
32
|
+
componentsB = await createComponents()
|
|
41
33
|
|
|
42
34
|
psA = await common.setup({
|
|
43
|
-
components:
|
|
44
|
-
peerId: peerIdA,
|
|
45
|
-
registrar: registrarA
|
|
46
|
-
}),
|
|
35
|
+
components: componentsA,
|
|
47
36
|
init: {
|
|
48
37
|
emitSelf: true
|
|
49
38
|
}
|
|
50
39
|
})
|
|
51
40
|
psB = await common.setup({
|
|
52
|
-
components:
|
|
53
|
-
peerId: peerIdB,
|
|
54
|
-
registrar: registrarB
|
|
55
|
-
}),
|
|
41
|
+
components: componentsB,
|
|
56
42
|
init: {
|
|
57
43
|
emitSelf: false
|
|
58
44
|
}
|
|
@@ -64,13 +50,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
64
50
|
expect(psA.getPeers()).to.be.empty()
|
|
65
51
|
expect(psB.getPeers()).to.be.empty()
|
|
66
52
|
|
|
67
|
-
await connectPeers(psA.multicodecs[0],
|
|
68
|
-
peerId: peerIdA,
|
|
69
|
-
registrar: registrarA
|
|
70
|
-
}, {
|
|
71
|
-
peerId: peerIdB,
|
|
72
|
-
registrar: registrarB
|
|
73
|
-
})
|
|
53
|
+
await connectPeers(psA.multicodecs[0], componentsA, componentsB)
|
|
74
54
|
|
|
75
55
|
// Wait for peers to be ready in pubsub
|
|
76
56
|
await pWaitFor(() => psA.getPeers().length === 1 && psB.getPeers().length === 1)
|
|
@@ -91,7 +71,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
91
71
|
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
92
72
|
expect(psA.getTopics()).to.deep.equal([topic])
|
|
93
73
|
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
94
|
-
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()])
|
|
95
75
|
expect(changedPeerId).to.deep.equal(psB.getPeers()[0])
|
|
96
76
|
expect(changedSubs).to.have.lengthOf(1)
|
|
97
77
|
expect(changedSubs[0].topic).to.equal(topic)
|
|
@@ -123,11 +103,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
123
103
|
psB.subscribe(topic)
|
|
124
104
|
|
|
125
105
|
await Promise.all([
|
|
126
|
-
waitForSubscriptionUpdate(psA,
|
|
127
|
-
waitForSubscriptionUpdate(psB,
|
|
106
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
107
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
128
108
|
])
|
|
129
109
|
|
|
130
|
-
psA.publish(topic, uint8ArrayFromString('hey'))
|
|
110
|
+
await psA.publish(topic, uint8ArrayFromString('hey'))
|
|
131
111
|
|
|
132
112
|
return await defer.promise
|
|
133
113
|
})
|
|
@@ -166,11 +146,11 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
166
146
|
psB.subscribe(topic)
|
|
167
147
|
|
|
168
148
|
await Promise.all([
|
|
169
|
-
waitForSubscriptionUpdate(psA,
|
|
170
|
-
waitForSubscriptionUpdate(psB,
|
|
149
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
150
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
171
151
|
])
|
|
172
152
|
|
|
173
|
-
psB.publish(topic, uint8ArrayFromString('banana'))
|
|
153
|
+
await psB.publish(topic, uint8ArrayFromString('banana'))
|
|
174
154
|
|
|
175
155
|
return await defer.promise
|
|
176
156
|
})
|
|
@@ -185,7 +165,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
185
165
|
function receivedMsg (evt: CustomEvent<Message>) {
|
|
186
166
|
const msg = evt.detail
|
|
187
167
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
188
|
-
expect(msg.from.toString()).to.equal(
|
|
168
|
+
expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString())
|
|
189
169
|
expect(msg.sequenceNumber).to.be.a('BigInt')
|
|
190
170
|
expect(msg.topic).to.be.equal(topic)
|
|
191
171
|
|
|
@@ -201,11 +181,13 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
201
181
|
psB.subscribe(topic)
|
|
202
182
|
|
|
203
183
|
await Promise.all([
|
|
204
|
-
waitForSubscriptionUpdate(psA,
|
|
205
|
-
waitForSubscriptionUpdate(psB,
|
|
184
|
+
waitForSubscriptionUpdate(psA, componentsB.getPeerId()),
|
|
185
|
+
waitForSubscriptionUpdate(psB, componentsA.getPeerId())
|
|
206
186
|
])
|
|
207
187
|
|
|
208
|
-
|
|
188
|
+
await Promise.all(
|
|
189
|
+
Array.from({ length: 10 }, async (_, i) => await psB.publish(topic, uint8ArrayFromString('banana')))
|
|
190
|
+
)
|
|
209
191
|
|
|
210
192
|
return await defer.promise
|
|
211
193
|
})
|
package/src/pubsub/utils.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { pEvent } from 'p-event'
|
|
2
2
|
import pWaitFor from 'p-wait-for'
|
|
3
|
+
import { Components } from '@libp2p/interfaces/components'
|
|
3
4
|
import type { PubSub, SubscriptionChangeData } from '@libp2p/interfaces/pubsub'
|
|
4
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
9
|
export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
|
|
7
10
|
await pWaitFor(async () => {
|
|
@@ -10,3 +13,11 @@ export async function waitForSubscriptionUpdate (a: PubSub, b: PeerId) {
|
|
|
10
13
|
return event.detail.peerId.equals(b)
|
|
11
14
|
})
|
|
12
15
|
}
|
|
16
|
+
|
|
17
|
+
export async function createComponents (): Promise<Components> {
|
|
18
|
+
return new Components({
|
|
19
|
+
peerId: await createEd25519PeerId(),
|
|
20
|
+
registrar: mockRegistrar(),
|
|
21
|
+
connectionManager: mockConnectionManager()
|
|
22
|
+
})
|
|
23
|
+
}
|