From 1b60f24708181a21c8f92ea4d5492fca5ee4678b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 16 May 2018 21:11:53 +0100 Subject: [PATCH 1/3] fix: wait until nodes are connected before starting ping tests License: MIT Signed-off-by: Alan Shaw --- js/src/ping.js | 21 ++++++++++----- js/src/utils/connections.js | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 js/src/utils/connections.js diff --git a/js/src/ping.js b/js/src/ping.js index a9bde93d8..d40a5aa84 100644 --- a/js/src/ping.js +++ b/js/src/ping.js @@ -6,7 +6,9 @@ const dirtyChai = require('dirty-chai') const pull = require('pull-stream') const pump = require('pump') const { Writable } = require('stream') +const series = require('async/series') const { spawnNodesWithId } = require('./utils/spawn') +const { waitUntilConnected } = require('./utils/connections') const expect = chai.expect chai.use(dirtyChai) @@ -21,7 +23,7 @@ function expectIsPingResponse (obj) { } module.exports = (common) => { - describe('.ping', function () { + describe.only('.ping', function () { let ipfsdA let ipfsdB @@ -31,12 +33,17 @@ module.exports = (common) => { common.setup((err, factory) => { if (err) return done(err) - spawnNodesWithId(2, factory, (err, nodes) => { - if (err) return done(err) - ipfsdA = nodes[0] - ipfsdB = nodes[1] - done() - }) + series([ + (cb) => { + spawnNodesWithId(2, factory, (err, nodes) => { + if (err) return cb(err) + ipfsdA = nodes[0] + ipfsdB = nodes[1] + cb() + }) + }, + (cb) => waitUntilConnected(ipfsdA, ipfsdB, cb) + ], done) }) }) diff --git a/js/src/utils/connections.js b/js/src/utils/connections.js new file mode 100644 index 000000000..44697e170 --- /dev/null +++ b/js/src/utils/connections.js @@ -0,0 +1,51 @@ +const waterfall = require('async/waterfall') + +function waitUntilConnected (fromNode, toNode, opts, cb) { + if (typeof opts === 'function') { + cb = opts + opts = {} + } + + opts = opts || {} + opts.timeout = opts.timeout || 15000 + opts.interval = opts.interval || 1000 + + const startTime = Date.now() + const checkConnected = () => { + isConnected(fromNode, toNode, (err, connected) => { + if (err) return cb(err) + if (connected) return cb() + + if (Date.now() > startTime + opts.timeout) { + return cb(new Error('timeout waiting for connected nodes')) + } + + setTimeout(checkConnected, opts.interval) + }) + } + + checkConnected() +} + +exports.waitUntilConnected = waitUntilConnected + +function isConnected (fromNode, toNode, cb) { + waterfall([ + (cb) => { + if (toNode.peerId) return cb(null, toNode.peerId) + toNode.id((err, id) => { + if (err) return cb(err) + toNode.peerId = id + cb(null, id) + }) + }, + (toPeerId, cb) => { + fromNode.swarm.peers((err, peers) => { + if (err) return cb(err) + cb(null, peers.some((p) => p.peer.toJSON().id === toPeerId.id)) + }) + } + ], cb) +} + +exports.isConnected = isConnected From 45fab1cc237ed7a6488d4741e9e703534b1a20c3 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 16 May 2018 21:20:45 +0100 Subject: [PATCH 2/3] fix: remove .only License: MIT Signed-off-by: Alan Shaw --- js/src/ping.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/ping.js b/js/src/ping.js index d40a5aa84..14dea0daa 100644 --- a/js/src/ping.js +++ b/js/src/ping.js @@ -23,7 +23,7 @@ function expectIsPingResponse (obj) { } module.exports = (common) => { - describe.only('.ping', function () { + describe('.ping', function () { let ipfsdA let ipfsdB From 9cba111cd951c64d2d18cd477709300ab7e31453 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 16 May 2018 21:31:27 +0100 Subject: [PATCH 3/3] fix: increase timeouts License: MIT Signed-off-by: Alan Shaw --- js/src/ping.js | 2 +- js/src/utils/connections.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/src/ping.js b/js/src/ping.js index 14dea0daa..d47fed448 100644 --- a/js/src/ping.js +++ b/js/src/ping.js @@ -28,7 +28,7 @@ module.exports = (common) => { let ipfsdB before(function (done) { - this.timeout(30 * 1000) + this.timeout(60 * 1000) common.setup((err, factory) => { if (err) return done(err) diff --git a/js/src/utils/connections.js b/js/src/utils/connections.js index 44697e170..0fd2bb844 100644 --- a/js/src/utils/connections.js +++ b/js/src/utils/connections.js @@ -7,7 +7,7 @@ function waitUntilConnected (fromNode, toNode, opts, cb) { } opts = opts || {} - opts.timeout = opts.timeout || 15000 + opts.timeout = opts.timeout || 30000 opts.interval = opts.interval || 1000 const startTime = Date.now()