@libp2p/interface-compliance-tests 1.1.6 → 1.1.10
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 +13 -2
- package/dist/src/mocks/connection.d.ts.map +1 -1
- package/dist/src/mocks/connection.js +56 -24
- 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 +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 -4
- 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 +163 -65
- 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 +80 -38
- 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 +80 -24
- package/src/mocks/multiaddr-connection.ts +3 -2
- package/src/mocks/muxer.ts +230 -28
- package/src/mocks/registrar.ts +65 -7
- package/src/mocks/upgrader.ts +8 -5
- 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 +189 -76
- package/src/pubsub/two-nodes.ts +94 -46
- 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/pubsub/messages.ts
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
import { expect } from 'aegir/utils/chai.js'
|
|
2
2
|
import sinon from 'sinon'
|
|
3
|
-
import
|
|
3
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
|
-
import
|
|
5
|
+
import { noSignMsgId } from '@libp2p/pubsub/utils'
|
|
6
6
|
import { PeerStreams } from '@libp2p/pubsub/peer-streams'
|
|
7
|
+
import { mockRegistrar } from '../mocks/registrar.js'
|
|
8
|
+
import pDefer from 'p-defer'
|
|
9
|
+
import delay from 'delay'
|
|
10
|
+
import pWaitFor from 'p-wait-for'
|
|
11
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
7
12
|
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type {
|
|
13
|
+
import type { PubSubOptions, RPC } from '@libp2p/interfaces/pubsub'
|
|
9
14
|
import type { EventMap } from './index.js'
|
|
15
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
10
16
|
|
|
11
17
|
const topic = 'foo'
|
|
12
18
|
const data = uint8ArrayFromString('bar')
|
|
13
19
|
|
|
14
|
-
export default (common: TestSetup<
|
|
20
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
15
21
|
describe('messages', () => {
|
|
16
|
-
let pubsub:
|
|
22
|
+
let pubsub: PubsubBaseProtocol<EventMap>
|
|
17
23
|
|
|
18
24
|
// Create pubsub router
|
|
19
25
|
beforeEach(async () => {
|
|
20
|
-
pubsub = await common.setup(
|
|
26
|
+
pubsub = await common.setup({
|
|
27
|
+
peerId: await createEd25519PeerId(),
|
|
28
|
+
registrar: mockRegistrar(),
|
|
29
|
+
emitSelf: true
|
|
30
|
+
})
|
|
21
31
|
await pubsub.start()
|
|
22
32
|
})
|
|
23
33
|
|
|
@@ -29,83 +39,88 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
29
39
|
|
|
30
40
|
it('should emit normalized signed messages on publish', async () => {
|
|
31
41
|
pubsub.globalSignaturePolicy = 'StrictSign'
|
|
32
|
-
// @ts-expect-error protected field
|
|
33
|
-
sinon.spy(pubsub, '_emitMessage')
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
const spy = sinon.spy(pubsub, 'publishMessage')
|
|
44
|
+
|
|
45
|
+
await pubsub.dispatchEvent(new CustomEvent(topic, { detail: data }))
|
|
46
|
+
|
|
47
|
+
await pWaitFor(async () => {
|
|
48
|
+
return spy.callCount === 1
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
expect(spy).to.have.property('callCount', 1)
|
|
52
|
+
|
|
53
|
+
const [from, messageToEmit] = spy.getCall(0).args
|
|
40
54
|
|
|
55
|
+
expect(from.toString()).to.equal(pubsub.peerId.toString())
|
|
41
56
|
expect(messageToEmit.seqno).to.not.eql(undefined)
|
|
42
57
|
expect(messageToEmit.key).to.not.eql(undefined)
|
|
43
58
|
expect(messageToEmit.signature).to.not.eql(undefined)
|
|
44
59
|
})
|
|
45
60
|
|
|
46
61
|
it('should drop unsigned messages', async () => {
|
|
47
|
-
|
|
48
|
-
sinon.spy(pubsub, '_emitMessage')
|
|
49
|
-
// @ts-expect-error protected field
|
|
50
|
-
sinon.spy(pubsub, '_publish')
|
|
62
|
+
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
51
63
|
sinon.spy(pubsub, 'validate')
|
|
52
64
|
|
|
53
65
|
const peerStream = new PeerStreams({
|
|
54
|
-
id: await
|
|
66
|
+
id: await createEd25519PeerId(),
|
|
55
67
|
protocol: 'test'
|
|
56
68
|
})
|
|
57
|
-
const rpc = {
|
|
69
|
+
const rpc: RPC = {
|
|
58
70
|
subscriptions: [],
|
|
59
|
-
|
|
60
|
-
receivedFrom: peerStream.id.toString(),
|
|
71
|
+
messages: [{
|
|
61
72
|
from: peerStream.id.toBytes(),
|
|
62
73
|
data,
|
|
63
|
-
seqno:
|
|
64
|
-
|
|
74
|
+
seqno: await noSignMsgId(data),
|
|
75
|
+
topic: topic
|
|
65
76
|
}]
|
|
66
77
|
}
|
|
67
78
|
|
|
68
79
|
pubsub.subscribe(topic)
|
|
69
|
-
// @ts-expect-error protected field
|
|
70
|
-
await pubsub._processRpc(peerStream.id.toString(), peerStream, rpc)
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
82
|
+
|
|
83
|
+
// message should not be delivered
|
|
84
|
+
await delay(1000)
|
|
85
|
+
|
|
86
|
+
expect(publishSpy).to.have.property('called', false)
|
|
77
87
|
})
|
|
78
88
|
|
|
79
89
|
it('should not drop unsigned messages if strict signing is disabled', async () => {
|
|
80
90
|
pubsub.globalSignaturePolicy = 'StrictNoSign'
|
|
81
|
-
|
|
82
|
-
sinon.spy(pubsub, '
|
|
83
|
-
// @ts-expect-error protected field
|
|
84
|
-
sinon.spy(pubsub, '_publish')
|
|
91
|
+
|
|
92
|
+
const publishSpy = sinon.spy(pubsub, 'publishMessage')
|
|
85
93
|
sinon.spy(pubsub, 'validate')
|
|
86
94
|
|
|
87
95
|
const peerStream = new PeerStreams({
|
|
88
|
-
id: await
|
|
96
|
+
id: await createEd25519PeerId(),
|
|
89
97
|
protocol: 'test'
|
|
90
98
|
})
|
|
91
99
|
|
|
92
|
-
const rpc = {
|
|
100
|
+
const rpc: RPC = {
|
|
93
101
|
subscriptions: [],
|
|
94
|
-
|
|
102
|
+
messages: [{
|
|
103
|
+
from: peerStream.id.toBytes(),
|
|
95
104
|
data,
|
|
96
|
-
|
|
105
|
+
topic
|
|
97
106
|
}]
|
|
98
107
|
}
|
|
99
108
|
|
|
100
109
|
pubsub.subscribe(topic)
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
|
|
111
|
+
const deferred = pDefer()
|
|
112
|
+
|
|
113
|
+
pubsub.addEventListener(topic, () => {
|
|
114
|
+
deferred.resolve()
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
await pubsub.processRpc(peerStream.id, peerStream, rpc)
|
|
118
|
+
|
|
119
|
+
// await message delivery
|
|
120
|
+
await deferred.promise
|
|
103
121
|
|
|
104
122
|
expect(pubsub.validate).to.have.property('callCount', 1)
|
|
105
|
-
|
|
106
|
-
expect(pubsub._emitMessage).to.have.property('called', 1)
|
|
107
|
-
// @ts-expect-error protected field
|
|
108
|
-
expect(pubsub._publish).to.have.property('called', 1)
|
|
123
|
+
expect(publishSpy).to.have.property('callCount', 1)
|
|
109
124
|
})
|
|
110
125
|
})
|
|
111
126
|
}
|
|
@@ -6,51 +6,97 @@ import pDefer from 'p-defer'
|
|
|
6
6
|
import pWaitFor from 'p-wait-for'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
8
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
9
|
-
import {
|
|
9
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
10
|
+
import { connectPeers, mockRegistrar } from '../mocks/registrar.js'
|
|
11
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
10
12
|
import type { TestSetup } from '../index.js'
|
|
11
|
-
import type {
|
|
13
|
+
import type { Message, PubSubOptions } from '@libp2p/interfaces/pubsub'
|
|
12
14
|
import type { EventMap } from './index.js'
|
|
15
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
16
|
+
import type { Registrar } from '@libp2p/interfaces/src/registrar'
|
|
17
|
+
import type { PubsubBaseProtocol } from '@libp2p/pubsub'
|
|
13
18
|
|
|
14
|
-
export default (common: TestSetup<
|
|
19
|
+
export default (common: TestSetup<PubsubBaseProtocol<EventMap>, PubSubOptions>) => {
|
|
15
20
|
describe('pubsub with multiple nodes', function () {
|
|
16
21
|
describe('every peer subscribes to the topic', () => {
|
|
17
22
|
describe('line', () => {
|
|
18
23
|
// line
|
|
19
24
|
// ◉────◉────◉
|
|
20
25
|
// a b c
|
|
21
|
-
let psA:
|
|
22
|
-
let psB:
|
|
23
|
-
let psC:
|
|
26
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
27
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
28
|
+
let psC: PubsubBaseProtocol<EventMap>
|
|
29
|
+
let peerIdA: PeerId
|
|
30
|
+
let peerIdB: PeerId
|
|
31
|
+
let peerIdC: PeerId
|
|
32
|
+
let registrarA: Registrar
|
|
33
|
+
let registrarB: Registrar
|
|
34
|
+
let registrarC: Registrar
|
|
24
35
|
|
|
25
36
|
// Create and start pubsub nodes
|
|
26
37
|
beforeEach(async () => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
peerIdA = await createEd25519PeerId()
|
|
39
|
+
peerIdB = await createEd25519PeerId()
|
|
40
|
+
peerIdC = await createEd25519PeerId()
|
|
41
|
+
|
|
42
|
+
registrarA = mockRegistrar()
|
|
43
|
+
registrarB = mockRegistrar()
|
|
44
|
+
registrarC = mockRegistrar()
|
|
45
|
+
|
|
46
|
+
psA = await common.setup({
|
|
47
|
+
peerId: peerIdA,
|
|
48
|
+
registrar: registrarA,
|
|
49
|
+
emitSelf: true
|
|
50
|
+
})
|
|
51
|
+
psB = await common.setup({
|
|
52
|
+
peerId: peerIdB,
|
|
53
|
+
registrar: registrarB,
|
|
54
|
+
emitSelf: true
|
|
55
|
+
})
|
|
56
|
+
psC = await common.setup({
|
|
57
|
+
peerId: peerIdC,
|
|
58
|
+
registrar: registrarC,
|
|
59
|
+
emitSelf: true
|
|
60
|
+
})
|
|
30
61
|
|
|
31
62
|
// Start pubsub modes
|
|
32
|
-
|
|
63
|
+
await Promise.all(
|
|
64
|
+
[psA, psB, psC].map(async (p) => await p.start())
|
|
65
|
+
)
|
|
33
66
|
})
|
|
34
67
|
|
|
35
68
|
// Connect nodes
|
|
36
69
|
beforeEach(async () => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
70
|
+
await connectPeers(psA.multicodecs[0], {
|
|
71
|
+
peerId: peerIdA,
|
|
72
|
+
registrar: registrarA
|
|
73
|
+
}, {
|
|
74
|
+
peerId: peerIdB,
|
|
75
|
+
registrar: registrarB
|
|
76
|
+
})
|
|
77
|
+
await connectPeers(psA.multicodecs[0], {
|
|
78
|
+
peerId: peerIdB,
|
|
79
|
+
registrar: registrarB
|
|
80
|
+
}, {
|
|
81
|
+
peerId: peerIdC,
|
|
82
|
+
registrar: registrarC
|
|
83
|
+
})
|
|
41
84
|
|
|
42
85
|
// Wait for peers to be ready in pubsub
|
|
43
86
|
await pWaitFor(() =>
|
|
44
|
-
psA.
|
|
45
|
-
psC.
|
|
46
|
-
psA.
|
|
87
|
+
psA.getPeers().length === 1 &&
|
|
88
|
+
psC.getPeers().length === 1 &&
|
|
89
|
+
psA.getPeers().length === 1
|
|
47
90
|
)
|
|
48
91
|
})
|
|
49
92
|
|
|
50
93
|
afterEach(async () => {
|
|
51
94
|
sinon.restore()
|
|
52
95
|
|
|
53
|
-
|
|
96
|
+
await Promise.all(
|
|
97
|
+
[psA, psB, psC].map(async (p) => await p.stop())
|
|
98
|
+
)
|
|
99
|
+
|
|
54
100
|
await common.teardown()
|
|
55
101
|
})
|
|
56
102
|
|
|
@@ -58,24 +104,23 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
58
104
|
const topic = 'Z'
|
|
59
105
|
|
|
60
106
|
psA.subscribe(topic)
|
|
61
|
-
|
|
107
|
+
expect(psA.getTopics()).to.deep.equal([topic])
|
|
62
108
|
|
|
63
109
|
await new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
|
|
64
110
|
once: true
|
|
65
111
|
}))
|
|
66
|
-
expect(psB.
|
|
112
|
+
expect(psB.getPeers().length).to.equal(2)
|
|
67
113
|
|
|
68
|
-
|
|
69
|
-
expectSet(psB.topics.get(topic), [aPeerId])
|
|
114
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdA.toString()])
|
|
70
115
|
|
|
71
|
-
expect(psC.
|
|
72
|
-
expect(psC.
|
|
116
|
+
expect(psC.getPeers().length).to.equal(1)
|
|
117
|
+
expect(psC.getSubscribers(topic)).to.be.empty()
|
|
73
118
|
})
|
|
74
119
|
|
|
75
120
|
it('subscribe to the topic on node b', async () => {
|
|
76
121
|
const topic = 'Z'
|
|
77
122
|
psB.subscribe(topic)
|
|
78
|
-
|
|
123
|
+
expect(psB.getTopics()).to.deep.equal([topic])
|
|
79
124
|
|
|
80
125
|
await Promise.all([
|
|
81
126
|
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
@@ -86,11 +131,11 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
86
131
|
}))
|
|
87
132
|
])
|
|
88
133
|
|
|
89
|
-
expect(psA.
|
|
90
|
-
|
|
134
|
+
expect(psA.getPeers().length).to.equal(1)
|
|
135
|
+
expect(psA.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
91
136
|
|
|
92
|
-
expect(psC.
|
|
93
|
-
|
|
137
|
+
expect(psC.getPeers().length).to.equal(1)
|
|
138
|
+
expect(psC.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdB.toString()])
|
|
94
139
|
})
|
|
95
140
|
|
|
96
141
|
it('subscribe to the topic on node c', async () => {
|
|
@@ -98,12 +143,12 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
98
143
|
const defer = pDefer()
|
|
99
144
|
|
|
100
145
|
psC.subscribe(topic)
|
|
101
|
-
|
|
146
|
+
expect(psC.getTopics()).to.deep.equal([topic])
|
|
102
147
|
|
|
103
148
|
psB.addEventListener('pubsub:subscription-change', () => {
|
|
104
|
-
expect(psA.
|
|
105
|
-
expect(psB.
|
|
106
|
-
|
|
149
|
+
expect(psA.getPeers().length).to.equal(1)
|
|
150
|
+
expect(psB.getPeers().length).to.equal(2)
|
|
151
|
+
expect(psB.getSubscribers(topic).map(p => p.toString())).to.deep.equal([peerIdC.toString()])
|
|
107
152
|
|
|
108
153
|
defer.resolve()
|
|
109
154
|
}, {
|
|
@@ -117,25 +162,24 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
117
162
|
const topic = 'Z'
|
|
118
163
|
const defer = pDefer()
|
|
119
164
|
|
|
120
|
-
psA.subscribe(topic)
|
|
121
|
-
psB.subscribe(topic)
|
|
122
|
-
psC.subscribe(topic)
|
|
123
|
-
|
|
124
165
|
// await subscription change
|
|
125
|
-
|
|
126
|
-
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(
|
|
166
|
+
const p = Promise.all([
|
|
167
|
+
new Promise<void>(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
127
168
|
once: true
|
|
128
169
|
})),
|
|
129
|
-
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(
|
|
170
|
+
new Promise<void>(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
130
171
|
once: true
|
|
131
172
|
})),
|
|
132
|
-
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(
|
|
173
|
+
new Promise<void>(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(), {
|
|
133
174
|
once: true
|
|
134
175
|
}))
|
|
135
176
|
])
|
|
136
177
|
|
|
137
|
-
|
|
138
|
-
|
|
178
|
+
psA.subscribe(topic)
|
|
179
|
+
psB.subscribe(topic)
|
|
180
|
+
psC.subscribe(topic)
|
|
181
|
+
|
|
182
|
+
await p
|
|
139
183
|
|
|
140
184
|
let counter = 0
|
|
141
185
|
|
|
@@ -143,7 +187,10 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
143
187
|
psB.addEventListener(topic, incMsg)
|
|
144
188
|
psC.addEventListener(topic, incMsg)
|
|
145
189
|
|
|
146
|
-
|
|
190
|
+
// await a cycle
|
|
191
|
+
await delay(1000)
|
|
192
|
+
|
|
193
|
+
void psA.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
147
194
|
|
|
148
195
|
function incMsg (evt: CustomEvent<Message>) {
|
|
149
196
|
const msg = evt.detail
|
|
@@ -201,7 +248,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
201
248
|
// await a cycle
|
|
202
249
|
await delay(1000)
|
|
203
250
|
|
|
204
|
-
void psB.
|
|
251
|
+
void psB.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('hey') }))
|
|
205
252
|
|
|
206
253
|
function incMsg (evt: CustomEvent<Message>) {
|
|
207
254
|
const msg = evt.detail
|
|
@@ -231,61 +278,127 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
231
278
|
// │b d│
|
|
232
279
|
// ◉─┘ └─◉
|
|
233
280
|
// a
|
|
234
|
-
let psA:
|
|
235
|
-
let psB:
|
|
236
|
-
let psC:
|
|
237
|
-
let psD:
|
|
238
|
-
let psE:
|
|
281
|
+
let psA: PubsubBaseProtocol<EventMap>
|
|
282
|
+
let psB: PubsubBaseProtocol<EventMap>
|
|
283
|
+
let psC: PubsubBaseProtocol<EventMap>
|
|
284
|
+
let psD: PubsubBaseProtocol<EventMap>
|
|
285
|
+
let psE: PubsubBaseProtocol<EventMap>
|
|
286
|
+
let peerIdA: PeerId
|
|
287
|
+
let peerIdB: PeerId
|
|
288
|
+
let peerIdC: PeerId
|
|
289
|
+
let peerIdD: PeerId
|
|
290
|
+
let peerIdE: PeerId
|
|
291
|
+
let registrarA: Registrar
|
|
292
|
+
let registrarB: Registrar
|
|
293
|
+
let registrarC: Registrar
|
|
294
|
+
let registrarD: Registrar
|
|
295
|
+
let registrarE: Registrar
|
|
239
296
|
|
|
240
297
|
// Create and start pubsub nodes
|
|
241
298
|
beforeEach(async () => {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
299
|
+
peerIdA = await createEd25519PeerId()
|
|
300
|
+
peerIdB = await createEd25519PeerId()
|
|
301
|
+
peerIdC = await createEd25519PeerId()
|
|
302
|
+
peerIdD = await createEd25519PeerId()
|
|
303
|
+
peerIdE = await createEd25519PeerId()
|
|
304
|
+
|
|
305
|
+
registrarA = mockRegistrar()
|
|
306
|
+
registrarB = mockRegistrar()
|
|
307
|
+
registrarC = mockRegistrar()
|
|
308
|
+
registrarD = mockRegistrar()
|
|
309
|
+
registrarE = mockRegistrar()
|
|
310
|
+
|
|
311
|
+
psA = await common.setup({
|
|
312
|
+
peerId: peerIdA,
|
|
313
|
+
registrar: registrarA,
|
|
314
|
+
emitSelf: true
|
|
315
|
+
})
|
|
316
|
+
psB = await common.setup({
|
|
317
|
+
peerId: peerIdB,
|
|
318
|
+
registrar: registrarB,
|
|
319
|
+
emitSelf: true
|
|
320
|
+
})
|
|
321
|
+
psC = await common.setup({
|
|
322
|
+
peerId: peerIdC,
|
|
323
|
+
registrar: registrarC,
|
|
324
|
+
emitSelf: true
|
|
325
|
+
})
|
|
326
|
+
psD = await common.setup({
|
|
327
|
+
peerId: peerIdD,
|
|
328
|
+
registrar: registrarD,
|
|
329
|
+
emitSelf: true
|
|
330
|
+
})
|
|
331
|
+
psE = await common.setup({
|
|
332
|
+
peerId: peerIdE,
|
|
333
|
+
registrar: registrarE,
|
|
334
|
+
emitSelf: true
|
|
335
|
+
})
|
|
247
336
|
|
|
248
337
|
// Start pubsub nodes
|
|
249
|
-
|
|
338
|
+
await Promise.all(
|
|
339
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.start())
|
|
340
|
+
)
|
|
250
341
|
})
|
|
251
342
|
|
|
252
343
|
// connect nodes
|
|
253
344
|
beforeEach(async () => {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
await
|
|
345
|
+
await connectPeers(psA.multicodecs[0], {
|
|
346
|
+
peerId: peerIdA,
|
|
347
|
+
registrar: registrarA
|
|
348
|
+
}, {
|
|
349
|
+
peerId: peerIdB,
|
|
350
|
+
registrar: registrarB
|
|
351
|
+
})
|
|
352
|
+
await connectPeers(psA.multicodecs[0], {
|
|
353
|
+
peerId: peerIdB,
|
|
354
|
+
registrar: registrarB
|
|
355
|
+
}, {
|
|
356
|
+
peerId: peerIdC,
|
|
357
|
+
registrar: registrarC
|
|
358
|
+
})
|
|
359
|
+
await connectPeers(psA.multicodecs[0], {
|
|
360
|
+
peerId: peerIdC,
|
|
361
|
+
registrar: registrarC
|
|
362
|
+
}, {
|
|
363
|
+
peerId: peerIdD,
|
|
364
|
+
registrar: registrarD
|
|
365
|
+
})
|
|
366
|
+
await connectPeers(psA.multicodecs[0], {
|
|
367
|
+
peerId: peerIdD,
|
|
368
|
+
registrar: registrarD
|
|
369
|
+
}, {
|
|
370
|
+
peerId: peerIdE,
|
|
371
|
+
registrar: registrarE
|
|
372
|
+
})
|
|
262
373
|
|
|
263
374
|
// Wait for peers to be ready in pubsub
|
|
264
375
|
await pWaitFor(() =>
|
|
265
|
-
psA.
|
|
266
|
-
psB.
|
|
267
|
-
psC.
|
|
268
|
-
psD.
|
|
269
|
-
psE.
|
|
376
|
+
psA.getPeers().length === 1 &&
|
|
377
|
+
psB.getPeers().length === 2 &&
|
|
378
|
+
psC.getPeers().length === 2 &&
|
|
379
|
+
psD.getPeers().length === 2 &&
|
|
380
|
+
psE.getPeers().length === 1
|
|
270
381
|
)
|
|
271
382
|
})
|
|
272
383
|
|
|
273
384
|
afterEach(async () => {
|
|
274
|
-
|
|
385
|
+
await Promise.all(
|
|
386
|
+
[psA, psB, psC, psD, psE].map(async (p) => await p.stop())
|
|
387
|
+
)
|
|
275
388
|
await common.teardown()
|
|
276
389
|
})
|
|
277
390
|
|
|
278
391
|
it('subscribes', () => {
|
|
279
392
|
psA.subscribe('Z')
|
|
280
|
-
|
|
393
|
+
expect(psA.getTopics()).to.deep.equal(['Z'])
|
|
281
394
|
psB.subscribe('Z')
|
|
282
|
-
|
|
395
|
+
expect(psB.getTopics()).to.deep.equal(['Z'])
|
|
283
396
|
psC.subscribe('Z')
|
|
284
|
-
|
|
397
|
+
expect(psC.getTopics()).to.deep.equal(['Z'])
|
|
285
398
|
psD.subscribe('Z')
|
|
286
|
-
|
|
399
|
+
expect(psD.getTopics()).to.deep.equal(['Z'])
|
|
287
400
|
psE.subscribe('Z')
|
|
288
|
-
|
|
401
|
+
expect(psE.getTopics()).to.deep.equal(['Z'])
|
|
289
402
|
})
|
|
290
403
|
|
|
291
404
|
it('publishes from c', async function () {
|
|
@@ -314,7 +427,7 @@ export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
|
314
427
|
// await a cycle
|
|
315
428
|
await delay(1000)
|
|
316
429
|
|
|
317
|
-
void psC.
|
|
430
|
+
void psC.dispatchEvent(new CustomEvent('Z', { detail: uint8ArrayFromString('hey from c') }))
|
|
318
431
|
|
|
319
432
|
function incMsg (evt: CustomEvent<Message>) {
|
|
320
433
|
const msg = evt.detail
|