Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit e77a2f6

Browse files
committed
feat: add utils to spawn multiple nodes and get their ID
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent d3986c0 commit e77a2f6

File tree

7 files changed

+56
-70
lines changed

7 files changed

+56
-70
lines changed

Diff for: js/src/dag.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const dagPB = require('ipld-dag-pb')
1313
const DAGNode = dagPB.DAGNode
1414
const dagCBOR = require('ipld-dag-cbor')
1515
const CID = require('cids')
16+
const { spawnNodeWithId } = require('./utils/spawn')
1617

1718
module.exports = (common) => {
1819
describe('.dag', () => {
@@ -26,14 +27,12 @@ module.exports = (common) => {
2627

2728
common.setup((err, factory) => {
2829
expect(err).to.not.exist()
29-
factory.spawnNode((err, node) => {
30+
31+
spawnNodeWithId(factory, (err, node) => {
3032
expect(err).to.not.exist()
3133
ipfs = node
32-
ipfs.id((err, id) => {
33-
expect(err).to.not.exist()
34-
withGo = id.agentVersion.startsWith('go-ipfs')
35-
done()
36-
})
34+
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
35+
done()
3736
})
3837
})
3938
})

Diff for: js/src/dht.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,9 @@ const dirtyChai = require('dirty-chai')
66
const expect = chai.expect
77
chai.use(dirtyChai)
88
const waterfall = require('async/waterfall')
9-
const series = require('async/series')
109
const parallel = require('async/parallel')
1110
const CID = require('cids')
12-
13-
function spawnWithId (factory, callback) {
14-
waterfall([
15-
(cb) => factory.spawnNode(cb),
16-
(node, cb) => node.id((err, peerId) => {
17-
if (err) {
18-
return cb(err)
19-
}
20-
node.peerId = peerId
21-
cb(null, node)
22-
})
23-
], callback)
24-
}
11+
const { spawnNodesWithId } = require('./utils/spawn')
2512

2613
module.exports = (common) => {
2714
describe('.dht', function () {
@@ -40,13 +27,8 @@ module.exports = (common) => {
4027

4128
common.setup((err, factory) => {
4229
expect(err).to.not.exist()
43-
series([
44-
(cb) => spawnWithId(factory, cb),
45-
(cb) => spawnWithId(factory, cb),
46-
(cb) => spawnWithId(factory, cb),
47-
(cb) => spawnWithId(factory, cb),
48-
(cb) => spawnWithId(factory, cb)
49-
], (err, nodes) => {
30+
31+
spawnNodesWithId(5, factory, (err, nodes) => {
5032
expect(err).to.not.exist()
5133

5234
nodeA = nodes[0]

Diff for: js/src/key.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const dirtyChai = require('dirty-chai')
88
const expect = chai.expect
99
chai.use(dirtyChai)
1010
const hat = require('hat')
11+
const { spawnNodeWithId } = require('./utils/spawn')
1112

1213
module.exports = (common) => {
1314
describe('.key', () => {
@@ -25,14 +26,11 @@ module.exports = (common) => {
2526

2627
common.setup((err, factory) => {
2728
expect(err).to.not.exist()
28-
factory.spawnNode((err, node) => {
29+
spawnNodeWithId(factory, (err, node) => {
2930
expect(err).to.not.exist()
3031
ipfs = node
31-
ipfs.id((err, id) => {
32-
expect(err).to.not.exist()
33-
withGo = id.agentVersion.startsWith('go-ipfs')
34-
done()
35-
})
32+
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
33+
done()
3634
})
3735
})
3836
})

Diff for: js/src/miscellaneous.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const chai = require('chai')
77
const dirtyChai = require('dirty-chai')
88
const expect = chai.expect
99
chai.use(dirtyChai)
10+
const { spawnNodeWithId } = require('./utils/spawn')
1011

1112
module.exports = (common) => {
1213
describe('.miscellaneous', () => {
@@ -20,14 +21,11 @@ module.exports = (common) => {
2021

2122
common.setup((err, factory) => {
2223
expect(err).to.not.exist()
23-
factory.spawnNode((err, node) => {
24+
spawnNodeWithId(factory, (err, node) => {
2425
expect(err).to.not.exist()
2526
ipfs = node
26-
ipfs.id((err, id) => {
27-
expect(err).to.not.exist()
28-
withGo = id.agentVersion.startsWith('go-ipfs')
29-
done()
30-
})
27+
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
28+
done()
3129
})
3230
})
3331
})

Diff for: js/src/pubsub.js

+2-19
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const expect = chai.expect
88
chai.use(dirtyChai)
99
const series = require('async/series')
1010
const each = require('async/each')
11-
const waterfall = require('async/waterfall')
1211
const parallel = require('async/parallel')
1312
const whilst = require('async/whilst')
1413
const hat = require('hat')
14+
const { spawnNodesWithId } = require('./utils/spawn')
1515

1616
// On Browsers it will be false, but the tests currently aren't run
1717
// there anyway
@@ -36,19 +36,6 @@ function waitForPeers (ipfs, topic, peersToWait, callback) {
3636
}, 500)
3737
}
3838

39-
function spawnWithId (factory, callback) {
40-
waterfall([
41-
(cb) => factory.spawnNode(cb),
42-
(node, cb) => node.id((err, res) => {
43-
if (err) {
44-
return cb(err)
45-
}
46-
node.peerId = res
47-
cb(null, node)
48-
})
49-
], callback)
50-
}
51-
5239
function makeCheck (n, done) {
5340
let i = 0
5441
return (err) => {
@@ -83,11 +70,7 @@ module.exports = (common) => {
8370
return done(err)
8471
}
8572

86-
series([
87-
(cb) => spawnWithId(factory, cb),
88-
(cb) => spawnWithId(factory, cb),
89-
(cb) => spawnWithId(factory, cb)
90-
], (err, nodes) => {
73+
spawnNodesWithId(3, factory, (err, nodes) => {
9174
if (err) {
9275
return done(err)
9376
}

Diff for: js/src/swarm.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const PeerId = require('peer-id')
1313
const os = require('os')
1414
const path = require('path')
1515
const hat = require('hat')
16+
const { spawnNodes } = require('./utils/spawn')
1617

1718
module.exports = (common) => {
1819
describe('.swarm', function () {
@@ -30,18 +31,13 @@ module.exports = (common) => {
3031
common.setup((err, factory) => {
3132
expect(err).to.not.exist()
3233
factoryInstance = factory
33-
series([
34-
(cb) => factory.spawnNode((err, node) => {
35-
expect(err).to.not.exist()
36-
ipfsA = node
37-
cb()
38-
}),
39-
(cb) => factory.spawnNode((err, node) => {
40-
expect(err).to.not.exist()
41-
ipfsB = node
42-
cb()
43-
})
44-
], done)
34+
35+
spawnNodes(2, factory, (err, nodes) => {
36+
expect(err).to.not.exist()
37+
ipfsA = nodes[0]
38+
ipfsB = nodes[1]
39+
done()
40+
})
4541
})
4642
})
4743

Diff for: js/src/utils/spawn.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const waterfall = require('async/waterfall')
2+
const times = require('async/times')
3+
4+
// Spawn a node, get it's id and set it as `peerId` on the node
5+
function spawnNodeWithId (factory, callback) {
6+
waterfall([
7+
(cb) => factory.spawnNode(cb),
8+
(node, cb) => node.id((err, id) => {
9+
if (err) return cb(err)
10+
node.peerId = id
11+
cb(null, node)
12+
})
13+
], callback)
14+
}
15+
16+
exports.spawnNodeWithId = spawnNodeWithId
17+
18+
// Spawn n nodes
19+
function spawnNodes (n, factory, callback) {
20+
times(n, (_, cb) => factory.spawnNode(cb), callback)
21+
}
22+
23+
exports.spawnNodes = spawnNodes
24+
25+
// Spawn n nodes, getting their id's and setting them as `peerId` on the nodes
26+
function spawnNodesWithId (n, factory, callback) {
27+
times(n, (_, cb) => spawnNodeWithId(factory, cb), callback)
28+
}
29+
30+
exports.spawnNodesWithId = spawnNodesWithId

0 commit comments

Comments
 (0)