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: add maxtimeout to dht get #248

Merged
merged 2 commits into from
Sep 19, 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
18 changes: 14 additions & 4 deletions src/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ module.exports = (node) => {

node._dht.put(key, value, callback)
},
get: (key, callback) => {
get: (key, maxTimeout, callback) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

things like timeouts should be in a options object rather than a arg otherwise you will fall into a future of having a huge array of options. @vasco-santos @jacobheun

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I will create a new PR for changing it here, as well as for js-libp2p-kad-dht and will change js-ipfs too.

if (typeof maxTimeout === 'function') {
callback = maxTimeout
maxTimeout = null
}

if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.get(key, callback)
node._dht.get(key, maxTimeout, callback)
},
getMany (key, nVals, callback) {
getMany: (key, nVals, maxTimeout, callback) => {
if (typeof maxTimeout === 'function') {
callback = maxTimeout
maxTimeout = null
}

if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.getMany(key, nVals, callback)
node._dht.getMany(key, nVals, maxTimeout, callback)
}
}
}
166 changes: 166 additions & 0 deletions test/dht.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* eslint-env mocha */

'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const createNode = require('./utils/create-node')

describe('.dht', () => {
describe('enabled', () => {
let nodeA

before(function (done) {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
EXPERIMENTAL: {
dht: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node

// Rewrite validators
nodeA._dht.validators.v = {
func (key, publicKey, callback) {
setImmediate(callback)
},
sign: false
}

// Rewrite selectors
nodeA._dht.selectors.v = () => 0

// Start
nodeA.start(done)
})
})

after((done) => {
nodeA.stop(done)
})

it('should be able to dht.put a value to the DHT', (done) => {
const key = Buffer.from('key')
const value = Buffer.from('value')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()
done()
})
})

it('should be able to dht.get a value from the DHT with a maxTimeout', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.get(key, 3000, (err, res) => {
expect(err).to.not.exist()
expect(res).to.eql(value)
done()
})
})
})

it('should be able to dht.get a value from the DHT with no maxTimeout defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.get(key, (err, res) => {
expect(err).to.not.exist()
expect(res).to.eql(value)
done()
})
})
})

it('should be able to dht.getMany a value from the DHT with a maxTimeout', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.getMany(key, 1, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
})

it('should be able to dht.getMany a value from the DHT with no maxTimeout defined', (done) => {
const key = Buffer.from('/v/hello')
const value = Buffer.from('world')

nodeA.dht.put(key, value, (err) => {
expect(err).to.not.exist()

nodeA.dht.getMany(key, 1, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
})
})

describe('disabled', () => {
let nodeA

before(function (done) {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
EXPERIMENTAL: {
dht: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
nodeA = node
nodeA.start(done)
})
})

after((done) => {
nodeA.stop(done)
})

it('should receive an error on dht.put if the dht is disabled', (done) => {
const key = Buffer.from('key')
const value = Buffer.from('value')

nodeA.dht.put(key, value, (err) => {
expect(err).to.exist()
done()
})
})

it('should receive an error on dht.get if the dht is disabled', (done) => {
const key = Buffer.from('key')

nodeA.dht.get(key, (err) => {
expect(err).to.exist()
done()
})
})

it('should receive an error on dht.getMany if the dht is disabled', (done) => {
const key = Buffer.from('key')

nodeA.dht.getMany(key, 10, (err) => {
expect(err).to.exist()
done()
})
})
})
})
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ require('./content-routing.node')
require('./circuit-relay.node')
require('./multiaddr-trim.node')
require('./stats')
require('./dht.node')