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
This commit is contained in:
Marin Petrunić 2022-09-05 16:17:35 +02:00 committed by GitHub
parent fc2224a1e8
commit d281a60dac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 9 deletions

View File

@ -77,4 +77,4 @@ const createNode = async (bootstrappers) => {
node1.start(), node1.start(),
node2.start() node2.start()
]) ])
})(); })()

View File

@ -27,7 +27,7 @@ export async function test () {
}) })
}) })
await pWaitFor(() => discoveredNodes > 1) await pWaitFor(() => discoveredNodes > 1, 600000)
proc.kill() proc.kill()
} }

View File

@ -7,7 +7,7 @@ import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url)) const __dirname = path.dirname(fileURLToPath(import.meta.url))
export async function test () { export async function test () {
let discoveredNodes = 0 const discoveredPeers = []
process.stdout.write('3.js\n') process.stdout.write('3.js\n')
@ -19,15 +19,20 @@ export async function test () {
proc.all.on('data', async (data) => { proc.all.on('data', async (data) => {
process.stdout.write(data) process.stdout.write(data)
const str = uint8ArrayToString(data) const str = uint8ArrayToString(data)
const discoveredPeersRegex = /Peer\s+(?<Peer1>[^\s]+)\s+discovered:\s+(?<Peer2>[^\s]+)/
str.split('\n').forEach(line => { str.split('\n').forEach(line => {
if (line.includes('discovered:')) { const peers = line.match(discoveredPeersRegex)
discoveredNodes++ 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() proc.kill()
} }

View File

@ -9,7 +9,7 @@
}, },
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@libp2p/pubsub-peer-discovery": "^6.0.1", "@libp2p/pubsub-peer-discovery": "^6.0.2",
"@libp2p/floodsub": "^3.0.3", "@libp2p/floodsub": "^3.0.3",
"@nodeutils/defaults-deep": "^1.1.0", "@nodeutils/defaults-deep": "^1.1.0",
"execa": "^6.1.0", "execa": "^6.1.0",