Skip to content

Commit d281a60

Browse files
authored
fix: discovery mechanism examples not working (#1365)
Co-authored-by: achingbrain <alex@achingbrain.net>- fixed tests that were passing even though the example isn't working - added timeouts to avoid infinite wait Fixes #1229
1 parent fc2224a commit d281a60

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

examples/discovery-mechanisms/3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const createNode = async (bootstrappers) => {
6767
const peer = evt.detail
6868
console.log(`Peer ${node1.peerId.toString()} discovered: ${peer.id.toString()}`)
6969
})
70-
node2.addEventListener('peer:discovery',(evt) => {
70+
node2.addEventListener('peer:discovery', (evt) => {
7171
const peer = evt.detail
7272
console.log(`Peer ${node2.peerId.toString()} discovered: ${peer.id.toString()}`)
7373
})
@@ -77,4 +77,4 @@ const createNode = async (bootstrappers) => {
7777
node1.start(),
7878
node2.start()
7979
])
80-
})();
80+
})()

examples/discovery-mechanisms/test-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function test () {
2727
})
2828
})
2929

30-
await pWaitFor(() => discoveredNodes > 1)
30+
await pWaitFor(() => discoveredNodes > 1, 600000)
3131

3232
proc.kill()
3333
}

examples/discovery-mechanisms/test-3.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'url'
77
const __dirname = path.dirname(fileURLToPath(import.meta.url))
88

99
export async function test () {
10-
let discoveredNodes = 0
10+
const discoveredPeers = []
1111

1212
process.stdout.write('3.js\n')
1313

@@ -19,15 +19,20 @@ export async function test () {
1919
proc.all.on('data', async (data) => {
2020
process.stdout.write(data)
2121
const str = uint8ArrayToString(data)
22-
22+
const discoveredPeersRegex = /Peer\s+(?<Peer1>[^\s]+)\s+discovered:\s+(?<Peer2>[^\s]+)/
2323
str.split('\n').forEach(line => {
24-
if (line.includes('discovered:')) {
25-
discoveredNodes++
24+
const peers = line.match(discoveredPeersRegex)
25+
if (peers != null) {
26+
// sort so we don't count reversed pair twice
27+
const match = [peers.groups.Peer1, peers.groups.Peer2].sort().join(',')
28+
if (!discoveredPeers.includes(match)) {
29+
discoveredPeers.push(match)
30+
}
2631
}
2732
})
2833
})
2934

30-
await pWaitFor(() => discoveredNodes > 3)
35+
await pWaitFor(() => discoveredPeers.length > 2, 600000)
3136

3237
proc.kill()
3338
}

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"license": "MIT",
1111
"dependencies": {
12-
"@libp2p/pubsub-peer-discovery": "^6.0.1",
12+
"@libp2p/pubsub-peer-discovery": "^6.0.2",
1313
"@libp2p/floodsub": "^3.0.3",
1414
"@nodeutils/defaults-deep": "^1.1.0",
1515
"execa": "^6.1.0",

0 commit comments

Comments
 (0)