@libp2p/interface-compliance-tests 1.1.7 → 1.1.11
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.d.ts +12 -2
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +59 -33
- package/dist/src/mocks/connection.js.map +1 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +2 -1
- package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -1
- package/dist/src/mocks/multiaddr-connection.js +2 -2
- package/dist/src/mocks/multiaddr-connection.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +11 -2
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +47 -5
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/mocks/upgrader.d.ts.map +1 -1
- package/dist/src/mocks/upgrader.js +8 -2
- package/dist/src/mocks/upgrader.js.map +1 -1
- package/dist/src/pubsub/api.d.ts +3 -2
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +23 -13
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts +3 -2
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +165 -69
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts +3 -2
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +15 -4
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +6 -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 +3 -2
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +42 -39
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +3 -2
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +172 -89
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts +3 -2
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +96 -40
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts +2 -2
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +7 -9
- package/dist/src/pubsub/utils.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +5 -2
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/package.json +8 -5
- package/src/mocks/connection.ts +83 -34
- package/src/mocks/multiaddr-connection.ts +3 -2
- package/src/mocks/registrar.ts +65 -7
- package/src/mocks/upgrader.ts +8 -2
- package/src/pubsub/api.ts +29 -15
- package/src/pubsub/connection-handlers.ts +186 -75
- package/src/pubsub/emit-self.ts +19 -7
- package/src/pubsub/index.ts +6 -3
- package/src/pubsub/messages.ts +59 -44
- package/src/pubsub/multiple-nodes.ts +197 -102
- package/src/pubsub/two-nodes.ts +111 -48
- package/src/pubsub/utils.ts +9 -10
- package/src/stream-muxer/close-test.ts +5 -2
package/src/mocks/registrar.ts
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
|
-
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
1
|
+
import type { IncomingStreamData, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
2
|
+
import type { Connection } from '@libp2p/interfaces/src/connection'
|
|
3
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
2
4
|
import type { Topology } from '@libp2p/interfaces/topology'
|
|
5
|
+
import { connectionPair } from './connection.js'
|
|
6
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
3
7
|
|
|
4
8
|
export class MockRegistrar implements Registrar {
|
|
5
9
|
private readonly topologies: Map<string, { topology: Topology, protocols: string[] }> = new Map()
|
|
6
10
|
private readonly handlers: Map<string, { handler: StreamHandler, protocols: string[] }> = new Map()
|
|
7
11
|
|
|
12
|
+
getProtocols () {
|
|
13
|
+
const protocols = new Set<string>()
|
|
14
|
+
|
|
15
|
+
for (const topology of this.topologies.values()) {
|
|
16
|
+
topology.protocols.forEach(protocol => protocols.add(protocol))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
for (const handler of this.handlers.values()) {
|
|
20
|
+
handler.protocols.forEach(protocol => protocols.add(protocol))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return Array.from(protocols).sort()
|
|
24
|
+
}
|
|
25
|
+
|
|
8
26
|
async handle (protocols: string | string[], handler: StreamHandler) {
|
|
9
27
|
if (!Array.isArray(protocols)) {
|
|
10
28
|
protocols = [protocols]
|
|
11
29
|
}
|
|
12
30
|
|
|
31
|
+
for (const protocol of protocols) {
|
|
32
|
+
for (const { protocols } of this.handlers.values()) {
|
|
33
|
+
if (protocols.includes(protocol)) {
|
|
34
|
+
throw new Error(`Handler already registered for protocol ${protocol}`)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
13
39
|
const id = `handler-id-${Math.random()}`
|
|
14
40
|
|
|
15
41
|
this.handlers.set(id, {
|
|
@@ -24,16 +50,14 @@ export class MockRegistrar implements Registrar {
|
|
|
24
50
|
this.handlers.delete(id)
|
|
25
51
|
}
|
|
26
52
|
|
|
27
|
-
|
|
28
|
-
const output: StreamHandler[] = []
|
|
29
|
-
|
|
53
|
+
getHandler (protocol: string) {
|
|
30
54
|
for (const { handler, protocols } of this.handlers.values()) {
|
|
31
55
|
if (protocols.includes(protocol)) {
|
|
32
|
-
|
|
56
|
+
return handler
|
|
33
57
|
}
|
|
34
58
|
}
|
|
35
59
|
|
|
36
|
-
|
|
60
|
+
throw new Error(`No handler registered for protocol ${protocol}`)
|
|
37
61
|
}
|
|
38
62
|
|
|
39
63
|
register (protocols: string | string[], topology: Topology) {
|
|
@@ -68,10 +92,44 @@ export class MockRegistrar implements Registrar {
|
|
|
68
92
|
}
|
|
69
93
|
}
|
|
70
94
|
|
|
71
|
-
|
|
95
|
+
if (output.length > 0) {
|
|
96
|
+
return output
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
throw new Error(`No topologies registered for protocol ${protocol}`)
|
|
72
100
|
}
|
|
73
101
|
}
|
|
74
102
|
|
|
75
103
|
export function mockRegistrar () {
|
|
76
104
|
return new MockRegistrar()
|
|
77
105
|
}
|
|
106
|
+
|
|
107
|
+
export async function mockIncomingStreamEvent (protocol: string, conn: Connection, remotePeer: PeerId): Promise<CustomEvent<IncomingStreamData>> {
|
|
108
|
+
// @ts-expect-error incomplete implementation
|
|
109
|
+
return new CustomEvent('incomingStream', {
|
|
110
|
+
detail: {
|
|
111
|
+
...await conn.newStream([protocol]),
|
|
112
|
+
connection: {
|
|
113
|
+
remotePeer
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface Peer {
|
|
120
|
+
peerId: PeerId
|
|
121
|
+
registrar: Registrar
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function connectPeers (protocol: string, a: Peer, b: Peer) {
|
|
125
|
+
// Notify peers of connection
|
|
126
|
+
const [aToB, bToA] = connectionPair(a, b)
|
|
127
|
+
|
|
128
|
+
for (const topology of a.registrar.getTopologies(protocol)) {
|
|
129
|
+
await topology.onConnect(b.peerId, aToB)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (const topology of b.registrar.getTopologies(protocol)) {
|
|
133
|
+
await topology.onConnect(a.peerId, bToA)
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/mocks/upgrader.ts
CHANGED
|
@@ -18,11 +18,17 @@ export function mockUpgrader (options: MockUpgraderOptions = {}) {
|
|
|
18
18
|
const upgrader: Upgrader = {
|
|
19
19
|
async upgradeOutbound (multiaddrConnection) {
|
|
20
20
|
ensureProps(multiaddrConnection)
|
|
21
|
-
return
|
|
21
|
+
return mockConnection(multiaddrConnection, {
|
|
22
|
+
direction: 'outbound',
|
|
23
|
+
muxer: options.muxer
|
|
24
|
+
})
|
|
22
25
|
},
|
|
23
26
|
async upgradeInbound (multiaddrConnection) {
|
|
24
27
|
ensureProps(multiaddrConnection)
|
|
25
|
-
return
|
|
28
|
+
return mockConnection(multiaddrConnection, {
|
|
29
|
+
direction: 'inbound',
|
|
30
|
+
muxer: options.muxer
|
|
31
|
+
})
|
|
26
32
|
}
|
|
27
33
|
}
|
|
28
34
|
|
package/src/pubsub/api.ts
CHANGED
|
@@ -3,20 +3,33 @@ import sinon from 'sinon'
|
|
|
3
3
|
import pDefer from 'p-defer'
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
|
+
import { mockRegistrar } from '../mocks/registrar.js'
|
|
7
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
|
+
import delay from 'delay'
|
|
9
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
6
10
|
import type { TestSetup } from '../index.js'
|
|
7
|
-
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
11
|
+
import type { PubSub, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
8
12
|
import type { EventMap } from './index.js'
|
|
13
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
14
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
9
15
|
|
|
10
16
|
const topic = 'foo'
|
|
11
17
|
const data = uint8ArrayFromString('bar')
|
|
12
18
|
|
|
13
|
-
export default (common: TestSetup<
|
|
19
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
14
20
|
describe('pubsub api', () => {
|
|
15
21
|
let pubsub: PubSub<EventMap>
|
|
22
|
+
let registrar: Registrar
|
|
16
23
|
|
|
17
24
|
// Create pubsub router
|
|
18
25
|
beforeEach(async () => {
|
|
19
|
-
|
|
26
|
+
registrar = mockRegistrar()
|
|
27
|
+
|
|
28
|
+
pubsub = await common.setup({
|
|
29
|
+
peerId: await createEd25519PeerId(),
|
|
30
|
+
registrar,
|
|
31
|
+
emitSelf: true
|
|
32
|
+
})
|
|
20
33
|
})
|
|
21
34
|
|
|
22
35
|
afterEach(async () => {
|
|
@@ -26,22 +39,22 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
26
39
|
})
|
|
27
40
|
|
|
28
41
|
it('can start correctly', async () => {
|
|
29
|
-
sinon.spy(
|
|
42
|
+
sinon.spy(registrar, 'register')
|
|
30
43
|
|
|
31
44
|
await pubsub.start()
|
|
32
45
|
|
|
33
|
-
expect(pubsub.
|
|
34
|
-
expect(
|
|
46
|
+
expect(pubsub.isStarted()).to.equal(true)
|
|
47
|
+
expect(registrar.register).to.have.property('callCount', 1)
|
|
35
48
|
})
|
|
36
49
|
|
|
37
50
|
it('can stop correctly', async () => {
|
|
38
|
-
sinon.spy(
|
|
51
|
+
sinon.spy(registrar, 'unregister')
|
|
39
52
|
|
|
40
53
|
await pubsub.start()
|
|
41
54
|
await pubsub.stop()
|
|
42
55
|
|
|
43
|
-
expect(pubsub.
|
|
44
|
-
expect(
|
|
56
|
+
expect(pubsub.isStarted()).to.equal(false)
|
|
57
|
+
expect(registrar.unregister).to.have.property('callCount', 1)
|
|
45
58
|
})
|
|
46
59
|
|
|
47
60
|
it('can subscribe and unsubscribe correctly', async () => {
|
|
@@ -50,20 +63,22 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
await pubsub.start()
|
|
53
|
-
pubsub.
|
|
54
|
-
pubsub.addEventListener('topic', handler)
|
|
66
|
+
pubsub.addEventListener(topic, handler)
|
|
55
67
|
|
|
56
68
|
await pWaitFor(() => {
|
|
57
69
|
const topics = pubsub.getTopics()
|
|
58
70
|
return topics.length === 1 && topics[0] === topic
|
|
59
71
|
})
|
|
60
72
|
|
|
61
|
-
pubsub.
|
|
73
|
+
pubsub.removeEventListener(topic, handler)
|
|
62
74
|
|
|
63
75
|
await pWaitFor(() => pubsub.getTopics().length === 0)
|
|
64
76
|
|
|
65
77
|
// Publish to guarantee the handler is not called
|
|
66
|
-
|
|
78
|
+
pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
79
|
+
|
|
80
|
+
// handlers are called async
|
|
81
|
+
await delay(100)
|
|
67
82
|
|
|
68
83
|
await pubsub.stop()
|
|
69
84
|
})
|
|
@@ -73,13 +88,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
73
88
|
|
|
74
89
|
await pubsub.start()
|
|
75
90
|
|
|
76
|
-
pubsub.subscribe(topic)
|
|
77
91
|
pubsub.addEventListener(topic, (evt) => {
|
|
78
92
|
const msg = evt.detail
|
|
79
93
|
expect(msg).to.not.eql(undefined)
|
|
80
94
|
defer.resolve()
|
|
81
95
|
})
|
|
82
|
-
|
|
96
|
+
pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
83
97
|
await defer.promise
|
|
84
98
|
|
|
85
99
|
await pubsub.stop()
|
|
@@ -4,28 +4,49 @@ 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 {
|
|
7
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
9
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
8
10
|
import type { TestSetup } from '../index.js'
|
|
9
|
-
import type {
|
|
11
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
10
12
|
import type { EventMap } from './index.js'
|
|
13
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
14
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
15
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
11
16
|
|
|
12
|
-
export default (common: TestSetup<
|
|
17
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
13
18
|
describe('pubsub connection handlers', () => {
|
|
14
|
-
let psA:
|
|
15
|
-
let psB:
|
|
19
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
20
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
21
|
+
let peerA: PeerId
|
|
22
|
+
let peerB: PeerId
|
|
23
|
+
let registrarA: Registrar
|
|
24
|
+
let registrarB: Registrar
|
|
16
25
|
|
|
17
26
|
describe('nodes send state on connection', () => {
|
|
18
27
|
// Create pubsub nodes and connect them
|
|
19
28
|
before(async () => {
|
|
20
|
-
|
|
21
|
-
|
|
29
|
+
peerA = await createEd25519PeerId()
|
|
30
|
+
peerB = await createEd25519PeerId()
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
registrarA = mockRegistrar()
|
|
33
|
+
registrarB = mockRegistrar()
|
|
34
|
+
|
|
35
|
+
psA = await common.setup({
|
|
36
|
+
peerId: peerA,
|
|
37
|
+
registrar: registrarA
|
|
38
|
+
})
|
|
39
|
+
psB = await common.setup({
|
|
40
|
+
peerId: peerB,
|
|
41
|
+
registrar: registrarB
|
|
42
|
+
})
|
|
25
43
|
|
|
26
44
|
// Start pubsub
|
|
27
45
|
await psA.start()
|
|
28
46
|
await psB.start()
|
|
47
|
+
|
|
48
|
+
expect(psA.getPeers()).to.be.empty()
|
|
49
|
+
expect(psB.getPeers()).to.be.empty()
|
|
29
50
|
})
|
|
30
51
|
|
|
31
52
|
// Make subscriptions prior to nodes connected
|
|
@@ -33,10 +54,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
33
54
|
psA.subscribe('Za')
|
|
34
55
|
psB.subscribe('Zb')
|
|
35
56
|
|
|
36
|
-
expect(psA.
|
|
37
|
-
|
|
38
|
-
expect(psB.
|
|
39
|
-
|
|
57
|
+
expect(psA.getPeers()).to.be.empty()
|
|
58
|
+
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
59
|
+
expect(psB.getPeers()).to.be.empty()
|
|
60
|
+
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
40
61
|
})
|
|
41
62
|
|
|
42
63
|
after(async () => {
|
|
@@ -45,9 +66,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
45
66
|
})
|
|
46
67
|
|
|
47
68
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
48
|
-
|
|
49
|
-
// @ts-expect-error protected fields
|
|
50
|
-
psA._libp2p.dial(psB.peerId),
|
|
69
|
+
const subscriptionsChanged = Promise.all([
|
|
51
70
|
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
52
71
|
once: true
|
|
53
72
|
})),
|
|
@@ -56,24 +75,51 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
56
75
|
}))
|
|
57
76
|
])
|
|
58
77
|
|
|
59
|
-
|
|
60
|
-
|
|
78
|
+
await connectPeers(psA.multicodecs[0], {
|
|
79
|
+
peerId: peerA,
|
|
80
|
+
registrar: registrarA
|
|
81
|
+
}, {
|
|
82
|
+
peerId: peerB,
|
|
83
|
+
registrar: registrarB
|
|
84
|
+
})
|
|
61
85
|
|
|
62
|
-
|
|
86
|
+
await subscriptionsChanged
|
|
63
87
|
|
|
64
|
-
|
|
88
|
+
expect(psA.getPeers()).to.have.lengthOf(1)
|
|
89
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
65
90
|
|
|
66
|
-
|
|
91
|
+
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
92
|
+
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
67
93
|
|
|
68
|
-
|
|
94
|
+
expect(psA.getSubscribers('Zb').map(p => p.toString())).to.deep.equal([peerB.toString()])
|
|
95
|
+
expect(psB.getSubscribers('Za').map(p => p.toString())).to.deep.equal([peerA.toString()])
|
|
69
96
|
})
|
|
70
97
|
})
|
|
71
98
|
|
|
72
99
|
describe('pubsub started before connect', () => {
|
|
100
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
101
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
102
|
+
let peerA: PeerId
|
|
103
|
+
let peerB: PeerId
|
|
104
|
+
let registrarA: Registrar
|
|
105
|
+
let registrarB: Registrar
|
|
106
|
+
|
|
73
107
|
// Create pubsub nodes and start them
|
|
74
108
|
beforeEach(async () => {
|
|
75
|
-
|
|
76
|
-
|
|
109
|
+
peerA = await createEd25519PeerId()
|
|
110
|
+
peerB = await createEd25519PeerId()
|
|
111
|
+
|
|
112
|
+
registrarA = mockRegistrar()
|
|
113
|
+
registrarB = mockRegistrar()
|
|
114
|
+
|
|
115
|
+
psA = await common.setup({
|
|
116
|
+
peerId: peerA,
|
|
117
|
+
registrar: registrarA
|
|
118
|
+
})
|
|
119
|
+
psB = await common.setup({
|
|
120
|
+
peerId: peerB,
|
|
121
|
+
registrar: registrarB
|
|
122
|
+
})
|
|
77
123
|
|
|
78
124
|
await psA.start()
|
|
79
125
|
await psB.start()
|
|
@@ -86,13 +132,17 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
86
132
|
})
|
|
87
133
|
|
|
88
134
|
it('should get notified of connected peers on dial', async () => {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
135
|
+
await connectPeers(psA.multicodecs[0], {
|
|
136
|
+
peerId: peerA,
|
|
137
|
+
registrar: registrarA
|
|
138
|
+
}, {
|
|
139
|
+
peerId: peerB,
|
|
140
|
+
registrar: registrarB
|
|
141
|
+
})
|
|
92
142
|
|
|
93
143
|
return await Promise.all([
|
|
94
|
-
pWaitFor(() => psA.
|
|
95
|
-
pWaitFor(() => psB.
|
|
144
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
145
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
96
146
|
])
|
|
97
147
|
})
|
|
98
148
|
|
|
@@ -101,8 +151,13 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
101
151
|
const topic = 'test-topic'
|
|
102
152
|
const data = uint8ArrayFromString('hey!')
|
|
103
153
|
|
|
104
|
-
|
|
105
|
-
|
|
154
|
+
await connectPeers(psA.multicodecs[0], {
|
|
155
|
+
peerId: peerA,
|
|
156
|
+
registrar: registrarA
|
|
157
|
+
}, {
|
|
158
|
+
peerId: peerB,
|
|
159
|
+
registrar: registrarB
|
|
160
|
+
})
|
|
106
161
|
|
|
107
162
|
let subscribedTopics = psA.getTopics()
|
|
108
163
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -120,19 +175,38 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
120
175
|
// wait for psB to know about psA subscription
|
|
121
176
|
await pWaitFor(() => {
|
|
122
177
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
123
|
-
return subscribedPeers.includes(
|
|
178
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
124
179
|
})
|
|
125
|
-
void psB.
|
|
180
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
126
181
|
|
|
127
182
|
await defer.promise
|
|
128
183
|
})
|
|
129
184
|
})
|
|
130
185
|
|
|
131
186
|
describe('pubsub started after connect', () => {
|
|
187
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
188
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
189
|
+
let peerA: PeerId
|
|
190
|
+
let peerB: PeerId
|
|
191
|
+
let registrarA: Registrar
|
|
192
|
+
let registrarB: Registrar
|
|
193
|
+
|
|
132
194
|
// Create pubsub nodes
|
|
133
195
|
beforeEach(async () => {
|
|
134
|
-
|
|
135
|
-
|
|
196
|
+
peerA = await createEd25519PeerId()
|
|
197
|
+
peerB = await createEd25519PeerId()
|
|
198
|
+
|
|
199
|
+
registrarA = mockRegistrar()
|
|
200
|
+
registrarB = mockRegistrar()
|
|
201
|
+
|
|
202
|
+
psA = await common.setup({
|
|
203
|
+
peerId: peerA,
|
|
204
|
+
registrar: registrarA
|
|
205
|
+
})
|
|
206
|
+
psB = await common.setup({
|
|
207
|
+
peerId: peerB,
|
|
208
|
+
registrar: registrarB
|
|
209
|
+
})
|
|
136
210
|
})
|
|
137
211
|
|
|
138
212
|
afterEach(async () => {
|
|
@@ -145,18 +219,20 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
145
219
|
})
|
|
146
220
|
|
|
147
221
|
it('should get notified of connected peers after starting', async () => {
|
|
148
|
-
// @ts-expect-error protected fields
|
|
149
|
-
const connection = await psA._libp2p.dial(psB.peerId)
|
|
150
|
-
expect(connection).to.exist()
|
|
151
|
-
expect(psA.peers.size).to.be.eql(0)
|
|
152
|
-
expect(psB.peers.size).to.be.eql(0)
|
|
153
|
-
|
|
154
222
|
await psA.start()
|
|
155
223
|
await psB.start()
|
|
156
224
|
|
|
225
|
+
await connectPeers(psA.multicodecs[0], {
|
|
226
|
+
peerId: peerA,
|
|
227
|
+
registrar: registrarA
|
|
228
|
+
}, {
|
|
229
|
+
peerId: peerB,
|
|
230
|
+
registrar: registrarB
|
|
231
|
+
})
|
|
232
|
+
|
|
157
233
|
return await Promise.all([
|
|
158
|
-
pWaitFor(() => psA.
|
|
159
|
-
pWaitFor(() => psB.
|
|
234
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
235
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
160
236
|
])
|
|
161
237
|
})
|
|
162
238
|
|
|
@@ -165,15 +241,20 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
165
241
|
const topic = 'test-topic'
|
|
166
242
|
const data = uint8ArrayFromString('hey!')
|
|
167
243
|
|
|
168
|
-
// @ts-expect-error protected fields
|
|
169
|
-
await psA._libp2p.dial(psB.peerId)
|
|
170
|
-
|
|
171
244
|
await psA.start()
|
|
172
245
|
await psB.start()
|
|
173
246
|
|
|
247
|
+
await connectPeers(psA.multicodecs[0], {
|
|
248
|
+
peerId: peerA,
|
|
249
|
+
registrar: registrarA
|
|
250
|
+
}, {
|
|
251
|
+
peerId: peerB,
|
|
252
|
+
registrar: registrarB
|
|
253
|
+
})
|
|
254
|
+
|
|
174
255
|
await Promise.all([
|
|
175
|
-
pWaitFor(() => psA.
|
|
176
|
-
pWaitFor(() => psB.
|
|
256
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
257
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
177
258
|
])
|
|
178
259
|
|
|
179
260
|
let subscribedTopics = psA.getTopics()
|
|
@@ -192,19 +273,38 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
192
273
|
// wait for psB to know about psA subscription
|
|
193
274
|
await pWaitFor(() => {
|
|
194
275
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
195
|
-
return subscribedPeers.includes(
|
|
276
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
196
277
|
})
|
|
197
|
-
void psB.
|
|
278
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
198
279
|
|
|
199
280
|
await defer.promise
|
|
200
281
|
})
|
|
201
282
|
})
|
|
202
283
|
|
|
203
284
|
describe('pubsub with intermittent connections', () => {
|
|
285
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
286
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
287
|
+
let peerA: PeerId
|
|
288
|
+
let peerB: PeerId
|
|
289
|
+
let registrarA: Registrar
|
|
290
|
+
let registrarB: Registrar
|
|
291
|
+
|
|
204
292
|
// Create pubsub nodes and start them
|
|
205
293
|
beforeEach(async () => {
|
|
206
|
-
|
|
207
|
-
|
|
294
|
+
peerA = await createEd25519PeerId()
|
|
295
|
+
peerB = await createEd25519PeerId()
|
|
296
|
+
|
|
297
|
+
registrarA = mockRegistrar()
|
|
298
|
+
registrarB = mockRegistrar()
|
|
299
|
+
|
|
300
|
+
psA = await common.setup({
|
|
301
|
+
peerId: peerA,
|
|
302
|
+
registrar: registrarA
|
|
303
|
+
})
|
|
304
|
+
psB = await common.setup({
|
|
305
|
+
peerId: peerB,
|
|
306
|
+
registrar: registrarB
|
|
307
|
+
})
|
|
208
308
|
|
|
209
309
|
await psA.start()
|
|
210
310
|
await psB.start()
|
|
@@ -219,17 +319,21 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
219
319
|
await common.teardown()
|
|
220
320
|
})
|
|
221
321
|
|
|
222
|
-
it('should receive pubsub messages after a node restart', async function () {
|
|
322
|
+
it.skip('should receive pubsub messages after a node restart', async function () {
|
|
223
323
|
const topic = 'test-topic'
|
|
224
324
|
const data = uint8ArrayFromString('hey!')
|
|
225
|
-
const psAid = psA.peerId.toString()
|
|
226
325
|
|
|
227
326
|
let counter = 0
|
|
228
327
|
const defer1 = pDefer()
|
|
229
328
|
const defer2 = pDefer()
|
|
230
329
|
|
|
231
|
-
|
|
232
|
-
|
|
330
|
+
await connectPeers(psA.multicodecs[0], {
|
|
331
|
+
peerId: peerA,
|
|
332
|
+
registrar: registrarA
|
|
333
|
+
}, {
|
|
334
|
+
peerId: peerB,
|
|
335
|
+
registrar: registrarB
|
|
336
|
+
})
|
|
233
337
|
|
|
234
338
|
let subscribedTopics = psA.getTopics()
|
|
235
339
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -248,9 +352,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
248
352
|
// wait for psB to know about psA subscription
|
|
249
353
|
await pWaitFor(() => {
|
|
250
354
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
251
|
-
return subscribedPeers.includes(
|
|
355
|
+
return subscribedPeers.map(p => p.toString()).includes(peerA.toString())
|
|
252
356
|
})
|
|
253
|
-
void psB.
|
|
357
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
254
358
|
|
|
255
359
|
await defer1.promise
|
|
256
360
|
|
|
@@ -269,23 +373,26 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
269
373
|
await psB._libp2p.start()
|
|
270
374
|
await psB.start()
|
|
271
375
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
376
|
+
await connectPeers(psA.multicodecs[0], {
|
|
377
|
+
peerId: peerA,
|
|
378
|
+
registrar: registrarA
|
|
379
|
+
}, {
|
|
380
|
+
peerId: peerB,
|
|
381
|
+
registrar: registrarB
|
|
382
|
+
})
|
|
276
383
|
|
|
277
384
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
278
385
|
await pWaitFor(() => {
|
|
279
386
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
280
|
-
return subscribedPeers.includes(
|
|
387
|
+
return subscribedPeers.toString().includes(peerA.toString())
|
|
281
388
|
})
|
|
282
389
|
|
|
283
|
-
void psB.
|
|
390
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
284
391
|
|
|
285
392
|
await defer2.promise
|
|
286
393
|
})
|
|
287
394
|
|
|
288
|
-
it('should handle quick reconnects with a delayed disconnect', async () => {
|
|
395
|
+
it.skip('should handle quick reconnects with a delayed disconnect', async () => {
|
|
289
396
|
// Subscribe on both
|
|
290
397
|
let aReceivedFirstMessageFromB = false
|
|
291
398
|
let aReceivedSecondMessageFromB = false
|
|
@@ -327,21 +434,25 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
327
434
|
// Create two connections to the remote peer
|
|
328
435
|
// @ts-expect-error protected fields
|
|
329
436
|
const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
437
|
+
|
|
330
438
|
// second connection
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
439
|
+
await connectPeers(psA.multicodecs[0], {
|
|
440
|
+
peerId: peerA,
|
|
441
|
+
registrar: registrarA
|
|
442
|
+
}, {
|
|
443
|
+
peerId: peerB,
|
|
444
|
+
registrar: registrarB
|
|
445
|
+
})
|
|
335
446
|
|
|
336
447
|
// Wait for subscriptions to occur
|
|
337
448
|
await pWaitFor(() => {
|
|
338
|
-
return psA.getSubscribers(topic).includes(
|
|
339
|
-
psB.getSubscribers(topic).includes(
|
|
449
|
+
return psA.getSubscribers(topic).includes(peerB) &&
|
|
450
|
+
psB.getSubscribers(topic).map(p => p.toString()).includes(peerA.toString())
|
|
340
451
|
})
|
|
341
452
|
|
|
342
453
|
// Verify messages go both ways
|
|
343
|
-
void psA.
|
|
344
|
-
void psB.
|
|
454
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-1') }))
|
|
455
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-1') }))
|
|
345
456
|
await pWaitFor(() => {
|
|
346
457
|
return aReceivedFirstMessageFromB && bReceivedFirstMessageFromA
|
|
347
458
|
})
|
|
@@ -354,8 +465,8 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
354
465
|
await pWaitFor(() => psAConnUpdateSpy.callCount === 1)
|
|
355
466
|
|
|
356
467
|
// Verify messages go both ways after the disconnect
|
|
357
|
-
void psA.
|
|
358
|
-
void psB.
|
|
468
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-a-2') }))
|
|
469
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('message-from-b-2') }))
|
|
359
470
|
await pWaitFor(() => {
|
|
360
471
|
return aReceivedSecondMessageFromB && bReceivedSecondMessageFromA
|
|
361
472
|
})
|