From 69f7264123a3521d28a635b14d77af82ffdf98f6 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 19 Sep 2018 18:31:36 +0100 Subject: [PATCH] fix: add maxtimeout to dht get (#248) * fix: add maxtimeout to dht get * chore: add tests --- src/dht.js | 18 +++-- test/dht.node.js | 166 +++++++++++++++++++++++++++++++++++++++++++++++ test/node.js | 1 + 3 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 test/dht.node.js diff --git a/src/dht.js b/src/dht.js index 504db7a643..10326aa9dd 100644 --- a/src/dht.js +++ b/src/dht.js @@ -9,19 +9,29 @@ module.exports = (node) => { node._dht.put(key, value, callback) }, - get: (key, callback) => { + get: (key, maxTimeout, callback) => { + 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) } } } diff --git a/test/dht.node.js b/test/dht.node.js new file mode 100644 index 0000000000..0231eb7a3f --- /dev/null +++ b/test/dht.node.js @@ -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() + }) + }) + }) +}) diff --git a/test/node.js b/test/node.js index e820ddf610..ccfa009962 100644 --- a/test/node.js +++ b/test/node.js @@ -10,3 +10,4 @@ require('./content-routing.node') require('./circuit-relay.node') require('./multiaddr-trim.node') require('./stats') +require('./dht.node')