-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add maxtimeout to dht get (#248)
* fix: add maxtimeout to dht get * chore: add tests
- Loading branch information
1 parent
e052021
commit 69f7264
Showing
3 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters