Skip to content

Commit 41a1f2f

Browse files
committed
fix: ensure all nodes are connected
1 parent 6ad5ac2 commit 41a1f2f

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/dht/content-fetching.ts

+37-22
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,57 @@ export function contentFetchingTests (factory: DaemonFactory): void {
3030

3131
function runContentFetchingTests (factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions): void {
3232
describe('dht.contentFetching', () => {
33-
let daemonA: Daemon
34-
let daemonB: Daemon
35-
let daemonC: Daemon
36-
let daemonD: Daemon
33+
let nodes: Daemon[]
3734

3835
// Start Daemons
3936
before(async function () {
4037
this.timeout(20 * 1000)
4138

42-
daemonA = await factory.spawn(optionsA)
43-
daemonB = await factory.spawn(optionsA)
44-
daemonC = await factory.spawn(optionsB)
45-
daemonD = await factory.spawn(optionsB)
39+
nodes = await Promise.all([
40+
factory.spawn(optionsA),
41+
...new Array(3).fill(0).map(async () => factory.spawn(optionsB))
42+
])
4643

47-
const identifyA = await daemonA.client.identify()
48-
const identifyB = await daemonB.client.identify()
49-
const identifyC = await daemonC.client.identify()
50-
const identifyD = await daemonD.client.identify()
44+
const identify = await Promise.all(
45+
nodes.map(async node => node.client.identify())
46+
)
47+
48+
// connect them all
49+
for (let i = 0; i < nodes.length; i++) {
50+
for (let k = 0; k < nodes.length; k++) {
51+
if (i === k) {
52+
continue
53+
}
54+
55+
const a = nodes[i]
56+
const b = identify[k]
5157

52-
// connect them A -> B -> C -> D
53-
await daemonA.client.connect(identifyB.peerId, identifyB.addrs)
54-
await daemonB.client.connect(identifyC.peerId, identifyC.addrs)
55-
await daemonC.client.connect(identifyD.peerId, identifyD.addrs)
58+
await a.client.connect(b.peerId, b.addrs)
59+
}
60+
}
5661

5762
// wait for identify
5863
await delay(1000)
5964

60-
// B can find D and C can find A, so their routing tables are not empty
61-
await expect(daemonB.client.dht.findPeer(identifyD.peerId)).to.eventually.be.ok()
62-
await expect(daemonC.client.dht.findPeer(identifyA.peerId)).to.eventually.be.ok()
65+
// ensure they can all find each other
66+
for (let i = 0; i < nodes.length; i++) {
67+
for (let k = 0; k < nodes.length; k++) {
68+
if (i === k) {
69+
continue
70+
}
71+
72+
const a = nodes[i]
73+
const b = identify[k]
74+
75+
await expect(a.client.dht.findPeer(b.peerId)).to.eventually.be.ok()
76+
}
77+
}
6378
})
6479

6580
// Stop daemons
6681
after(async function () {
6782
await Promise.all(
68-
[daemonA, daemonB, daemonC, daemonD]
83+
nodes
6984
.filter(Boolean)
7085
.map(async d => { await d.stop() })
7186
)
@@ -74,9 +89,9 @@ function runContentFetchingTests (factory: DaemonFactory, optionsA: SpawnOptions
7489
it(`${optionsA.type} peer to ${optionsB.type} peer`, async function () {
7590
this.timeout(10 * 1000)
7691

77-
await daemonB.client.dht.put(record.key, record.value)
92+
await nodes[0].client.dht.put(record.key, record.value)
7893

79-
const data = await daemonC.client.dht.get(record.key)
94+
const data = await nodes[1].client.dht.get(record.key)
8095
expect(data).to.equalBytes(record.value)
8196
})
8297
})

0 commit comments

Comments
 (0)