-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgossipsub.ts
72 lines (59 loc) · 2.02 KB
/
gossipsub.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import type { Daemon, DaemonFactory, NodeType, SpawnOptions } from '../index.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import first from 'it-first'
export function gossipsubTests (factory: DaemonFactory) {
const nodeTypes: NodeType[] = ['js', 'go']
for (const typeA of nodeTypes) {
for (const typeB of nodeTypes) {
runGossipsubTests(
factory,
{ type: typeA, pubsub: true, pubsubRouter: 'gossipsub' },
{ type: typeB, pubsub: true, pubsubRouter: 'gossipsub' }
)
}
}
}
function runGossipsubTests (factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions) {
describe('pubsub.gossipsub', () => {
let daemons: Daemon[]
// Start Daemons
before(async function () {
this.timeout(20 * 1000)
daemons = await Promise.all([
factory.spawn(optionsA),
factory.spawn(optionsB)
])
const identify1 = await daemons[1].client.identify()
await daemons[0].client.connect(identify1.peerId, identify1.addrs)
})
// Stop daemons
after(async function () {
if (daemons != null) {
await Promise.all(
daemons.map(async (daemon) => await daemon.stop())
)
}
})
it(`${optionsA.type} peer to ${optionsB.type} peer`, async function () {
const topic = 'test-topic'
const data = uint8ArrayFromString('test-data')
const subscribeIterator = daemons[1].client.pubsub.subscribe(topic)
const subscriber = async () => {
const message = await first(subscribeIterator)
expect(message).to.exist()
expect(message).to.have.property('data').that.equalBytes(data)
}
const publisher = async () => {
// wait for subscription stream
await new Promise(resolve => setTimeout(resolve, 800))
await daemons[0].client.pubsub.publish(topic, data)
}
return await Promise.all([
subscriber(),
publisher()
])
})
})
}