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