Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions test/announces.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,59 @@ test('announce to group and lookup', async function (t) {
}
})

test('announce to group and lookup (manual addNode)', async function (t) {
// Swarm with no bootstrap
const nodes = await swarm(t, 32, [])
await Promise.all(nodes.map(n => n.ready()))

// Manually add all nodes in swarm to each other
for (const node of nodes) {
for (const otherNode of nodes) {
if (node === otherNode) continue
const { address: host, port } = otherNode.address()
node.addNode({ host, port })
}
}

const [a, b] = nodes
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything from here onwards is unchanged from the previous test (above)

const keyPair1 = DHT.keyPair()
const keyPair2 = DHT.keyPair()
const target = DHT.hash(Buffer.from('testing...'))

await a.announce(target, keyPair1, []).finished()

{
const result = await toArray(b.lookup(target))
t.ok(result.length > 0, 'has at least one result')
t.alike(result[0].peers.length, 1, 'one peer')
t.alike(result[0].peers[0].publicKey, keyPair1.publicKey)
}

await a.announce(target, keyPair2, [{ host: '1.2.3.4', port: 1234 }]).finished()

{
const result = await toArray(b.lookup(target))
t.ok(result.length > 0, 'has at least one result')
t.alike(result[0].peers.length, 2, 'two peers')
t.alike([result[0].peers[0].publicKey, result[0].peers[1].publicKey].sort(), [keyPair1.publicKey, keyPair2.publicKey].sort())

const latest = result[0].peers[result[0].peers[0].publicKey.equals(keyPair2.publicKey) ? 0 : 1]

t.is(latest.relayAddresses.length, 1, 'announced one relay')
t.alike(latest.relayAddresses[0], { host: '1.2.3.4', port: 1234 })
}

for await (const data of a.findPeer(keyPair1.publicKey)) {
t.fail('peer should not be announced')
t.absent(data, 'just make standard happy')
}

for await (const data of a.findPeer(keyPair2.publicKey)) {
t.fail('peer should not be announced')
t.absent(data, 'just make standard happy')
}
})

test('announce null relay addresses', async function (t) {
const [a] = await swarm(t)
const keyPair = DHT.keyPair()
Expand Down