Skip to content

Commit 0a02207

Browse files
authored
chore: add discovery example tests (#841)
1 parent 0b854a9 commit 0a02207

File tree

6 files changed

+109
-9
lines changed

6 files changed

+109
-9
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ jobs:
7979
- uses: actions/checkout@v2
8080
- run: yarn
8181
- run: cd examples && yarn && npm run test -- libp2p-in-the-browser
82+
test-discovery-mechanisms-example:
83+
needs: check
84+
runs-on: macos-latest
85+
steps:
86+
- uses: actions/checkout@v2
87+
- run: yarn
88+
- run: cd examples && yarn && npm run test -- discovery-mechanisms

examples/discovery-mechanisms/1.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ const Mplex = require('libp2p-mplex')
77
const { NOISE } = require('libp2p-noise')
88
const Bootstrap = require('libp2p-bootstrap')
99

10-
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
11-
const bootstrapers = [
12-
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
13-
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
14-
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
15-
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
16-
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
17-
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
18-
]
10+
const bootstrapers = require('./bootstrapers')
1911

2012
;(async () => {
2113
const node = await Libp2p.create({
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-nodejs.json
4+
const bootstrapers = [
5+
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
6+
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
7+
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
8+
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
9+
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
10+
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
11+
]
12+
13+
module.exports = bootstrapers
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const execa = require('execa')
5+
const pWaitFor = require('p-wait-for')
6+
const uint8ArrayToString = require('uint8arrays/to-string')
7+
const bootstrapers = require('./bootstrapers')
8+
9+
const discoveredCopy = 'Discovered:'
10+
const connectedCopy = 'Connection established to:'
11+
12+
async function test () {
13+
const discoveredNodes = []
14+
const connectedNodes = []
15+
16+
process.stdout.write('1.js\n')
17+
18+
const proc = execa('node', [path.join(__dirname, '1.js')], {
19+
cwd: path.resolve(__dirname),
20+
all: true
21+
})
22+
23+
proc.all.on('data', async (data) => {
24+
process.stdout.write(data)
25+
const line = uint8ArrayToString(data)
26+
27+
// Discovered or Connected
28+
if (line.includes(discoveredCopy)) {
29+
const id = line.trim().split(discoveredCopy)[1]
30+
discoveredNodes.push(id)
31+
} else if (line.includes(connectedCopy)) {
32+
const id = line.trim().split(connectedCopy)[1]
33+
connectedNodes.push(id)
34+
}
35+
})
36+
37+
await pWaitFor(() => discoveredNodes.length === bootstrapers.length && connectedNodes.length === bootstrapers.length)
38+
39+
proc.kill()
40+
}
41+
42+
module.exports = test
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const execa = require('execa')
5+
const pWaitFor = require('p-wait-for')
6+
const uint8ArrayToString = require('uint8arrays/to-string')
7+
8+
const discoveredCopy = 'Discovered:'
9+
10+
async function test() {
11+
const discoveredNodes = []
12+
13+
process.stdout.write('2.js\n')
14+
15+
const proc = execa('node', [path.join(__dirname, '2.js')], {
16+
cwd: path.resolve(__dirname),
17+
all: true
18+
})
19+
20+
proc.all.on('data', async (data) => {
21+
process.stdout.write(data)
22+
const line = uint8ArrayToString(data)
23+
24+
if (line.includes(discoveredCopy)) {
25+
const id = line.trim().split(discoveredCopy)[1]
26+
discoveredNodes.push(id)
27+
}
28+
})
29+
30+
await pWaitFor(() => discoveredNodes.length === 2)
31+
32+
proc.kill()
33+
}
34+
35+
module.exports = test

examples/discovery-mechanisms/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const test1 = require('./test-1')
4+
const test2 = require('./test-2')
5+
6+
async function test () {
7+
await test1()
8+
await test2()
9+
}
10+
11+
module.exports = test

0 commit comments

Comments
 (0)