@libp2p/interface-compliance-tests 1.1.0 → 1.1.1
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/peer-discovery/index.d.ts.map +1 -1
- package/dist/src/peer-discovery/index.js +5 -5
- package/dist/src/peer-discovery/index.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 +6 -6
- 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 +18 -9
- 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 +6 -2
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/index.d.ts +9 -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.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 +58 -35
- 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 +44 -19
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/transport/listen-test.d.ts.map +1 -1
- package/dist/src/transport/listen-test.js +10 -7
- package/dist/src/transport/listen-test.js.map +1 -1
- package/dist/src/utils/mock-connection-manager.d.ts +18 -0
- package/dist/src/utils/mock-connection-manager.d.ts.map +1 -0
- package/dist/src/utils/mock-connection-manager.js +22 -0
- package/dist/src/utils/mock-connection-manager.js.map +1 -0
- package/dist/src/utils/mock-registrar.d.ts +14 -0
- package/dist/src/utils/mock-registrar.d.ts.map +1 -0
- package/dist/src/utils/mock-registrar.js +38 -0
- package/dist/src/utils/mock-registrar.js.map +1 -0
- package/package.json +11 -3
- package/src/peer-discovery/index.ts +5 -7
- package/src/pubsub/api.ts +10 -11
- package/src/pubsub/connection-handlers.ts +22 -13
- package/src/pubsub/emit-self.ts +9 -5
- package/src/pubsub/index.ts +10 -3
- package/src/pubsub/messages.ts +3 -3
- package/src/pubsub/multiple-nodes.ts +68 -45
- package/src/pubsub/two-nodes.ts +48 -23
- package/src/transport/listen-test.ts +10 -7
- package/src/utils/mock-connection-manager.ts +30 -0
- package/src/utils/mock-registrar.ts +49 -0
|
@@ -7,12 +7,12 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
7
7
|
import { expectSet } from './utils.js'
|
|
8
8
|
import type { TestSetup } from '../index.js'
|
|
9
9
|
import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
|
|
10
|
-
import type {
|
|
10
|
+
import type { EventMap } from './index.js'
|
|
11
11
|
|
|
12
|
-
export default (common: TestSetup<PubSub
|
|
12
|
+
export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
13
13
|
describe('pubsub connection handlers', () => {
|
|
14
|
-
let psA: PubSub
|
|
15
|
-
let psB: PubSub
|
|
14
|
+
let psA: PubSub<EventMap>
|
|
15
|
+
let psB: PubSub<EventMap>
|
|
16
16
|
|
|
17
17
|
describe('nodes send state on connection', () => {
|
|
18
18
|
// Create pubsub nodes and connect them
|
|
@@ -48,8 +48,12 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
48
48
|
await Promise.all([
|
|
49
49
|
// @ts-expect-error protected fields
|
|
50
50
|
psA._libp2p.dial(psB.peerId),
|
|
51
|
-
new Promise((resolve) => psA.
|
|
52
|
-
|
|
51
|
+
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
52
|
+
once: true
|
|
53
|
+
})),
|
|
54
|
+
new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
|
|
55
|
+
once: true
|
|
56
|
+
}))
|
|
53
57
|
])
|
|
54
58
|
|
|
55
59
|
expect(psA.peers.size).to.equal(1)
|
|
@@ -103,7 +107,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
103
107
|
let subscribedTopics = psA.getTopics()
|
|
104
108
|
expect(subscribedTopics).to.not.include(topic)
|
|
105
109
|
|
|
106
|
-
psA.
|
|
110
|
+
psA.addEventListener(topic, (evt) => {
|
|
111
|
+
const msg = evt.detail
|
|
107
112
|
expect(msg.data).to.equalBytes(data)
|
|
108
113
|
defer.resolve()
|
|
109
114
|
})
|
|
@@ -174,7 +179,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
174
179
|
let subscribedTopics = psA.getTopics()
|
|
175
180
|
expect(subscribedTopics).to.not.include(topic)
|
|
176
181
|
|
|
177
|
-
psA.
|
|
182
|
+
psA.addEventListener(topic, (evt) => {
|
|
183
|
+
const msg = evt.detail
|
|
178
184
|
expect(msg.data).to.equalBytes(data)
|
|
179
185
|
defer.resolve()
|
|
180
186
|
})
|
|
@@ -228,7 +234,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
228
234
|
let subscribedTopics = psA.getTopics()
|
|
229
235
|
expect(subscribedTopics).to.not.include(topic)
|
|
230
236
|
|
|
231
|
-
psA.
|
|
237
|
+
psA.addEventListener(topic, (evt) => {
|
|
238
|
+
const msg = evt.detail
|
|
232
239
|
expect(msg.data).to.equalBytes(data)
|
|
233
240
|
counter++
|
|
234
241
|
counter === 1 ? defer1.resolve() : defer2.resolve()
|
|
@@ -285,7 +292,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
285
292
|
let bReceivedFirstMessageFromA = false
|
|
286
293
|
let bReceivedSecondMessageFromA = false
|
|
287
294
|
|
|
288
|
-
const handlerSpyA = (
|
|
295
|
+
const handlerSpyA = (evt: CustomEvent<Message>) => {
|
|
296
|
+
const message = evt.detail
|
|
289
297
|
const data = uint8ArrayToString(message.data)
|
|
290
298
|
|
|
291
299
|
if (data === 'message-from-b-1') {
|
|
@@ -296,7 +304,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
296
304
|
aReceivedSecondMessageFromB = true
|
|
297
305
|
}
|
|
298
306
|
}
|
|
299
|
-
const handlerSpyB = (
|
|
307
|
+
const handlerSpyB = (evt: CustomEvent<Message>) => {
|
|
308
|
+
const message = evt.detail
|
|
300
309
|
const data = uint8ArrayToString(message.data)
|
|
301
310
|
|
|
302
311
|
if (data === 'message-from-a-1') {
|
|
@@ -310,8 +319,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
310
319
|
|
|
311
320
|
const topic = 'reconnect-channel'
|
|
312
321
|
|
|
313
|
-
psA.
|
|
314
|
-
psB.
|
|
322
|
+
psA.addEventListener(topic, handlerSpyA)
|
|
323
|
+
psB.addEventListener(topic, handlerSpyB)
|
|
315
324
|
psA.subscribe(topic)
|
|
316
325
|
psB.subscribe(topic)
|
|
317
326
|
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -3,15 +3,15 @@ import sinon from 'sinon'
|
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import type { TestSetup } from '../index.js'
|
|
5
5
|
import type { PubSub, PubsubOptions } from '@libp2p/interfaces/pubsub'
|
|
6
|
-
import type {
|
|
6
|
+
import type { EventMap } from './index.js'
|
|
7
7
|
|
|
8
8
|
const topic = 'foo'
|
|
9
9
|
const data = uint8ArrayFromString('bar')
|
|
10
10
|
const shouldNotHappen = () => expect.fail()
|
|
11
11
|
|
|
12
|
-
export default (common: TestSetup<PubSub
|
|
12
|
+
export default (common: TestSetup<PubSub<EventMap>, Partial<PubsubOptions>>) => {
|
|
13
13
|
describe('emit self', () => {
|
|
14
|
-
let pubsub: PubSub
|
|
14
|
+
let pubsub: PubSub<EventMap>
|
|
15
15
|
|
|
16
16
|
describe('enabled', () => {
|
|
17
17
|
before(async () => {
|
|
@@ -30,7 +30,9 @@ export default (common: TestSetup<PubSub & Startable, Partial<PubsubOptions>>) =
|
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('should emit to self on publish', async () => {
|
|
33
|
-
const promise = new Promise((resolve) => pubsub.
|
|
33
|
+
const promise = new Promise((resolve) => pubsub.addEventListener(topic, resolve, {
|
|
34
|
+
once: true
|
|
35
|
+
}))
|
|
34
36
|
|
|
35
37
|
void pubsub.publish(topic, data)
|
|
36
38
|
|
|
@@ -55,7 +57,9 @@ export default (common: TestSetup<PubSub & Startable, Partial<PubsubOptions>>) =
|
|
|
55
57
|
})
|
|
56
58
|
|
|
57
59
|
it('should not emit to self on publish', async () => {
|
|
58
|
-
pubsub.
|
|
60
|
+
pubsub.addEventListener(topic, () => shouldNotHappen, {
|
|
61
|
+
once: true
|
|
62
|
+
})
|
|
59
63
|
|
|
60
64
|
void pubsub.publish(topic, data)
|
|
61
65
|
|
package/src/pubsub/index.ts
CHANGED
|
@@ -5,10 +5,17 @@ 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 } from '@libp2p/interfaces/pubsub'
|
|
9
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
8
|
+
import type { PubSub, Message, PubsubEvents } from '@libp2p/interfaces/pubsub'
|
|
10
9
|
|
|
11
|
-
export
|
|
10
|
+
export interface EventMap extends PubsubEvents {
|
|
11
|
+
'topic': CustomEvent<Message>
|
|
12
|
+
'foo': CustomEvent<Message>
|
|
13
|
+
'test-topic': CustomEvent<Message>
|
|
14
|
+
'reconnect-channel': CustomEvent<Message>
|
|
15
|
+
'Z': CustomEvent<Message>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
12
19
|
describe('interface-pubsub compliance tests', () => {
|
|
13
20
|
apiTest(common)
|
|
14
21
|
emitSelfTest(common)
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -6,14 +6,14 @@ import * as utils from '@libp2p/pubsub/utils'
|
|
|
6
6
|
import { PeerStreams } from '@libp2p/pubsub/peer-streams'
|
|
7
7
|
import type { TestSetup } from '../index.js'
|
|
8
8
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
9
|
-
import type {
|
|
9
|
+
import type { EventMap } from './index.js'
|
|
10
10
|
|
|
11
11
|
const topic = 'foo'
|
|
12
12
|
const data = uint8ArrayFromString('bar')
|
|
13
13
|
|
|
14
|
-
export default (common: TestSetup<PubSub
|
|
14
|
+
export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
15
15
|
describe('messages', () => {
|
|
16
|
-
let pubsub: PubSub
|
|
16
|
+
let pubsub: PubSub<EventMap>
|
|
17
17
|
|
|
18
18
|
// Create pubsub router
|
|
19
19
|
beforeEach(async () => {
|
|
@@ -9,18 +9,18 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
9
9
|
import { expectSet } from './utils.js'
|
|
10
10
|
import type { TestSetup } from '../index.js'
|
|
11
11
|
import type { PubSub, Message } from '@libp2p/interfaces/pubsub'
|
|
12
|
-
import type {
|
|
12
|
+
import type { EventMap } from './index.js'
|
|
13
13
|
|
|
14
|
-
export default (common: TestSetup<PubSub
|
|
14
|
+
export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
15
15
|
describe('pubsub with multiple nodes', function () {
|
|
16
16
|
describe('every peer subscribes to the topic', () => {
|
|
17
17
|
describe('line', () => {
|
|
18
18
|
// line
|
|
19
19
|
// ◉────◉────◉
|
|
20
20
|
// a b c
|
|
21
|
-
let psA: PubSub
|
|
22
|
-
let psB: PubSub
|
|
23
|
-
let psC: PubSub
|
|
21
|
+
let psA: PubSub<EventMap>
|
|
22
|
+
let psB: PubSub<EventMap>
|
|
23
|
+
let psC: PubSub<EventMap>
|
|
24
24
|
|
|
25
25
|
// Create and start pubsub nodes
|
|
26
26
|
beforeEach(async () => {
|
|
@@ -60,7 +60,9 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
60
60
|
psA.subscribe(topic)
|
|
61
61
|
expectSet(psA.subscriptions, [topic])
|
|
62
62
|
|
|
63
|
-
await new Promise((resolve) => psB.
|
|
63
|
+
await new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve, {
|
|
64
|
+
once: true
|
|
65
|
+
}))
|
|
64
66
|
expect(psB.peers.size).to.equal(2)
|
|
65
67
|
|
|
66
68
|
const aPeerId = psA.peerId.toString()
|
|
@@ -76,8 +78,12 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
76
78
|
expectSet(psB.subscriptions, [topic])
|
|
77
79
|
|
|
78
80
|
await Promise.all([
|
|
79
|
-
new Promise((resolve) => psA.
|
|
80
|
-
|
|
81
|
+
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve, {
|
|
82
|
+
once: true
|
|
83
|
+
})),
|
|
84
|
+
new Promise((resolve) => psC.addEventListener('pubsub:subscription-change', resolve, {
|
|
85
|
+
once: true
|
|
86
|
+
}))
|
|
81
87
|
])
|
|
82
88
|
|
|
83
89
|
expect(psA.peers.size).to.equal(1)
|
|
@@ -94,12 +100,14 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
94
100
|
psC.subscribe(topic)
|
|
95
101
|
expectSet(psC.subscriptions, [topic])
|
|
96
102
|
|
|
97
|
-
psB.
|
|
103
|
+
psB.addEventListener('pubsub:subscription-change', () => {
|
|
98
104
|
expect(psA.peers.size).to.equal(1)
|
|
99
105
|
expect(psB.peers.size).to.equal(2)
|
|
100
106
|
expectSet(psB.topics.get(topic), [psC.peerId.toString()])
|
|
101
107
|
|
|
102
108
|
defer.resolve()
|
|
109
|
+
}, {
|
|
110
|
+
once: true
|
|
103
111
|
})
|
|
104
112
|
|
|
105
113
|
return await defer.promise
|
|
@@ -115,9 +123,15 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
115
123
|
|
|
116
124
|
// await subscription change
|
|
117
125
|
await Promise.all([
|
|
118
|
-
new Promise(resolve => psA.
|
|
119
|
-
|
|
120
|
-
|
|
126
|
+
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
127
|
+
once: true
|
|
128
|
+
})),
|
|
129
|
+
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
130
|
+
once: true
|
|
131
|
+
})),
|
|
132
|
+
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
133
|
+
once: true
|
|
134
|
+
}))
|
|
121
135
|
])
|
|
122
136
|
|
|
123
137
|
// await a cycle
|
|
@@ -125,22 +139,23 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
125
139
|
|
|
126
140
|
let counter = 0
|
|
127
141
|
|
|
128
|
-
psA.
|
|
129
|
-
psB.
|
|
130
|
-
psC.
|
|
142
|
+
psA.addEventListener(topic, incMsg)
|
|
143
|
+
psB.addEventListener(topic, incMsg)
|
|
144
|
+
psC.addEventListener(topic, incMsg)
|
|
131
145
|
|
|
132
146
|
void psA.publish(topic, uint8ArrayFromString('hey'))
|
|
133
147
|
|
|
134
|
-
function incMsg (
|
|
148
|
+
function incMsg (evt: CustomEvent<Message>) {
|
|
149
|
+
const msg = evt.detail
|
|
135
150
|
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
136
151
|
check()
|
|
137
152
|
}
|
|
138
153
|
|
|
139
154
|
function check () {
|
|
140
155
|
if (++counter === 3) {
|
|
141
|
-
psA.
|
|
142
|
-
psB.
|
|
143
|
-
psC.
|
|
156
|
+
psA.removeEventListener(topic, incMsg)
|
|
157
|
+
psB.removeEventListener(topic, incMsg)
|
|
158
|
+
psC.removeEventListener(topic, incMsg)
|
|
144
159
|
defer.resolve()
|
|
145
160
|
}
|
|
146
161
|
}
|
|
@@ -168,30 +183,37 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
168
183
|
|
|
169
184
|
// await subscription change
|
|
170
185
|
await Promise.all([
|
|
171
|
-
new Promise(resolve => psA.
|
|
172
|
-
|
|
173
|
-
|
|
186
|
+
new Promise(resolve => psA.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
187
|
+
once: true
|
|
188
|
+
})),
|
|
189
|
+
new Promise(resolve => psB.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
190
|
+
once: true
|
|
191
|
+
})),
|
|
192
|
+
new Promise(resolve => psC.addEventListener('pubsub:subscription-change', () => resolve(null), {
|
|
193
|
+
once: true
|
|
194
|
+
}))
|
|
174
195
|
])
|
|
175
196
|
|
|
176
|
-
psA.
|
|
177
|
-
psB.
|
|
178
|
-
psC.
|
|
197
|
+
psA.addEventListener(topic, incMsg)
|
|
198
|
+
psB.addEventListener(topic, incMsg)
|
|
199
|
+
psC.addEventListener(topic, incMsg)
|
|
179
200
|
|
|
180
201
|
// await a cycle
|
|
181
202
|
await delay(1000)
|
|
182
203
|
|
|
183
204
|
void psB.publish(topic, uint8ArrayFromString('hey'))
|
|
184
205
|
|
|
185
|
-
function incMsg (
|
|
206
|
+
function incMsg (evt: CustomEvent<Message>) {
|
|
207
|
+
const msg = evt.detail
|
|
186
208
|
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
187
209
|
check()
|
|
188
210
|
}
|
|
189
211
|
|
|
190
212
|
function check () {
|
|
191
213
|
if (++counter === 3) {
|
|
192
|
-
psA.
|
|
193
|
-
psB.
|
|
194
|
-
psC.
|
|
214
|
+
psA.removeEventListener(topic, incMsg)
|
|
215
|
+
psB.removeEventListener(topic, incMsg)
|
|
216
|
+
psC.removeEventListener(topic, incMsg)
|
|
195
217
|
defer.resolve()
|
|
196
218
|
}
|
|
197
219
|
}
|
|
@@ -209,11 +231,11 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
209
231
|
// │b d│
|
|
210
232
|
// ◉─┘ └─◉
|
|
211
233
|
// a
|
|
212
|
-
let psA: PubSub
|
|
213
|
-
let psB: PubSub
|
|
214
|
-
let psC: PubSub
|
|
215
|
-
let psD: PubSub
|
|
216
|
-
let psE: PubSub
|
|
234
|
+
let psA: PubSub<EventMap>
|
|
235
|
+
let psB: PubSub<EventMap>
|
|
236
|
+
let psC: PubSub<EventMap>
|
|
237
|
+
let psD: PubSub<EventMap>
|
|
238
|
+
let psE: PubSub<EventMap>
|
|
217
239
|
|
|
218
240
|
// Create and start pubsub nodes
|
|
219
241
|
beforeEach(async () => {
|
|
@@ -271,22 +293,22 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
271
293
|
let counter = 0
|
|
272
294
|
|
|
273
295
|
psA.subscribe('Z')
|
|
274
|
-
psA.
|
|
296
|
+
psA.addEventListener('Z', incMsg)
|
|
275
297
|
psB.subscribe('Z')
|
|
276
|
-
psB.
|
|
298
|
+
psB.addEventListener('Z', incMsg)
|
|
277
299
|
psC.subscribe('Z')
|
|
278
|
-
psC.
|
|
300
|
+
psC.addEventListener('Z', incMsg)
|
|
279
301
|
psD.subscribe('Z')
|
|
280
|
-
psD.
|
|
302
|
+
psD.addEventListener('Z', incMsg)
|
|
281
303
|
psE.subscribe('Z')
|
|
282
|
-
psE.
|
|
304
|
+
psE.addEventListener('Z', incMsg)
|
|
283
305
|
|
|
284
306
|
await Promise.all([
|
|
285
|
-
new Promise((resolve) => psA.
|
|
286
|
-
new Promise((resolve) => psB.
|
|
287
|
-
new Promise((resolve) => psC.
|
|
288
|
-
new Promise((resolve) => psD.
|
|
289
|
-
new Promise((resolve) => psE.
|
|
307
|
+
new Promise((resolve) => psA.addEventListener('pubsub:subscription-change', resolve)),
|
|
308
|
+
new Promise((resolve) => psB.addEventListener('pubsub:subscription-change', resolve)),
|
|
309
|
+
new Promise((resolve) => psC.addEventListener('pubsub:subscription-change', resolve)),
|
|
310
|
+
new Promise((resolve) => psD.addEventListener('pubsub:subscription-change', resolve)),
|
|
311
|
+
new Promise((resolve) => psE.addEventListener('pubsub:subscription-change', resolve))
|
|
290
312
|
])
|
|
291
313
|
|
|
292
314
|
// await a cycle
|
|
@@ -294,7 +316,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
294
316
|
|
|
295
317
|
void psC.publish('Z', uint8ArrayFromString('hey from c'))
|
|
296
318
|
|
|
297
|
-
function incMsg (
|
|
319
|
+
function incMsg (evt: CustomEvent<Message>) {
|
|
320
|
+
const msg = evt.detail
|
|
298
321
|
expect(uint8ArrayToString(msg.data)).to.equal('hey from c')
|
|
299
322
|
check()
|
|
300
323
|
}
|
package/src/pubsub/two-nodes.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
first,
|
|
12
12
|
expectSet
|
|
13
13
|
} from './utils.js'
|
|
14
|
-
import type {
|
|
14
|
+
import type { EventMap } from './index.js'
|
|
15
15
|
|
|
16
16
|
const topic = 'foo'
|
|
17
17
|
|
|
@@ -19,10 +19,10 @@ function shouldNotHappen () {
|
|
|
19
19
|
expect.fail()
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export default (common: TestSetup<PubSub
|
|
22
|
+
export default (common: TestSetup<PubSub<EventMap>>) => {
|
|
23
23
|
describe('pubsub with two nodes', () => {
|
|
24
|
-
let psA: PubSub
|
|
25
|
-
let psB: PubSub
|
|
24
|
+
let psA: PubSub<EventMap>
|
|
25
|
+
let psB: PubSub<EventMap>
|
|
26
26
|
|
|
27
27
|
// Create pubsub nodes and connect them
|
|
28
28
|
before(async () => {
|
|
@@ -55,7 +55,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
55
55
|
it('Subscribe to a topic in nodeA', async () => {
|
|
56
56
|
const defer = pDefer()
|
|
57
57
|
|
|
58
|
-
psB.
|
|
58
|
+
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
59
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
59
60
|
expectSet(psA.subscriptions, [topic])
|
|
60
61
|
expect(psB.peers.size).to.equal(1)
|
|
61
62
|
expectSet(psB.topics.get(topic), [psA.peerId.toString()])
|
|
@@ -64,6 +65,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
64
65
|
expect(changedSubs[0].topicID).to.equal(topic)
|
|
65
66
|
expect(changedSubs[0].subscribe).to.equal(true)
|
|
66
67
|
defer.resolve()
|
|
68
|
+
}, {
|
|
69
|
+
once: true
|
|
67
70
|
})
|
|
68
71
|
psA.subscribe(topic)
|
|
69
72
|
|
|
@@ -73,13 +76,18 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
73
76
|
it('Publish to a topic in nodeA', async () => {
|
|
74
77
|
const defer = pDefer()
|
|
75
78
|
|
|
76
|
-
psA.
|
|
79
|
+
psA.addEventListener(topic, (evt) => {
|
|
80
|
+
const msg = evt.detail
|
|
77
81
|
expect(uint8ArrayToString(msg.data)).to.equal('hey')
|
|
78
|
-
psB.
|
|
82
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
79
83
|
defer.resolve()
|
|
84
|
+
}, {
|
|
85
|
+
once: true
|
|
80
86
|
})
|
|
81
87
|
|
|
82
|
-
psB.
|
|
88
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
89
|
+
once: true
|
|
90
|
+
})
|
|
83
91
|
|
|
84
92
|
void psA.publish(topic, uint8ArrayFromString('hey'))
|
|
85
93
|
|
|
@@ -89,19 +97,26 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
89
97
|
it('Publish to a topic in nodeB', async () => {
|
|
90
98
|
const defer = pDefer()
|
|
91
99
|
|
|
92
|
-
psA.
|
|
93
|
-
|
|
100
|
+
psA.addEventListener(topic, (evt) => {
|
|
101
|
+
const msg = evt.detail
|
|
102
|
+
psA.addEventListener(topic, shouldNotHappen, {
|
|
103
|
+
once: true
|
|
104
|
+
})
|
|
94
105
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
95
106
|
|
|
96
107
|
setTimeout(() => {
|
|
97
|
-
psA.
|
|
98
|
-
psB.
|
|
108
|
+
psA.removeEventListener(topic, shouldNotHappen)
|
|
109
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
99
110
|
|
|
100
111
|
defer.resolve()
|
|
101
112
|
}, 100)
|
|
113
|
+
}, {
|
|
114
|
+
once: true
|
|
102
115
|
})
|
|
103
116
|
|
|
104
|
-
psB.
|
|
117
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
118
|
+
once: true
|
|
119
|
+
})
|
|
105
120
|
|
|
106
121
|
void psB.publish(topic, uint8ArrayFromString('banana'))
|
|
107
122
|
|
|
@@ -112,18 +127,21 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
112
127
|
const defer = pDefer()
|
|
113
128
|
let counter = 0
|
|
114
129
|
|
|
115
|
-
psB.
|
|
116
|
-
|
|
130
|
+
psB.addEventListener(topic, shouldNotHappen, {
|
|
131
|
+
once: true
|
|
132
|
+
})
|
|
133
|
+
psA.addEventListener(topic, receivedMsg)
|
|
117
134
|
|
|
118
|
-
function receivedMsg (
|
|
135
|
+
function receivedMsg (evt: CustomEvent<Message>) {
|
|
136
|
+
const msg = evt.detail
|
|
119
137
|
expect(uint8ArrayToString(msg.data)).to.equal('banana')
|
|
120
138
|
expect(msg.from).to.be.eql(psB.peerId.toString())
|
|
121
139
|
expect(msg.seqno).to.be.a('Uint8Array')
|
|
122
140
|
expect(msg.topicIDs).to.be.eql([topic])
|
|
123
141
|
|
|
124
142
|
if (++counter === 10) {
|
|
125
|
-
psA.
|
|
126
|
-
psB.
|
|
143
|
+
psA.removeEventListener(topic, receivedMsg)
|
|
144
|
+
psB.removeEventListener(topic, shouldNotHappen)
|
|
127
145
|
|
|
128
146
|
defer.resolve()
|
|
129
147
|
}
|
|
@@ -140,7 +158,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
140
158
|
psA.unsubscribe(topic)
|
|
141
159
|
expect(psA.subscriptions.size).to.equal(0)
|
|
142
160
|
|
|
143
|
-
psB.
|
|
161
|
+
psB.addEventListener('pubsub:subscription-change', (evt) => {
|
|
162
|
+
const { peerId: changedPeerId, subscriptions: changedSubs } = evt.detail
|
|
144
163
|
expect(psB.peers.size).to.equal(1)
|
|
145
164
|
expectSet(psB.topics.get(topic), [])
|
|
146
165
|
expect(changedPeerId.toString()).to.equal(first(psB.peers).id.toString())
|
|
@@ -149,6 +168,8 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
149
168
|
expect(changedSubs[0].subscribe).to.equal(false)
|
|
150
169
|
|
|
151
170
|
defer.resolve()
|
|
171
|
+
}, {
|
|
172
|
+
once: true
|
|
152
173
|
})
|
|
153
174
|
|
|
154
175
|
return await defer.promise
|
|
@@ -157,12 +178,16 @@ export default (common: TestSetup<PubSub & Startable>) => {
|
|
|
157
178
|
it('Publish to a topic:Z in nodeA nodeB', async () => {
|
|
158
179
|
const defer = pDefer()
|
|
159
180
|
|
|
160
|
-
psA.
|
|
161
|
-
|
|
181
|
+
psA.addEventListener('Z', shouldNotHappen, {
|
|
182
|
+
once: true
|
|
183
|
+
})
|
|
184
|
+
psB.addEventListener('Z', shouldNotHappen, {
|
|
185
|
+
once: true
|
|
186
|
+
})
|
|
162
187
|
|
|
163
188
|
setTimeout(() => {
|
|
164
|
-
psA.
|
|
165
|
-
psB.
|
|
189
|
+
psA.removeEventListener('Z', shouldNotHappen)
|
|
190
|
+
psB.removeEventListener('Z', shouldNotHappen)
|
|
166
191
|
defer.resolve()
|
|
167
192
|
}, 100)
|
|
168
193
|
|
|
@@ -6,6 +6,7 @@ import { pipe } from 'it-pipe'
|
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
7
|
import { isValidTick, mockUpgrader } from './utils/index.js'
|
|
8
8
|
import defer from 'p-defer'
|
|
9
|
+
import { CustomEvent } from '@libp2p/interfaces'
|
|
9
10
|
import type { TestSetup } from '../index.js'
|
|
10
11
|
import type { Transport } from '@libp2p/interfaces/transport'
|
|
11
12
|
import type { TransportTestFixtures, SetupArgs } from './index.js'
|
|
@@ -108,8 +109,8 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
|
|
|
108
109
|
const deferred = defer()
|
|
109
110
|
let conn
|
|
110
111
|
|
|
111
|
-
listener.
|
|
112
|
-
conn =
|
|
112
|
+
listener.addEventListener('connection', (evt) => {
|
|
113
|
+
conn = evt.detail
|
|
113
114
|
deferred.resolve()
|
|
114
115
|
})
|
|
115
116
|
|
|
@@ -127,7 +128,7 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
|
|
|
127
128
|
|
|
128
129
|
it('listening', (done) => {
|
|
129
130
|
const listener = transport.createListener()
|
|
130
|
-
listener.
|
|
131
|
+
listener.addEventListener('listening', () => {
|
|
131
132
|
listener.close().then(done, done)
|
|
132
133
|
})
|
|
133
134
|
void listener.listen(addrs[0])
|
|
@@ -135,16 +136,18 @@ export default (common: TestSetup<TransportTestFixtures, SetupArgs>) => {
|
|
|
135
136
|
|
|
136
137
|
it('error', (done) => {
|
|
137
138
|
const listener = transport.createListener()
|
|
138
|
-
listener.
|
|
139
|
-
expect(
|
|
139
|
+
listener.addEventListener('error', (evt) => {
|
|
140
|
+
expect(evt.detail).to.be.an.instanceOf(Error)
|
|
140
141
|
listener.close().then(done, done)
|
|
141
142
|
})
|
|
142
|
-
listener.
|
|
143
|
+
listener.dispatchEvent(new CustomEvent('error', {
|
|
144
|
+
detail: new Error('my err')
|
|
145
|
+
}))
|
|
143
146
|
})
|
|
144
147
|
|
|
145
148
|
it('close', (done) => {
|
|
146
149
|
const listener = transport.createListener()
|
|
147
|
-
listener.
|
|
150
|
+
listener.addEventListener('close', done)
|
|
148
151
|
|
|
149
152
|
void (async () => {
|
|
150
153
|
await listener.listen(addrs[0])
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EventEmitter } from '@libp2p/interfaces'
|
|
2
|
+
import type { Connection } from '@libp2p/interfaces/src/connection'
|
|
3
|
+
import type { PeerId } from '@libp2p/interfaces/src/peer-id'
|
|
4
|
+
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interfaces/src/registrar'
|
|
5
|
+
|
|
6
|
+
class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager {
|
|
7
|
+
getConnection (peerId: PeerId): Connection | undefined {
|
|
8
|
+
throw new Error('Method not implemented.')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
listenerCount (type: string): number {
|
|
12
|
+
throw new Error('Method not implemented.')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
addEventListener<U extends 'peer:connect'>(type: U, callback: ((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null, options?: boolean | AddEventListenerOptions): void {
|
|
16
|
+
throw new Error('Method not implemented.')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
removeEventListener<U extends 'peer:connect'>(type: U, callback: (((evt: ConnectionManagerEvents[U]) => void) | { handleEvent: (evt: ConnectionManagerEvents[U]) => void } | null) | undefined, options?: boolean | EventListenerOptions): void {
|
|
20
|
+
throw new Error('Method not implemented.')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
dispatchEvent (event: Event): boolean {
|
|
24
|
+
throw new Error('Method not implemented.')
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function mockConnectionManager () {
|
|
29
|
+
return new MockConnectionManager()
|
|
30
|
+
}
|