Skip to content

Commit

Permalink
fix: discovery mechanism examples not working (#1365)
Browse files Browse the repository at this point in the history
Co-authored-by: achingbrain <[email protected]>- fixed tests that were passing even though the example isn't working
- added timeouts to avoid infinite wait

Fixes #1229
  • Loading branch information
mpetrunic authored Sep 5, 2022
1 parent fc2224a commit d281a60
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/discovery-mechanisms/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const createNode = async (bootstrappers) => {
const peer = evt.detail
console.log(`Peer ${node1.peerId.toString()} discovered: ${peer.id.toString()}`)
})
node2.addEventListener('peer:discovery',(evt) => {
node2.addEventListener('peer:discovery', (evt) => {
const peer = evt.detail
console.log(`Peer ${node2.peerId.toString()} discovered: ${peer.id.toString()}`)
})
Expand All @@ -77,4 +77,4 @@ const createNode = async (bootstrappers) => {
node1.start(),
node2.start()
])
})();
})()
2 changes: 1 addition & 1 deletion examples/discovery-mechanisms/test-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function test () {
})
})

await pWaitFor(() => discoveredNodes > 1)
await pWaitFor(() => discoveredNodes > 1, 600000)

proc.kill()
}
15 changes: 10 additions & 5 deletions examples/discovery-mechanisms/test-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

export async function test () {
let discoveredNodes = 0
const discoveredPeers = []

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

Expand All @@ -19,15 +19,20 @@ export async function test () {
proc.all.on('data', async (data) => {
process.stdout.write(data)
const str = uint8ArrayToString(data)

const discoveredPeersRegex = /Peer\s+(?<Peer1>[^\s]+)\s+discovered:\s+(?<Peer2>[^\s]+)/
str.split('\n').forEach(line => {
if (line.includes('discovered:')) {
discoveredNodes++
const peers = line.match(discoveredPeersRegex)
if (peers != null) {
// sort so we don't count reversed pair twice
const match = [peers.groups.Peer1, peers.groups.Peer2].sort().join(',')
if (!discoveredPeers.includes(match)) {
discoveredPeers.push(match)
}
}
})
})

await pWaitFor(() => discoveredNodes > 3)
await pWaitFor(() => discoveredPeers.length > 2, 600000)

proc.kill()
}
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"license": "MIT",
"dependencies": {
"@libp2p/pubsub-peer-discovery": "^6.0.1",
"@libp2p/pubsub-peer-discovery": "^6.0.2",
"@libp2p/floodsub": "^3.0.3",
"@nodeutils/defaults-deep": "^1.1.0",
"execa": "^6.1.0",
Expand Down

0 comments on commit d281a60

Please sign in to comment.