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

Commit

Permalink
fix: dht tests (#486)
Browse files Browse the repository at this point in the history
closes #383

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw authored May 31, 2019
1 parent 74bccc2 commit 2952672
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
18 changes: 8 additions & 10 deletions src/dht/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'

const hat = require('hat')
const waterfall = require('async/waterfall')
const { spawnNodesWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
Expand Down Expand Up @@ -43,7 +44,7 @@ module.exports = (createCommon, options) => {
})

it('should error when getting a non-existent key from the DHT', (done) => {
nodeA.dht.get('non-existing', { timeout: '100ms' }, (err, value) => {
nodeA.dht.get('non-existing', { timeout: 100 }, (err, value) => {
expect(err).to.be.an.instanceof(Error)
done()
})
Expand All @@ -52,17 +53,14 @@ module.exports = (createCommon, options) => {
it('should get a value after it was put on another node', function (done) {
this.timeout(80 * 1000)

// TODO - this test needs to keep tryingl instead of the setTimeout
waterfall([
(cb) => nodeB.object.new('unixfs-dir', cb),
(dagNode, cb) => setTimeout(() => cb(null, dagNode), 20000),
(dagNode, cb) => {
const multihash = dagNode.toJSON().multihash
const key = Buffer.from(hat())
const value = Buffer.from(hat())

nodeA.dht.get(multihash, cb)
},
waterfall([
cb => nodeB.dht.put(key, value, cb),
cb => nodeA.dht.get(key, cb),
(result, cb) => {
expect(result).to.eql('')
expect(result).to.eql(value)
cb()
}
], done)
Expand Down
2 changes: 1 addition & 1 deletion src/dht/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const { createSuite } = require('../utils/suite')

const tests = {
get: require('./get'),
put: require('./put'),
get: require('./get'),
findPeer: require('./find-peer'),
provide: require('./provide'),
findProvs: require('./find-provs'),
Expand Down
27 changes: 24 additions & 3 deletions src/dht/put.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
/* eslint-env mocha */
'use strict'

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

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.dht.put', function () {
this.timeout(80 * 1000)

let nodeA
let nodeB

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
done()

spawnNodesWithId(2, factory, (err, nodes) => {
expect(err).to.not.exist()

nodeA = nodes[0]
nodeB = nodes[1]
connect(nodeA, nodeB.peerId.addresses[0], done)
})
})
})

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

it.skip('should put a value on the DHT', (done) => {
// TODO: implement me
it('should put a value to the DHT', (done) => {
this.timeout(80 * 1000)
const key = Buffer.from('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
const data = Buffer.from('data')

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

0 comments on commit 2952672

Please sign in to comment.