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

Commit

Permalink
feat: add utils to spawn multiple nodes and get their ID
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw committed May 15, 2018
1 parent d3986c0 commit e77a2f6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 70 deletions.
11 changes: 5 additions & 6 deletions js/src/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const CID = require('cids')
const { spawnNodeWithId } = require('./utils/spawn')

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

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {

spawnNodeWithId(factory, (err, node) => {
expect(err).to.not.exist()
ipfs = node
ipfs.id((err, id) => {
expect(err).to.not.exist()
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
done()
})
})
})
Expand Down
24 changes: 3 additions & 21 deletions js/src/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,9 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const waterfall = require('async/waterfall')
const series = require('async/series')
const parallel = require('async/parallel')
const CID = require('cids')

function spawnWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, peerId) => {
if (err) {
return cb(err)
}
node.peerId = peerId
cb(null, node)
})
], callback)
}
const { spawnNodesWithId } = require('./utils/spawn')

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

common.setup((err, factory) => {
expect(err).to.not.exist()
series([
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb)
], (err, nodes) => {

spawnNodesWithId(5, factory, (err, nodes) => {
expect(err).to.not.exist()

nodeA = nodes[0]
Expand Down
10 changes: 4 additions & 6 deletions js/src/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const hat = require('hat')
const { spawnNodeWithId } = require('./utils/spawn')

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

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
spawnNodeWithId(factory, (err, node) => {
expect(err).to.not.exist()
ipfs = node
ipfs.id((err, id) => {
expect(err).to.not.exist()
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
done()
})
})
})
Expand Down
10 changes: 4 additions & 6 deletions js/src/miscellaneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { spawnNodeWithId } = require('./utils/spawn')

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

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
spawnNodeWithId(factory, (err, node) => {
expect(err).to.not.exist()
ipfs = node
ipfs.id((err, id) => {
expect(err).to.not.exist()
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
withGo = node.peerId.agentVersion.startsWith('go-ipfs')
done()
})
})
})
Expand Down
21 changes: 2 additions & 19 deletions js/src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const expect = chai.expect
chai.use(dirtyChai)
const series = require('async/series')
const each = require('async/each')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const whilst = require('async/whilst')
const hat = require('hat')
const { spawnNodesWithId } = require('./utils/spawn')

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

function spawnWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, res) => {
if (err) {
return cb(err)
}
node.peerId = res
cb(null, node)
})
], callback)
}

function makeCheck (n, done) {
let i = 0
return (err) => {
Expand Down Expand Up @@ -83,11 +70,7 @@ module.exports = (common) => {
return done(err)
}

series([
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb),
(cb) => spawnWithId(factory, cb)
], (err, nodes) => {
spawnNodesWithId(3, factory, (err, nodes) => {
if (err) {
return done(err)
}
Expand Down
20 changes: 8 additions & 12 deletions js/src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const PeerId = require('peer-id')
const os = require('os')
const path = require('path')
const hat = require('hat')
const { spawnNodes } = require('./utils/spawn')

module.exports = (common) => {
describe('.swarm', function () {
Expand All @@ -30,18 +31,13 @@ module.exports = (common) => {
common.setup((err, factory) => {
expect(err).to.not.exist()
factoryInstance = factory
series([
(cb) => factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfsA = node
cb()
}),
(cb) => factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfsB = node
cb()
})
], done)

spawnNodes(2, factory, (err, nodes) => {
expect(err).to.not.exist()
ipfsA = nodes[0]
ipfsB = nodes[1]
done()
})
})
})

Expand Down
30 changes: 30 additions & 0 deletions js/src/utils/spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const waterfall = require('async/waterfall')
const times = require('async/times')

// Spawn a node, get it's id and set it as `peerId` on the node
function spawnNodeWithId (factory, callback) {
waterfall([
(cb) => factory.spawnNode(cb),
(node, cb) => node.id((err, id) => {
if (err) return cb(err)
node.peerId = id
cb(null, node)
})
], callback)
}

exports.spawnNodeWithId = spawnNodeWithId

// Spawn n nodes
function spawnNodes (n, factory, callback) {
times(n, (_, cb) => factory.spawnNode(cb), callback)
}

exports.spawnNodes = spawnNodes

// Spawn n nodes, getting their id's and setting them as `peerId` on the nodes
function spawnNodesWithId (n, factory, callback) {
times(n, (_, cb) => spawnNodeWithId(factory, cb), callback)
}

exports.spawnNodesWithId = spawnNodesWithId

0 comments on commit e77a2f6

Please sign in to comment.