Skip to content

Commit

Permalink
fix: ensure all nodes are connected
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 29, 2024
1 parent 6ad5ac2 commit 41a1f2f
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/dht/content-fetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,57 @@ export function contentFetchingTests (factory: DaemonFactory): void {

function runContentFetchingTests (factory: DaemonFactory, optionsA: SpawnOptions, optionsB: SpawnOptions): void {
describe('dht.contentFetching', () => {
let daemonA: Daemon
let daemonB: Daemon
let daemonC: Daemon
let daemonD: Daemon
let nodes: Daemon[]

// Start Daemons
before(async function () {
this.timeout(20 * 1000)

daemonA = await factory.spawn(optionsA)
daemonB = await factory.spawn(optionsA)
daemonC = await factory.spawn(optionsB)
daemonD = await factory.spawn(optionsB)
nodes = await Promise.all([
factory.spawn(optionsA),
...new Array(3).fill(0).map(async () => factory.spawn(optionsB))
])

const identifyA = await daemonA.client.identify()
const identifyB = await daemonB.client.identify()
const identifyC = await daemonC.client.identify()
const identifyD = await daemonD.client.identify()
const identify = await Promise.all(
nodes.map(async node => node.client.identify())
)

// connect them all
for (let i = 0; i < nodes.length; i++) {
for (let k = 0; k < nodes.length; k++) {
if (i === k) {
continue
}

const a = nodes[i]
const b = identify[k]

// connect them A -> B -> C -> D
await daemonA.client.connect(identifyB.peerId, identifyB.addrs)
await daemonB.client.connect(identifyC.peerId, identifyC.addrs)
await daemonC.client.connect(identifyD.peerId, identifyD.addrs)
await a.client.connect(b.peerId, b.addrs)
}
}

// wait for identify
await delay(1000)

// B can find D and C can find A, so their routing tables are not empty
await expect(daemonB.client.dht.findPeer(identifyD.peerId)).to.eventually.be.ok()
await expect(daemonC.client.dht.findPeer(identifyA.peerId)).to.eventually.be.ok()
// ensure they can all find each other
for (let i = 0; i < nodes.length; i++) {
for (let k = 0; k < nodes.length; k++) {
if (i === k) {
continue
}

const a = nodes[i]
const b = identify[k]

await expect(a.client.dht.findPeer(b.peerId)).to.eventually.be.ok()
}
}
})

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

await daemonB.client.dht.put(record.key, record.value)
await nodes[0].client.dht.put(record.key, record.value)

const data = await daemonC.client.dht.get(record.key)
const data = await nodes[1].client.dht.get(record.key)
expect(data).to.equalBytes(record.value)
})
})
Expand Down

0 comments on commit 41a1f2f

Please sign in to comment.