Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 60e089d

Browse files
committed
fix: update dht tests
1 parent 4e6baa0 commit 60e089d

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

js/src/dht/findpeer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = (createCommon, options) => {
4141
nodeA.dht.findpeer(nodeB.peerId.id, (err, peer) => {
4242
expect(err).to.not.exist()
4343
// TODO upgrade the answer, format is weird
44-
expect(peer[0].Responses[0].ID).to.be.equal(nodeB.peerId.id)
44+
expect(peer.responses[0].id).to.be.equal(nodeB.peerId.id)
4545
done()
4646
})
4747
})

js/src/dht/get.js

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const hat = require('hat')
45
const waterfall = require('async/waterfall')
56
const { spawnNodesWithId } = require('../utils/spawn')
67
const { getDescribe, getIt, expect } = require('../utils/mocha')
@@ -30,7 +31,6 @@ module.exports = (createCommon, options) => {
3031

3132
nodeA = nodes[0]
3233
nodeB = nodes[1]
33-
3434
connect(nodeA, nodeB.peerId.addresses[0], done)
3535
})
3636
})
@@ -39,29 +39,54 @@ module.exports = (createCommon, options) => {
3939
after((done) => common.teardown(done))
4040

4141
it('should error when getting a non-existent key from the DHT', (done) => {
42-
nodeA.dht.get('non-existing', { timeout: '100ms' }, (err, value) => {
42+
nodeA.dht.get('non-existing', { timeout: '100ms' }, (err) => {
4343
expect(err).to.be.an.instanceof(Error)
4444
done()
4545
})
4646
})
4747

48-
it('should get a value after it was put on another node', function (done) {
48+
it('should get a value after it was added on another node', function (done) {
4949
this.timeout(80 * 1000)
5050

51-
// TODO - this test needs to keep tryingl instead of the setTimeout
5251
waterfall([
53-
(cb) => nodeB.object.new('unixfs-dir', cb),
52+
(cb) => nodeB.object.put(Buffer.from(hat()), cb),
5453
(dagNode, cb) => setTimeout(() => cb(null, dagNode), 20000),
5554
(dagNode, cb) => {
56-
const multihash = dagNode.toJSON().multihash
55+
const hash = dagNode.toJSON().hash
5756

58-
nodeA.dht.get(multihash, cb)
57+
nodeA.object.get(hash, cb)
5958
},
6059
(result, cb) => {
61-
expect(result).to.eql('')
60+
expect(result).to.exist()
6261
cb()
6362
}
6463
], done)
6564
})
65+
66+
it('should get a value after it was put on another node', function (done) {
67+
this.timeout(80 * 1000)
68+
const multihash = Buffer.from('/v/hello')
69+
const data = Buffer.from('data')
70+
71+
// Rewrite validators to simply validate the record
72+
nodeA._libp2pNode._dht.validators.v = nodeB._libp2pNode._dht.validators.v = {
73+
func (key, publicKey, callback) {
74+
setImmediate(callback)
75+
},
76+
sign: false
77+
}
78+
79+
// Rewrite selectors to select first received record
80+
nodeA._libp2pNode._dht.selectors.v = () => 0
81+
82+
waterfall([
83+
(cb) => nodeB.dht.put(multihash, data, cb),
84+
(cb) => nodeA.dht.get(multihash, (err, res) => {
85+
expect(err).to.not.exist()
86+
expect(res).to.eql(data)
87+
cb()
88+
})
89+
], done)
90+
})
6691
})
6792
}

js/src/dht/put.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,50 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const { spawnNodesWithId } = require('../utils/spawn')
45
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
const { connect } = require('../utils/swarm')
57

68
module.exports = (createCommon, options) => {
79
const describe = getDescribe(options)
810
const it = getIt(options)
911
const common = createCommon()
1012

1113
describe('.dht.put', function () {
14+
this.timeout(80 * 1000)
15+
16+
let nodeA
17+
let nodeB
18+
1219
before(function (done) {
1320
// CI takes longer to instantiate the daemon, so we need to increase the
1421
// timeout for the before step
1522
this.timeout(60 * 1000)
1623

1724
common.setup((err, factory) => {
1825
expect(err).to.not.exist()
19-
done()
26+
27+
spawnNodesWithId(2, factory, (err, nodes) => {
28+
expect(err).to.not.exist()
29+
30+
nodeA = nodes[0]
31+
nodeB = nodes[1]
32+
connect(nodeA, nodeB.peerId.addresses[0], done)
33+
})
2034
})
2135
})
2236

2337
after((done) => common.teardown(done))
2438

25-
it.skip('should put a value on the DHT', (done) => {
26-
// TODO: implement me
39+
it('should put a value on the DHT and it become provided by the peer', (done) => {
40+
this.timeout(80 * 1000)
41+
const key = Buffer.from('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
42+
const data = Buffer.from('data')
43+
44+
nodeA.dht.put(key, data, (err) => {
45+
expect(err).to.not.exist()
46+
done()
47+
})
2748
})
2849
})
2950
}

0 commit comments

Comments
 (0)