From cc727f519c064c3c261b4b9e9b6aba7a1f4b983c Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Mon, 5 Nov 2018 13:56:45 +0100 Subject: [PATCH] fix: dont call callback before it's properly set --- src/content-routing.js | 8 ++++---- src/peer-routing.js | 8 ++++---- test/content-routing.node.js | 25 +++++++++++++++++++++++++ test/peer-routing.node.js | 25 +++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/content-routing.js b/src/content-routing.js index b9f507f9f5..6c94bd7327 100644 --- a/src/content-routing.js +++ b/src/content-routing.js @@ -24,10 +24,6 @@ module.exports = (node) => { * @returns {void} */ findProviders: (key, options, callback) => { - if (!routers.length) { - return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')) - } - if (typeof options === 'function') { callback = options options = {} @@ -37,6 +33,10 @@ module.exports = (node) => { } } + if (!routers.length) { + return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')) + } + const tasks = routers.map((router) => { return (cb) => router.findProviders(key, options, (err, results) => { if (err) { diff --git a/src/peer-routing.js b/src/peer-routing.js index 0d5f5dbcb5..b69787a818 100644 --- a/src/peer-routing.js +++ b/src/peer-routing.js @@ -22,15 +22,15 @@ module.exports = (node) => { * @returns {void} */ findPeer: (id, options, callback) => { - if (!routers.length) { - callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')) - } - if (typeof options === 'function') { callback = options options = {} } + if (!routers.length) { + callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')) + } + const tasks = routers.map((router) => { return (cb) => router.findPeer(id, options, (err, result) => { if (err) { diff --git a/test/content-routing.node.js b/test/content-routing.node.js index 14a64a774e..7f8adb07fb 100644 --- a/test/content-routing.node.js +++ b/test/content-routing.node.js @@ -367,4 +367,29 @@ describe('.contentRouting', () => { }) }) }) + + describe('no routers', () => { + let nodeA + before((done) => { + createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { + expect(err).to.not.exist() + nodeA = node + done() + }) + }) + + it('.findProviders should return an error with no options', (done) => { + nodeA.contentRouting.findProviders('a cid', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('.findProviders should return an error with options', (done) => { + nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err) => { + expect(err).to.exist() + done() + }) + }) + }) }) diff --git a/test/peer-routing.node.js b/test/peer-routing.node.js index 7d728ccd9d..37319d86d0 100644 --- a/test/peer-routing.node.js +++ b/test/peer-routing.node.js @@ -266,4 +266,29 @@ describe('.peerRouting', () => { }) }) }) + + describe('no routers', () => { + let nodeA + before((done) => { + createNode('/ip4/0.0.0.0/tcp/0', (err, node) => { + expect(err).to.not.exist() + nodeA = node + done() + }) + }) + + it('.findPeer should return an error with no options', (done) => { + nodeA.peerRouting.findPeer('a cid', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('.findPeer should return an error with options', (done) => { + nodeA.peerRouting.findPeer('a cid', { maxTimeout: 5000 }, (err) => { + expect(err).to.exist() + done() + }) + }) + }) })