@libp2p/interface-compliance-tests 1.0.8 → 1.1.3
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/stream-muxer/base-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/base-test.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/transport/utils/index.d.ts.map +1 -1
- package/dist/src/transport/utils/index.js +4 -1
- package/dist/src/transport/utils/index.js.map +1 -1
- package/dist/src/utils/mock-connection-gater.d.ts +12 -0
- package/dist/src/utils/mock-connection-gater.d.ts.map +1 -0
- package/dist/src/utils/mock-connection-gater.js +14 -0
- package/dist/src/utils/mock-connection-gater.js.map +1 -0
- 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-connection.d.ts +5 -0
- package/dist/src/utils/mock-connection.d.ts.map +1 -0
- package/dist/src/utils/mock-connection.js +52 -0
- package/dist/src/utils/mock-connection.js.map +1 -0
- package/dist/src/utils/mock-multiaddr-connection.d.ts +4 -0
- package/dist/src/utils/mock-multiaddr-connection.d.ts.map +1 -0
- package/dist/src/utils/mock-multiaddr-connection.js +14 -0
- package/dist/src/utils/mock-multiaddr-connection.js.map +1 -0
- package/dist/src/utils/mock-muxer.d.ts +3 -0
- package/dist/src/utils/mock-muxer.d.ts.map +1 -0
- package/dist/src/utils/mock-muxer.js +37 -0
- package/dist/src/utils/mock-muxer.js.map +1 -0
- package/dist/src/utils/mock-registrar.d.ts +12 -0
- package/dist/src/utils/mock-registrar.d.ts.map +1 -0
- package/dist/src/utils/mock-registrar.js +27 -0
- package/dist/src/utils/mock-registrar.js.map +1 -0
- package/dist/src/utils/mock-upgrader.d.ts +7 -0
- package/dist/src/utils/mock-upgrader.d.ts.map +1 -0
- package/dist/src/utils/mock-upgrader.js +24 -0
- package/dist/src/utils/mock-upgrader.js.map +1 -0
- package/package.json +42 -8
- 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/stream-muxer/base-test.ts +9 -8
- package/src/transport/listen-test.ts +10 -7
- package/src/transport/utils/index.ts +12 -8
- package/src/utils/mock-connection-gater.ts +14 -0
- package/src/utils/mock-connection-manager.ts +30 -0
- package/src/utils/mock-connection.ts +66 -0
- package/src/utils/mock-multiaddr-connection.ts +18 -0
- package/src/utils/mock-muxer.ts +43 -0
- package/src/utils/mock-registrar.ts +35 -0
- package/src/utils/mock-upgrader.ts +33 -0
|
@@ -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
|
|
|
@@ -10,7 +10,8 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
10
10
|
import { isValidTick } from '../transport/utils/index.js'
|
|
11
11
|
import type { DeferredPromise } from 'p-defer'
|
|
12
12
|
import type { TestSetup } from '../index.js'
|
|
13
|
-
import type {
|
|
13
|
+
import type { Stream } from '@libp2p/interfaces/connection'
|
|
14
|
+
import type { Muxer, MuxerOptions } from '@libp2p/interfaces/stream-muxer'
|
|
14
15
|
import type { Source, Duplex } from 'it-stream-types'
|
|
15
16
|
|
|
16
17
|
async function drainAndClose (stream: Duplex<Uint8Array>) {
|
|
@@ -22,8 +23,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
22
23
|
it('Open a stream from the dialer', async () => {
|
|
23
24
|
const p = duplexPair<Uint8Array>()
|
|
24
25
|
const dialer = await common.setup()
|
|
25
|
-
const onStreamPromise: DeferredPromise<
|
|
26
|
-
const onStreamEndPromise: DeferredPromise<
|
|
26
|
+
const onStreamPromise: DeferredPromise<Stream> = defer()
|
|
27
|
+
const onStreamEndPromise: DeferredPromise<Stream> = defer()
|
|
27
28
|
|
|
28
29
|
const listener = await common.setup({
|
|
29
30
|
onStream: stream => {
|
|
@@ -71,7 +72,7 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
71
72
|
|
|
72
73
|
it('Open a stream from the listener', async () => {
|
|
73
74
|
const p = duplexPair<Uint8Array>()
|
|
74
|
-
const onStreamPromise: DeferredPromise<
|
|
75
|
+
const onStreamPromise: DeferredPromise<Stream> = defer()
|
|
75
76
|
const dialer = await common.setup({
|
|
76
77
|
onStream: stream => {
|
|
77
78
|
onStreamPromise.resolve(stream)
|
|
@@ -99,8 +100,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
99
100
|
|
|
100
101
|
it('Open a stream on both sides', async () => {
|
|
101
102
|
const p = duplexPair<Uint8Array>()
|
|
102
|
-
const onDialerStreamPromise: DeferredPromise<
|
|
103
|
-
const onListenerStreamPromise: DeferredPromise<
|
|
103
|
+
const onDialerStreamPromise: DeferredPromise<Stream> = defer()
|
|
104
|
+
const onListenerStreamPromise: DeferredPromise<Stream> = defer()
|
|
104
105
|
const dialer = await common.setup({
|
|
105
106
|
onStream: stream => {
|
|
106
107
|
onDialerStreamPromise.resolve(stream)
|
|
@@ -134,8 +135,8 @@ export default (common: TestSetup<Muxer, MuxerOptions>) => {
|
|
|
134
135
|
it('Open a stream on one side, write, open a stream on the other side', async () => {
|
|
135
136
|
const toString = (source: Source<Uint8Array>) => map(source, (u) => uint8ArrayToString(u))
|
|
136
137
|
const p = duplexPair<Uint8Array>()
|
|
137
|
-
const onDialerStreamPromise: DeferredPromise<
|
|
138
|
-
const onListenerStreamPromise: DeferredPromise<
|
|
138
|
+
const onDialerStreamPromise: DeferredPromise<Stream> = defer()
|
|
139
|
+
const onListenerStreamPromise: DeferredPromise<Stream> = defer()
|
|
139
140
|
const dialer = await common.setup({
|
|
140
141
|
onStream: stream => {
|
|
141
142
|
onDialerStreamPromise.resolve(stream)
|
|
@@ -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])
|
|
@@ -7,8 +7,8 @@ import drain from 'it-drain'
|
|
|
7
7
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
8
8
|
import { pipe } from 'it-pipe'
|
|
9
9
|
import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
|
|
10
|
-
import type { Connection,
|
|
11
|
-
import type {
|
|
10
|
+
import type { Connection, Stream, Metadata, ProtocolStream } from '@libp2p/interfaces/connection'
|
|
11
|
+
import type { Muxer } from '@libp2p/interfaces/stream-muxer'
|
|
12
12
|
import type { Duplex } from 'it-stream-types'
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -46,7 +46,7 @@ export function mockMultiaddrConnection (source: Duplex<Uint8Array>): MultiaddrC
|
|
|
46
46
|
|
|
47
47
|
export function mockMuxer (): Muxer {
|
|
48
48
|
let streamId = 0
|
|
49
|
-
let streams:
|
|
49
|
+
let streams: Stream[] = []
|
|
50
50
|
const p = pushable<Uint8Array>()
|
|
51
51
|
|
|
52
52
|
const muxer: Muxer = {
|
|
@@ -61,7 +61,7 @@ export function mockMuxer (): Muxer {
|
|
|
61
61
|
const echo = pair<Uint8Array>()
|
|
62
62
|
|
|
63
63
|
const id = `${streamId++}`
|
|
64
|
-
const stream:
|
|
64
|
+
const stream: Stream = {
|
|
65
65
|
id,
|
|
66
66
|
sink: echo.sink,
|
|
67
67
|
source: echo.source,
|
|
@@ -116,7 +116,7 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun
|
|
|
116
116
|
const remotePeerIdStr = remoteAddr.getPeerId()
|
|
117
117
|
const remotePeer = remotePeerIdStr != null ? PeerId.fromString(remotePeerIdStr) : await PeerIdFactory.createEd25519PeerId()
|
|
118
118
|
|
|
119
|
-
const streams:
|
|
119
|
+
const streams: Stream[] = []
|
|
120
120
|
let streamId = 0
|
|
121
121
|
|
|
122
122
|
const registry = new Map()
|
|
@@ -140,13 +140,17 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun
|
|
|
140
140
|
tags: [],
|
|
141
141
|
streams,
|
|
142
142
|
newStream: async (protocols) => {
|
|
143
|
+
if (!Array.isArray(protocols)) {
|
|
144
|
+
protocols = [protocols]
|
|
145
|
+
}
|
|
146
|
+
|
|
143
147
|
if (protocols.length === 0) {
|
|
144
148
|
throw new Error('protocols must have a length')
|
|
145
149
|
}
|
|
146
150
|
|
|
147
151
|
const id = `${streamId++}`
|
|
148
|
-
const stream:
|
|
149
|
-
const streamData = {
|
|
152
|
+
const stream: Stream = muxer.newStream(id)
|
|
153
|
+
const streamData: ProtocolStream = {
|
|
150
154
|
protocol: protocols[0],
|
|
151
155
|
stream
|
|
152
156
|
}
|
|
@@ -155,7 +159,7 @@ async function createConnection (maConn: MultiaddrConnection, direction: 'inboun
|
|
|
155
159
|
|
|
156
160
|
return streamData
|
|
157
161
|
},
|
|
158
|
-
addStream: (
|
|
162
|
+
addStream: (stream: Stream, metadata: Metadata) => {
|
|
159
163
|
|
|
160
164
|
},
|
|
161
165
|
removeStream: (id: string) => {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
export function mockConnectionGater () {
|
|
3
|
+
return {
|
|
4
|
+
denyDialPeer: async () => await Promise.resolve(false),
|
|
5
|
+
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
6
|
+
denyInboundConnection: async () => await Promise.resolve(false),
|
|
7
|
+
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
8
|
+
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
9
|
+
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
10
|
+
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
11
|
+
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
12
|
+
filterMultiaddrForPeer: async () => await Promise.resolve(true)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { PeerId } from '@libp2p/peer-id'
|
|
2
|
+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
3
|
+
import { pipe } from 'it-pipe'
|
|
4
|
+
import type { MultiaddrConnection } from '@libp2p/interfaces/transport'
|
|
5
|
+
import type { Connection, Stream, Metadata, ProtocolStream } from '@libp2p/interfaces/connection'
|
|
6
|
+
import type { Muxer } from '@libp2p/interfaces/stream-muxer'
|
|
7
|
+
|
|
8
|
+
export async function mockConnection (maConn: MultiaddrConnection, direction: 'inbound' | 'outbound', muxer: Muxer): Promise<Connection> {
|
|
9
|
+
const remoteAddr = maConn.remoteAddr
|
|
10
|
+
const remotePeerIdStr = remoteAddr.getPeerId()
|
|
11
|
+
const remotePeer = remotePeerIdStr != null ? PeerId.fromString(remotePeerIdStr) : await createEd25519PeerId()
|
|
12
|
+
|
|
13
|
+
const streams: Stream[] = []
|
|
14
|
+
let streamId = 0
|
|
15
|
+
|
|
16
|
+
const registry = new Map()
|
|
17
|
+
|
|
18
|
+
void pipe(
|
|
19
|
+
maConn, muxer, maConn
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
id: 'mock-connection',
|
|
24
|
+
remoteAddr,
|
|
25
|
+
remotePeer,
|
|
26
|
+
stat: {
|
|
27
|
+
status: 'OPEN',
|
|
28
|
+
direction,
|
|
29
|
+
timeline: maConn.timeline,
|
|
30
|
+
multiplexer: 'test-multiplexer',
|
|
31
|
+
encryption: 'yes-yes-very-secure'
|
|
32
|
+
},
|
|
33
|
+
registry,
|
|
34
|
+
tags: [],
|
|
35
|
+
streams,
|
|
36
|
+
newStream: async (protocols) => {
|
|
37
|
+
if (!Array.isArray(protocols)) {
|
|
38
|
+
protocols = [protocols]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (protocols.length === 0) {
|
|
42
|
+
throw new Error('protocols must have a length')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const id = `${streamId++}`
|
|
46
|
+
const stream: Stream = muxer.newStream(id)
|
|
47
|
+
const streamData: ProtocolStream = {
|
|
48
|
+
protocol: protocols[0],
|
|
49
|
+
stream
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
registry.set(id, streamData)
|
|
53
|
+
|
|
54
|
+
return streamData
|
|
55
|
+
},
|
|
56
|
+
addStream: (stream: Stream, metadata: Metadata) => {
|
|
57
|
+
|
|
58
|
+
},
|
|
59
|
+
removeStream: (id: string) => {
|
|
60
|
+
registry.delete(id)
|
|
61
|
+
},
|
|
62
|
+
close: async () => {
|
|
63
|
+
await maConn.close()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|