Browse Source

port of last of the tests

session-estimator
Mathias Buus 4 years ago
parent
commit
bd1c516a7a
  1. 4
      index.js
  2. 100
      test.js

4
index.js

@ -165,6 +165,8 @@ class DHT extends EventEmitter {
}
async bootstrap () {
await Promise.resolve() // wait a tick, so apis can be used from the outside
for (let i = 0; i < 2; i++) {
await this._backgroundQuery(this.table.id, 'find_node', null).finished()
if (this.bootstrapped || !this._forcePersistent || !(await this._onpersistent())) break
@ -564,7 +566,7 @@ class DHT extends EventEmitter {
token: null,
added: this._tick,
seen: this._tick,
network: host,
network: null,
prev: null,
next: null
})

100
test.js

@ -18,7 +18,7 @@ tape('make bigger swarm', async function (t) {
for await (const data of q) {
messages++
if (data.id.equals(targetNode.id)) {
if (data.id && data.id.equals(targetNode.id)) {
found = true
break
}
@ -32,7 +32,7 @@ tape('make bigger swarm', async function (t) {
for await (const data of q) {
messages++
if (data.id.equals(targetNode.id)) {
if (data.id && data.id.equals(targetNode.id)) {
found = true
break
}
@ -100,6 +100,102 @@ tape('map query stream', async function (t) {
destroy(swarm)
})
tape('timeouts', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3)
let tries = 0
b.on('request', function (req) {
if (req.command === 'nope') {
tries++
t.pass('ignoring request')
}
})
const q = a.query(Buffer.alloc(32), 'nope')
await q.finished()
t.same(tries, 4)
bootstrap.destroy()
a.destroy()
b.destroy()
})
tape('timeouts when commiting', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3)
let tries = 0
b.on('request', function (req) {
if (req.command === 'nope') {
tries++
t.pass('ignoring request')
}
})
const q = a.query(Buffer.alloc(32), 'nope', null, { commit: true })
let error = null
try {
await q.finished()
} catch (err) {
error = err
}
t.ok(error, 'commit should fail')
t.same(tries, 4)
bootstrap.destroy()
a.destroy()
b.destroy()
})
tape('toArray', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3)
t.same(a.toArray(), [{ host: '127.0.0.1', port: b.address().port }])
t.same(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }])
t.same(bootstrap.toArray().sort(), [{ host: '127.0.0.1', port: a.address().port }, { host: '127.0.0.1', port: b.address().port }].sort())
a.destroy()
b.destroy()
bootstrap.destroy()
})
tape('addNode / nodes option', async function (t) {
const [bootstrap, a] = await makeSwarm(2)
a.on('request', function (req) {
t.same(req.value, null, 'expected data')
req.reply(Buffer.from('world'))
})
await bootstrap.ready()
await a.ready()
const b = new DHT({ ephemeral: false, nodes: [{ host: '127.0.0.1', port: a.address().port }] })
await b.ready()
const bNodes = b.toArray()
t.deepEqual(bNodes, [{ host: '127.0.0.1', port: a.address().port }])
const responses = []
for await (const data of b.query(a.id, 'hello')) {
responses.push(data)
}
t.same(responses.length, 1, 'one response')
t.same(responses[0].value, Buffer.from('world'), 'responded')
const aNodes = a.toArray()
t.deepEqual(aNodes, [{ host: '127.0.0.1', port: b.address().port }])
a.destroy()
b.destroy()
bootstrap.destroy()
})
function destroy (list) {
for (const node of list) node.destroy()
}

Loading…
Cancel
Save