Skip to content

Commit 69f7264

Browse files
vasco-santosjacobheun
authored andcommitted
fix: add maxtimeout to dht get (#248)
* fix: add maxtimeout to dht get * chore: add tests
1 parent e052021 commit 69f7264

File tree

3 files changed

+181
-4
lines changed

3 files changed

+181
-4
lines changed

src/dht.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@ module.exports = (node) => {
99

1010
node._dht.put(key, value, callback)
1111
},
12-
get: (key, callback) => {
12+
get: (key, maxTimeout, callback) => {
13+
if (typeof maxTimeout === 'function') {
14+
callback = maxTimeout
15+
maxTimeout = null
16+
}
17+
1318
if (!node._dht) {
1419
return callback(new Error('DHT is not available'))
1520
}
1621

17-
node._dht.get(key, callback)
22+
node._dht.get(key, maxTimeout, callback)
1823
},
19-
getMany (key, nVals, callback) {
24+
getMany: (key, nVals, maxTimeout, callback) => {
25+
if (typeof maxTimeout === 'function') {
26+
callback = maxTimeout
27+
maxTimeout = null
28+
}
29+
2030
if (!node._dht) {
2131
return callback(new Error('DHT is not available'))
2232
}
2333

24-
node._dht.getMany(key, nVals, callback)
34+
node._dht.getMany(key, nVals, maxTimeout, callback)
2535
}
2636
}
2737
}

test/dht.node.js

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* eslint-env mocha */
2+
3+
'use strict'
4+
5+
const chai = require('chai')
6+
chai.use(require('dirty-chai'))
7+
const expect = chai.expect
8+
9+
const createNode = require('./utils/create-node')
10+
11+
describe('.dht', () => {
12+
describe('enabled', () => {
13+
let nodeA
14+
15+
before(function (done) {
16+
createNode('/ip4/0.0.0.0/tcp/0', {
17+
config: {
18+
EXPERIMENTAL: {
19+
dht: true
20+
}
21+
}
22+
}, (err, node) => {
23+
expect(err).to.not.exist()
24+
nodeA = node
25+
26+
// Rewrite validators
27+
nodeA._dht.validators.v = {
28+
func (key, publicKey, callback) {
29+
setImmediate(callback)
30+
},
31+
sign: false
32+
}
33+
34+
// Rewrite selectors
35+
nodeA._dht.selectors.v = () => 0
36+
37+
// Start
38+
nodeA.start(done)
39+
})
40+
})
41+
42+
after((done) => {
43+
nodeA.stop(done)
44+
})
45+
46+
it('should be able to dht.put a value to the DHT', (done) => {
47+
const key = Buffer.from('key')
48+
const value = Buffer.from('value')
49+
50+
nodeA.dht.put(key, value, (err) => {
51+
expect(err).to.not.exist()
52+
done()
53+
})
54+
})
55+
56+
it('should be able to dht.get a value from the DHT with a maxTimeout', (done) => {
57+
const key = Buffer.from('/v/hello')
58+
const value = Buffer.from('world')
59+
60+
nodeA.dht.put(key, value, (err) => {
61+
expect(err).to.not.exist()
62+
63+
nodeA.dht.get(key, 3000, (err, res) => {
64+
expect(err).to.not.exist()
65+
expect(res).to.eql(value)
66+
done()
67+
})
68+
})
69+
})
70+
71+
it('should be able to dht.get a value from the DHT with no maxTimeout defined', (done) => {
72+
const key = Buffer.from('/v/hello')
73+
const value = Buffer.from('world')
74+
75+
nodeA.dht.put(key, value, (err) => {
76+
expect(err).to.not.exist()
77+
78+
nodeA.dht.get(key, (err, res) => {
79+
expect(err).to.not.exist()
80+
expect(res).to.eql(value)
81+
done()
82+
})
83+
})
84+
})
85+
86+
it('should be able to dht.getMany a value from the DHT with a maxTimeout', (done) => {
87+
const key = Buffer.from('/v/hello')
88+
const value = Buffer.from('world')
89+
90+
nodeA.dht.put(key, value, (err) => {
91+
expect(err).to.not.exist()
92+
93+
nodeA.dht.getMany(key, 1, (err, res) => {
94+
expect(err).to.not.exist()
95+
expect(res).to.exist()
96+
done()
97+
})
98+
})
99+
})
100+
101+
it('should be able to dht.getMany a value from the DHT with no maxTimeout defined', (done) => {
102+
const key = Buffer.from('/v/hello')
103+
const value = Buffer.from('world')
104+
105+
nodeA.dht.put(key, value, (err) => {
106+
expect(err).to.not.exist()
107+
108+
nodeA.dht.getMany(key, 1, (err, res) => {
109+
expect(err).to.not.exist()
110+
expect(res).to.exist()
111+
done()
112+
})
113+
})
114+
})
115+
})
116+
117+
describe('disabled', () => {
118+
let nodeA
119+
120+
before(function (done) {
121+
createNode('/ip4/0.0.0.0/tcp/0', {
122+
config: {
123+
EXPERIMENTAL: {
124+
dht: false
125+
}
126+
}
127+
}, (err, node) => {
128+
expect(err).to.not.exist()
129+
nodeA = node
130+
nodeA.start(done)
131+
})
132+
})
133+
134+
after((done) => {
135+
nodeA.stop(done)
136+
})
137+
138+
it('should receive an error on dht.put if the dht is disabled', (done) => {
139+
const key = Buffer.from('key')
140+
const value = Buffer.from('value')
141+
142+
nodeA.dht.put(key, value, (err) => {
143+
expect(err).to.exist()
144+
done()
145+
})
146+
})
147+
148+
it('should receive an error on dht.get if the dht is disabled', (done) => {
149+
const key = Buffer.from('key')
150+
151+
nodeA.dht.get(key, (err) => {
152+
expect(err).to.exist()
153+
done()
154+
})
155+
})
156+
157+
it('should receive an error on dht.getMany if the dht is disabled', (done) => {
158+
const key = Buffer.from('key')
159+
160+
nodeA.dht.getMany(key, 10, (err) => {
161+
expect(err).to.exist()
162+
done()
163+
})
164+
})
165+
})
166+
})

test/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ require('./content-routing.node')
1010
require('./circuit-relay.node')
1111
require('./multiaddr-trim.node')
1212
require('./stats')
13+
require('./dht.node')

0 commit comments

Comments
 (0)