Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont call callback before it's properly set #278

Merged
merged 1 commit into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/content-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/peer-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions test/content-routing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
})
25 changes: 25 additions & 0 deletions test/peer-routing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
})