@libp2p/interface-compliance-tests 1.1.4 → 1.1.8
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 +9 -2
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +45 -21
- 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/muxer.d.ts +2 -2
- package/dist/src/mocks/muxer.d.ts.map +1 -1
- package/dist/src/mocks/muxer.js +175 -26
- package/dist/src/mocks/muxer.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +7 -2
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +54 -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 -4
- package/dist/src/mocks/upgrader.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 +15 -7
- 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 +53 -51
- 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 +12 -2
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +3 -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 +36 -32
- 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 +105 -53
- 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 +31 -17
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/stream-muxer/base-test.js +1 -1
- package/dist/src/stream-muxer/base-test.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +12 -10
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/dist/src/transport/dial-test.d.ts.map +1 -1
- package/dist/src/transport/dial-test.js +2 -1
- package/dist/src/transport/dial-test.js.map +1 -1
- package/dist/src/transport/filter-test.js +1 -1
- package/dist/src/transport/filter-test.js.map +1 -1
- package/dist/src/transport/listen-test.d.ts.map +1 -1
- package/dist/src/transport/listen-test.js +2 -1
- package/dist/src/transport/listen-test.js.map +1 -1
- package/dist/src/utils/is-valid-tick.d.ts +6 -0
- package/dist/src/utils/is-valid-tick.d.ts.map +1 -0
- package/dist/src/utils/is-valid-tick.js +15 -0
- package/dist/src/utils/is-valid-tick.js.map +1 -0
- package/package.json +7 -5
- package/src/mocks/connection.ts +63 -21
- package/src/mocks/multiaddr-connection.ts +3 -2
- package/src/mocks/muxer.ts +230 -28
- package/src/mocks/registrar.ts +69 -7
- package/src/mocks/upgrader.ts +8 -5
- package/src/pubsub/api.ts +19 -9
- package/src/pubsub/connection-handlers.ts +61 -55
- package/src/pubsub/emit-self.ts +14 -4
- package/src/pubsub/index.ts +3 -3
- package/src/pubsub/messages.ts +50 -36
- package/src/pubsub/multiple-nodes.ts +120 -55
- package/src/pubsub/two-nodes.ts +37 -22
- package/src/stream-muxer/base-test.ts +1 -1
- package/src/stream-muxer/close-test.ts +13 -12
- package/src/transport/dial-test.ts +2 -1
- package/src/transport/filter-test.ts +1 -1
- package/src/transport/listen-test.ts +2 -1
- package/src/utils/is-valid-tick.ts +18 -0
- package/dist/src/pubsub/utils.d.ts +0 -3
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -11
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/transport/utils/index.d.ts +0 -15
- package/dist/src/transport/utils/index.d.ts.map +0 -1
- package/dist/src/transport/utils/index.js +0 -137
- package/dist/src/transport/utils/index.js.map +0 -1
- package/src/pubsub/utils.ts +0 -13
- package/src/transport/utils/index.ts +0 -172
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) {
|
|
@@ -62,10 +86,48 @@ export class MockRegistrar implements Registrar {
|
|
|
62
86
|
getTopologies (protocol: string) {
|
|
63
87
|
const output: Topology[] = []
|
|
64
88
|
|
|
65
|
-
|
|
89
|
+
for (const { topology, protocols } of this.topologies.values()) {
|
|
90
|
+
if (protocols.includes(protocol)) {
|
|
91
|
+
output.push(topology)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (output.length > 0) {
|
|
96
|
+
return output
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
throw new Error(`No topologies registered for protocol ${protocol}`)
|
|
66
100
|
}
|
|
67
101
|
}
|
|
68
102
|
|
|
69
103
|
export function mockRegistrar () {
|
|
70
104
|
return new MockRegistrar()
|
|
71
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 async function connectPeers (protocol: string, registrarA: Registrar, registrarB: Registrar, peerIdA: PeerId, peerIdB: PeerId) {
|
|
120
|
+
const topologyA = registrarA.getTopologies(protocol)[0]
|
|
121
|
+
const topologyB = registrarB.getTopologies(protocol)[0]
|
|
122
|
+
// const handlerA = registrarA.getHandler(protocol)
|
|
123
|
+
// const handlerB = registrarB.getHandler(protocol)
|
|
124
|
+
|
|
125
|
+
// Notify peers of connection
|
|
126
|
+
const [bToA, aToB] = connectionPair(peerIdA, peerIdB)
|
|
127
|
+
|
|
128
|
+
await topologyA.onConnect(peerIdB, aToB)
|
|
129
|
+
// await handlerA(await mockIncomingStreamEvent(protocol, aToB, peerIdB))
|
|
130
|
+
|
|
131
|
+
await topologyB.onConnect(peerIdA, bToA)
|
|
132
|
+
// await handlerB(await mockIncomingStreamEvent(protocol, bToA, peerIdA))
|
|
133
|
+
}
|
package/src/mocks/upgrader.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { expect } from 'aegir/utils/chai.js'
|
|
2
|
-
import { mockMuxer } from './muxer.js'
|
|
3
2
|
import { mockConnection } from './connection.js'
|
|
4
3
|
import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
|
|
5
4
|
import type { Muxer } from '@libp2p/interfaces/stream-muxer'
|
|
@@ -16,16 +15,20 @@ export function mockUpgrader (options: MockUpgraderOptions = {}) {
|
|
|
16
15
|
return multiaddrConnection
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
const muxer = options.muxer ?? mockMuxer()
|
|
20
|
-
|
|
21
18
|
const upgrader: Upgrader = {
|
|
22
19
|
async upgradeOutbound (multiaddrConnection) {
|
|
23
20
|
ensureProps(multiaddrConnection)
|
|
24
|
-
return
|
|
21
|
+
return mockConnection(multiaddrConnection, {
|
|
22
|
+
direction: 'outbound',
|
|
23
|
+
muxer: options.muxer
|
|
24
|
+
})
|
|
25
25
|
},
|
|
26
26
|
async upgradeInbound (multiaddrConnection) {
|
|
27
27
|
ensureProps(multiaddrConnection)
|
|
28
|
-
return
|
|
28
|
+
return mockConnection(multiaddrConnection, {
|
|
29
|
+
direction: 'inbound',
|
|
30
|
+
muxer: options.muxer
|
|
31
|
+
})
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
package/src/pubsub/api.ts
CHANGED
|
@@ -4,19 +4,29 @@ import pDefer from 'p-defer'
|
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
6
|
import type { TestSetup } from '../index.js'
|
|
7
|
-
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
7
|
+
import type { PubSub, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
8
8
|
import type { EventMap } from './index.js'
|
|
9
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
10
|
+
import { mockRegistrar } from '../mocks/registrar.js'
|
|
11
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
9
12
|
|
|
10
13
|
const topic = 'foo'
|
|
11
14
|
const data = uint8ArrayFromString('bar')
|
|
12
15
|
|
|
13
|
-
export default (common: TestSetup<PubSub<EventMap
|
|
16
|
+
export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
14
17
|
describe('pubsub api', () => {
|
|
15
18
|
let pubsub: PubSub<EventMap>
|
|
19
|
+
let registrar: Registrar
|
|
16
20
|
|
|
17
21
|
// Create pubsub router
|
|
18
22
|
beforeEach(async () => {
|
|
19
|
-
|
|
23
|
+
registrar = mockRegistrar()
|
|
24
|
+
|
|
25
|
+
pubsub = await common.setup({
|
|
26
|
+
peerId: await createEd25519PeerId(),
|
|
27
|
+
registrar,
|
|
28
|
+
emitSelf: true
|
|
29
|
+
})
|
|
20
30
|
})
|
|
21
31
|
|
|
22
32
|
afterEach(async () => {
|
|
@@ -26,22 +36,22 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
26
36
|
})
|
|
27
37
|
|
|
28
38
|
it('can start correctly', async () => {
|
|
29
|
-
sinon.spy(
|
|
39
|
+
sinon.spy(registrar, 'register')
|
|
30
40
|
|
|
31
41
|
await pubsub.start()
|
|
32
42
|
|
|
33
|
-
expect(pubsub.
|
|
34
|
-
expect(
|
|
43
|
+
expect(pubsub.isStarted()).to.equal(true)
|
|
44
|
+
expect(registrar.register).to.have.property('callCount', 1)
|
|
35
45
|
})
|
|
36
46
|
|
|
37
47
|
it('can stop correctly', async () => {
|
|
38
|
-
sinon.spy(
|
|
48
|
+
sinon.spy(registrar, 'unregister')
|
|
39
49
|
|
|
40
50
|
await pubsub.start()
|
|
41
51
|
await pubsub.stop()
|
|
42
52
|
|
|
43
|
-
expect(pubsub.
|
|
44
|
-
expect(
|
|
53
|
+
expect(pubsub.isStarted()).to.equal(false)
|
|
54
|
+
expect(registrar.unregister).to.have.property('callCount', 1)
|
|
45
55
|
})
|
|
46
56
|
|
|
47
57
|
it('can subscribe and unsubscribe correctly', async () => {
|
|
@@ -4,28 +4,47 @@ 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 { expectSet } from './utils.js'
|
|
8
7
|
import type { TestSetup } from '../index.js'
|
|
9
|
-
import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
|
|
8
|
+
import type { PubSub, Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
10
9
|
import type { EventMap } from './index.js'
|
|
10
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
11
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
12
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
13
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
11
14
|
|
|
12
|
-
export default (common: TestSetup<PubSub<EventMap
|
|
15
|
+
export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
13
16
|
describe('pubsub connection handlers', () => {
|
|
14
17
|
let psA: PubSub<EventMap>
|
|
15
18
|
let psB: PubSub<EventMap>
|
|
19
|
+
let peerA: PeerId
|
|
20
|
+
let peerB: PeerId
|
|
21
|
+
let registrarA: Registrar
|
|
22
|
+
let registrarB: Registrar
|
|
16
23
|
|
|
17
24
|
describe('nodes send state on connection', () => {
|
|
18
25
|
// Create pubsub nodes and connect them
|
|
19
26
|
before(async () => {
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
peerA = await createEd25519PeerId()
|
|
28
|
+
peerB = await createEd25519PeerId()
|
|
29
|
+
|
|
30
|
+
registrarA = mockRegistrar()
|
|
31
|
+
registrarB = mockRegistrar()
|
|
22
32
|
|
|
23
|
-
|
|
24
|
-
|
|
33
|
+
psA = await common.setup({
|
|
34
|
+
peerId: peerA,
|
|
35
|
+
registrar: registrarA
|
|
36
|
+
})
|
|
37
|
+
psB = await common.setup({
|
|
38
|
+
peerId: peerB,
|
|
39
|
+
registrar: registrarB
|
|
40
|
+
})
|
|
25
41
|
|
|
26
42
|
// Start pubsub
|
|
27
43
|
await psA.start()
|
|
28
44
|
await psB.start()
|
|
45
|
+
|
|
46
|
+
expect(psA.getPeers()).to.be.empty()
|
|
47
|
+
expect(psB.getPeers()).to.be.empty()
|
|
29
48
|
})
|
|
30
49
|
|
|
31
50
|
// Make subscriptions prior to nodes connected
|
|
@@ -33,10 +52,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
33
52
|
psA.subscribe('Za')
|
|
34
53
|
psB.subscribe('Zb')
|
|
35
54
|
|
|
36
|
-
expect(psA.
|
|
37
|
-
|
|
38
|
-
expect(psB.
|
|
39
|
-
|
|
55
|
+
expect(psA.getPeers()).to.be.empty()
|
|
56
|
+
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
57
|
+
expect(psB.getPeers()).to.be.empty()
|
|
58
|
+
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
40
59
|
})
|
|
41
60
|
|
|
42
61
|
after(async () => {
|
|
@@ -45,9 +64,9 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
45
64
|
})
|
|
46
65
|
|
|
47
66
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
67
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
68
|
+
|
|
48
69
|
await Promise.all([
|
|
49
|
-
// @ts-expect-error protected fields
|
|
50
|
-
psA._libp2p.dial(psB.peerId),
|
|
51
70
|
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
52
71
|
once: true
|
|
53
72
|
})),
|
|
@@ -56,16 +75,14 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
56
75
|
}))
|
|
57
76
|
])
|
|
58
77
|
|
|
59
|
-
expect(psA.
|
|
60
|
-
expect(psB.
|
|
61
|
-
|
|
62
|
-
expectSet(psA.subscriptions, ['Za'])
|
|
78
|
+
expect(psA.getPeers()).to.have.lengthOf(1)
|
|
79
|
+
expect(psB.getPeers()).to.have.lengthOf(1)
|
|
63
80
|
|
|
64
|
-
|
|
81
|
+
expect(psA.getTopics()).to.deep.equal(['Za'])
|
|
82
|
+
expect(psB.getTopics()).to.deep.equal(['Zb'])
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
expectSet(psA.topics.get('Zb'), [psB.peerId.toString()])
|
|
84
|
+
expect(psA.getSubscribers('Zb')).to.deep.equal([peerB])
|
|
85
|
+
expect(psB.getSubscribers('Za')).to.deep.equal([peerA])
|
|
69
86
|
})
|
|
70
87
|
})
|
|
71
88
|
|
|
@@ -86,13 +103,11 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
86
103
|
})
|
|
87
104
|
|
|
88
105
|
it('should get notified of connected peers on dial', async () => {
|
|
89
|
-
|
|
90
|
-
const connection = await psA._libp2p.dial(psB.peerId)
|
|
91
|
-
expect(connection).to.exist()
|
|
106
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
92
107
|
|
|
93
108
|
return await Promise.all([
|
|
94
|
-
pWaitFor(() => psA.
|
|
95
|
-
pWaitFor(() => psB.
|
|
109
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
110
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
96
111
|
])
|
|
97
112
|
})
|
|
98
113
|
|
|
@@ -101,8 +116,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
101
116
|
const topic = 'test-topic'
|
|
102
117
|
const data = uint8ArrayFromString('hey!')
|
|
103
118
|
|
|
104
|
-
|
|
105
|
-
await psA._libp2p.dial(psB.peerId)
|
|
119
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
106
120
|
|
|
107
121
|
let subscribedTopics = psA.getTopics()
|
|
108
122
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -120,7 +134,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
120
134
|
// wait for psB to know about psA subscription
|
|
121
135
|
await pWaitFor(() => {
|
|
122
136
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
123
|
-
return subscribedPeers.includes(
|
|
137
|
+
return subscribedPeers.includes(peerA)
|
|
124
138
|
})
|
|
125
139
|
void psB.publish(topic, data)
|
|
126
140
|
|
|
@@ -148,15 +162,15 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
148
162
|
// @ts-expect-error protected fields
|
|
149
163
|
const connection = await psA._libp2p.dial(psB.peerId)
|
|
150
164
|
expect(connection).to.exist()
|
|
151
|
-
expect(psA.
|
|
152
|
-
expect(psB.
|
|
165
|
+
expect(psA.getPeers()).to.be.empty()
|
|
166
|
+
expect(psB.getPeers()).to.be.empty()
|
|
153
167
|
|
|
154
168
|
await psA.start()
|
|
155
169
|
await psB.start()
|
|
156
170
|
|
|
157
171
|
return await Promise.all([
|
|
158
|
-
pWaitFor(() => psA.
|
|
159
|
-
pWaitFor(() => psB.
|
|
172
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
173
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
160
174
|
])
|
|
161
175
|
})
|
|
162
176
|
|
|
@@ -165,15 +179,14 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
165
179
|
const topic = 'test-topic'
|
|
166
180
|
const data = uint8ArrayFromString('hey!')
|
|
167
181
|
|
|
168
|
-
|
|
169
|
-
await psA._libp2p.dial(psB.peerId)
|
|
182
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
170
183
|
|
|
171
184
|
await psA.start()
|
|
172
185
|
await psB.start()
|
|
173
186
|
|
|
174
187
|
await Promise.all([
|
|
175
|
-
pWaitFor(() => psA.
|
|
176
|
-
pWaitFor(() => psB.
|
|
188
|
+
pWaitFor(() => psA.getPeers().length === 1),
|
|
189
|
+
pWaitFor(() => psB.getPeers().length === 1)
|
|
177
190
|
])
|
|
178
191
|
|
|
179
192
|
let subscribedTopics = psA.getTopics()
|
|
@@ -192,7 +205,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
192
205
|
// wait for psB to know about psA subscription
|
|
193
206
|
await pWaitFor(() => {
|
|
194
207
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
195
|
-
return subscribedPeers.includes(
|
|
208
|
+
return subscribedPeers.includes(peerA)
|
|
196
209
|
})
|
|
197
210
|
void psB.publish(topic, data)
|
|
198
211
|
|
|
@@ -219,17 +232,15 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
219
232
|
await common.teardown()
|
|
220
233
|
})
|
|
221
234
|
|
|
222
|
-
it('should receive pubsub messages after a node restart', async function () {
|
|
235
|
+
it.skip('should receive pubsub messages after a node restart', async function () {
|
|
223
236
|
const topic = 'test-topic'
|
|
224
237
|
const data = uint8ArrayFromString('hey!')
|
|
225
|
-
const psAid = psA.peerId.toString()
|
|
226
238
|
|
|
227
239
|
let counter = 0
|
|
228
240
|
const defer1 = pDefer()
|
|
229
241
|
const defer2 = pDefer()
|
|
230
242
|
|
|
231
|
-
|
|
232
|
-
await psA._libp2p.dial(psB.peerId)
|
|
243
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
233
244
|
|
|
234
245
|
let subscribedTopics = psA.getTopics()
|
|
235
246
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -248,7 +259,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
248
259
|
// wait for psB to know about psA subscription
|
|
249
260
|
await pWaitFor(() => {
|
|
250
261
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
251
|
-
return subscribedPeers.includes(
|
|
262
|
+
return subscribedPeers.includes(peerA)
|
|
252
263
|
})
|
|
253
264
|
void psB.publish(topic, data)
|
|
254
265
|
|
|
@@ -269,15 +280,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
269
280
|
await psB._libp2p.start()
|
|
270
281
|
await psB.start()
|
|
271
282
|
|
|
272
|
-
|
|
273
|
-
psA._libp2p.peerStore.addressBook.set(psB.peerId, psB._libp2p.multiaddrs)
|
|
274
|
-
// @ts-expect-error protected fields
|
|
275
|
-
await psA._libp2p.dial(psB.peerId)
|
|
283
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
276
284
|
|
|
277
285
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
278
286
|
await pWaitFor(() => {
|
|
279
287
|
const subscribedPeers = psB.getSubscribers(topic)
|
|
280
|
-
return subscribedPeers.includes(
|
|
288
|
+
return subscribedPeers.includes(peerA)
|
|
281
289
|
})
|
|
282
290
|
|
|
283
291
|
void psB.publish(topic, data)
|
|
@@ -285,7 +293,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
285
293
|
await defer2.promise
|
|
286
294
|
})
|
|
287
295
|
|
|
288
|
-
it('should handle quick reconnects with a delayed disconnect', async () => {
|
|
296
|
+
it.skip('should handle quick reconnects with a delayed disconnect', async () => {
|
|
289
297
|
// Subscribe on both
|
|
290
298
|
let aReceivedFirstMessageFromB = false
|
|
291
299
|
let aReceivedSecondMessageFromB = false
|
|
@@ -327,16 +335,14 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
327
335
|
// Create two connections to the remote peer
|
|
328
336
|
// @ts-expect-error protected fields
|
|
329
337
|
const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
338
|
+
|
|
330
339
|
// second connection
|
|
331
|
-
|
|
332
|
-
await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
333
|
-
// @ts-expect-error protected fields
|
|
334
|
-
expect(psA._libp2p.connections.get(psB.peerId.toString())).to.have.length(2)
|
|
340
|
+
await connectPeers(psA.multicodecs[0], registrarA, registrarB, peerA, peerB)
|
|
335
341
|
|
|
336
342
|
// Wait for subscriptions to occur
|
|
337
343
|
await pWaitFor(() => {
|
|
338
|
-
return psA.getSubscribers(topic).includes(
|
|
339
|
-
psB.getSubscribers(topic).includes(
|
|
344
|
+
return psA.getSubscribers(topic).includes(peerB) &&
|
|
345
|
+
psB.getSubscribers(topic).includes(peerA)
|
|
340
346
|
})
|
|
341
347
|
|
|
342
348
|
// Verify messages go both ways
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -2,20 +2,26 @@ import { expect } from 'aegir/utils/chai.js'
|
|
|
2
2
|
import sinon from 'sinon'
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import type { TestSetup } from '../index.js'
|
|
5
|
-
import type { PubSub,
|
|
5
|
+
import type { PubSub, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
6
6
|
import type { EventMap } from './index.js'
|
|
7
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
8
|
+
import { mockRegistrar } from '../mocks/registrar.js'
|
|
7
9
|
|
|
8
10
|
const topic = 'foo'
|
|
9
11
|
const data = uint8ArrayFromString('bar')
|
|
10
12
|
const shouldNotHappen = () => expect.fail()
|
|
11
13
|
|
|
12
|
-
export default (common: TestSetup<PubSub<EventMap>,
|
|
14
|
+
export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
13
15
|
describe('emit self', () => {
|
|
14
16
|
let pubsub: PubSub<EventMap>
|
|
15
17
|
|
|
16
18
|
describe('enabled', () => {
|
|
17
19
|
before(async () => {
|
|
18
|
-
pubsub = await common.setup({
|
|
20
|
+
pubsub = await common.setup({
|
|
21
|
+
peerId: await createEd25519PeerId(),
|
|
22
|
+
registrar: mockRegistrar(),
|
|
23
|
+
emitSelf: true
|
|
24
|
+
})
|
|
19
25
|
})
|
|
20
26
|
|
|
21
27
|
before(async () => {
|
|
@@ -42,7 +48,11 @@ export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) =>
|
|
|
42
48
|
|
|
43
49
|
describe('disabled', () => {
|
|
44
50
|
before(async () => {
|
|
45
|
-
pubsub = await common.setup({
|
|
51
|
+
pubsub = await common.setup({
|
|
52
|
+
peerId: await createEd25519PeerId(),
|
|
53
|
+
registrar: mockRegistrar(),
|
|
54
|
+
emitSelf: false
|
|
55
|
+
})
|
|
46
56
|
})
|
|
47
57
|
|
|
48
58
|
before(async () => {
|
package/src/pubsub/index.ts
CHANGED
|
@@ -5,9 +5,9 @@ import connectionHandlersTest from './connection-handlers.js'
|
|
|
5
5
|
import twoNodesTest from './two-nodes.js'
|
|
6
6
|
import multipleNodesTest from './multiple-nodes.js'
|
|
7
7
|
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type { PubSub, Message,
|
|
8
|
+
import type { PubSub, Message, PubSubEvents, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
9
9
|
|
|
10
|
-
export interface EventMap extends
|
|
10
|
+
export interface EventMap extends PubSubEvents {
|
|
11
11
|
'topic': CustomEvent<Message>
|
|
12
12
|
'foo': CustomEvent<Message>
|
|
13
13
|
'test-topic': CustomEvent<Message>
|
|
@@ -15,7 +15,7 @@ export interface EventMap extends PubsubEvents {
|
|
|
15
15
|
'Z': CustomEvent<Message>
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export default (common: TestSetup<PubSub<EventMap
|
|
18
|
+
export default (common: TestSetup<PubSub<EventMap>, PubSubOptions>) => {
|
|
19
19
|
describe('interface-pubsub compliance tests', () => {
|
|
20
20
|
apiTest(common)
|
|
21
21
|
emitSelfTest(common)
|