Browse Source

testing updates

session-estimator
Mathias Buus 3 years ago
parent
commit
4d7e0cf455
  1. 7
      .github/workflows/test-node.yml
  2. 1
      .gitignore
  3. 6
      package.json
  4. 159
      test.js

7
.github/workflows/test-node.yml

@ -1,5 +1,4 @@
name: Build Status name: Build Status
on: on:
push: push:
branches: branches:
@ -11,13 +10,13 @@ jobs:
build: build:
strategy: strategy:
matrix: matrix:
node-version: [14.x] node-version: [lts/*]
os: [ubuntu-16.04, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }} - name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1 uses: actions/setup-node@v2
with: with:
node-version: ${{ matrix.node-version }} node-version: ${{ matrix.node-version }}
- run: npm install - run: npm install

1
.gitignore

@ -1,3 +1,4 @@
node_modules node_modules
sandbox.js sandbox.js
sandbox sandbox
coverage

6
package.json

@ -14,11 +14,11 @@
"time-ordered-set": "^1.0.2" "time-ordered-set": "^1.0.2"
}, },
"devDependencies": { "devDependencies": {
"standard": "^16.0.3", "brittle": "^1.3.9",
"tape": "^5.2.2" "standard": "^16.0.3"
}, },
"scripts": { "scripts": {
"test": "standard && tape test.js" "test": "standard && brittle test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

159
test.js

@ -1,15 +1,14 @@
const tape = require('tape') const test = require('brittle')
const dgram = require('dgram') const dgram = require('dgram')
const DHT = require('./') const DHT = require('./')
tape('make tiny swarm', async function (t) { test('make tiny swarm', async function (t) {
const swarm = await makeSwarm(2) await makeSwarm(2, t)
t.pass('could make swarm') t.pass('could make swarm')
destroy(swarm)
}) })
tape('make bigger swarm', async function (t) { test('make bigger swarm', async function (t) {
const swarm = await makeSwarm(500) const swarm = await makeSwarm(500, t)
const targetNode = swarm[25] const targetNode = swarm[25]
const target = targetNode.id const target = targetNode.id
@ -59,15 +58,13 @@ tape('make bigger swarm', async function (t) {
const { firewalled, host, port } = swarm[490] const { firewalled, host, port } = swarm[490]
t.same(firewalled, false) t.is(firewalled, false)
t.same(port, swarm[490].address().port) t.is(port, swarm[490].address().port)
t.ok(host) t.ok(host)
destroy(swarm)
}) })
tape('commit after query', async function (t) { test('commit after query', async function (t) {
const swarm = await makeSwarm(100) const swarm = await makeSwarm(100, t)
let commits = 0 let commits = 0
@ -91,13 +88,11 @@ tape('commit after query', async function (t) {
await q.finished() await q.finished()
t.same(commits, swarm[42].table.k) t.is(commits, swarm[42].table.k)
destroy(swarm)
}) })
tape('map query stream', async function (t) { test('map query stream', async function (t) {
const swarm = await makeSwarm(10) const swarm = await makeSwarm(10, t)
const expected = [] const expected = []
const q = swarm[0].query({ command: 'find_node', target: swarm[0].table.id }, { const q = swarm[0].query({ command: 'find_node', target: swarm[0].table.id }, {
@ -114,13 +109,11 @@ tape('map query stream', async function (t) {
await q.finished() await q.finished()
t.ok(expected.length > 0) t.ok(expected.length > 0)
t.same(buf, expected) t.alike(buf, expected)
destroy(swarm)
}) })
tape('timeouts', async function (t) { test('timeouts', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3) const [, a, b] = await makeSwarm(3, t)
let tries = 0 let tries = 0
b.on('request', function (req) { b.on('request', function (req) {
@ -133,15 +126,11 @@ tape('timeouts', async function (t) {
const q = a.query({ command: 'nope', target: Buffer.alloc(32) }) const q = a.query({ command: 'nope', target: Buffer.alloc(32) })
await q.finished() await q.finished()
t.same(tries, 3) t.is(tries, 3)
bootstrap.destroy()
a.destroy()
b.destroy()
}) })
tape('request with/without retries', async function (t) { test('request with/without retries', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3) const [, a, b] = await makeSwarm(3, t)
let tries = 0 let tries = 0
b.on('request', function (req) { b.on('request', function (req) {
@ -157,7 +146,7 @@ tape('request with/without retries', async function (t) {
// do nothing // do nothing
} }
t.same(tries, 3) t.is(tries, 3)
try { try {
await a.request({ command: 'nope' }, { host: '127.0.0.1', port: b.address().port }, { retry: false }) await a.request({ command: 'nope' }, { host: '127.0.0.1', port: b.address().port }, { retry: false })
@ -165,15 +154,11 @@ tape('request with/without retries', async function (t) {
// do nothing // do nothing
} }
t.same(tries, 4) t.is(tries, 4)
bootstrap.destroy()
a.destroy()
b.destroy()
}) })
tape('reply onflush', async function (t) { test('reply onflush', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3) const [, a, b] = await makeSwarm(3, t)
let flushed = false let flushed = false
@ -187,14 +172,10 @@ tape('reply onflush', async function (t) {
await a.request({ command: 'hello' }, { host: '127.0.0.1', port: b.address().port }) await a.request({ command: 'hello' }, { host: '127.0.0.1', port: b.address().port })
t.ok(flushed) t.ok(flushed)
bootstrap.destroy()
a.destroy()
b.destroy()
}) })
tape('shorthand commit', async function (t) { test('shorthand commit', async function (t) {
const swarm = await makeSwarm(40) const swarm = await makeSwarm(40, t)
let tokens = 0 let tokens = 0
let notTokens = 0 let notTokens = 0
@ -211,13 +192,11 @@ tape('shorthand commit', async function (t) {
await q.finished() await q.finished()
t.same(tokens, 20) t.is(tokens, 20)
t.ok(notTokens >= tokens) t.ok(notTokens >= tokens)
destroy(swarm)
}) })
tape('after ready it is always bound', async function (t) { test('after ready it is always bound', async function (t) {
t.plan(2) t.plan(2)
const node = new DHT() const node = new DHT()
@ -231,11 +210,11 @@ tape('after ready it is always bound', async function (t) {
t.ok(typeof addr.port, 'is number') t.ok(typeof addr.port, 'is number')
node.destroy() await node.destroy()
}) })
tape('timeouts when commiting', async function (t) { test('timeouts when commiting', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3) const [, a, b] = await makeSwarm(3, t)
let tries = 0 let tries = 0
b.on('request', function (req) { b.on('request', function (req) {
@ -255,30 +234,22 @@ tape('timeouts when commiting', async function (t) {
} }
t.ok(error, 'commit should fail') t.ok(error, 'commit should fail')
t.same(tries, 3) t.is(tries, 3)
bootstrap.destroy()
a.destroy()
b.destroy()
}) })
tape('toArray', async function (t) { test('toArray', async function (t) {
const [bootstrap, a, b] = await makeSwarm(3) const [bootstrap, a, b] = await makeSwarm(3, t)
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() t.alike(a.toArray(), [{ host: '127.0.0.1', port: b.address().port }])
b.destroy() t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }])
bootstrap.destroy() t.alike(bootstrap.toArray().sort(), [{ host: '127.0.0.1', port: a.address().port }, { host: '127.0.0.1', port: b.address().port }].sort())
}) })
tape('addNode / nodes option', async function (t) { test('addNode / nodes option', async function (t) {
const [bootstrap, a] = await makeSwarm(2) const [bootstrap, a] = await makeSwarm(2, t)
a.on('request', function (req) { a.on('request', function (req) {
t.same(req.value, null, 'expected data') t.is(req.value, null, 'expected data')
req.reply(Buffer.from('world')) req.reply(Buffer.from('world'))
}) })
@ -290,75 +261,64 @@ tape('addNode / nodes option', async function (t) {
const bNodes = b.toArray() const bNodes = b.toArray()
t.same(bNodes, [{ host: '127.0.0.1', port: a.address().port }]) t.alike(bNodes, [{ host: '127.0.0.1', port: a.address().port }])
const responses = [] const responses = []
for await (const data of b.query({ command: 'hello', target: a.id })) { for await (const data of b.query({ command: 'hello', target: a.id })) {
responses.push(data) responses.push(data)
} }
t.same(responses.length, 1, 'one response') t.is(responses.length, 1, 'one response')
t.same(responses[0].value, Buffer.from('world'), 'responded') t.alike(responses[0].value, Buffer.from('world'), 'responded')
const aNodes = a.toArray() const aNodes = a.toArray()
t.same(aNodes, [{ host: '127.0.0.1', port: b.address().port }]) t.alike(aNodes, [{ host: '127.0.0.1', port: b.address().port }])
a.destroy() await b.destroy()
b.destroy()
bootstrap.destroy()
}) })
tape('set bind', async function (t) { test('set bind', async function (t) {
const port = await freePort() const port = await freePort()
const a = new DHT({ bind: port, firewalled: false }) const a = new DHT({ bind: port, firewalled: false })
await a.ready() await a.ready()
t.same(a.address().port, port, 'bound to explicit port') t.alike(a.address().port, port, 'bound to explicit port')
const b = new DHT({ bind: port }) const b = new DHT({ bind: port })
await b.ready() await b.ready()
t.notSame(b.address().port, port, 'bound to different port as explicit one is taken') t.not(b.address().port, port, 'bound to different port as explicit one is taken')
a.destroy() await a.destroy()
b.destroy() await b.destroy()
}) })
tape('relay', async function (t) { test('relay', async function (t) {
const [bootstrap, a, b, c] = await makeSwarm(4) const [, a, b, c] = await makeSwarm(4, t)
b.on('request', function (req) { b.on('request', function (req) {
t.same(req.command, 'route', 'b got request') t.is(req.command, 'route', 'b got request')
t.same(req.from.port, a.address().port, 'from a') t.is(req.from.port, a.address().port, 'from a')
const value = Buffer.concat([req.value, Buffer.from('b')]) const value = Buffer.concat([req.value, Buffer.from('b')])
req.relay(value, { host: '127.0.0.1', port: c.address().port }) req.relay(value, { host: '127.0.0.1', port: c.address().port })
}) })
c.on('request', function (req) { c.on('request', function (req) {
t.same(req.command, 'route', 'c got request') t.is(req.command, 'route', 'c got request')
t.same(req.from.port, b.address().port, 'from b') t.is(req.from.port, b.address().port, 'from b')
const value = Buffer.concat([req.value, Buffer.from('c')]) const value = Buffer.concat([req.value, Buffer.from('c')])
req.reply(value, { to: { host: '127.0.0.1', port: a.address().port } }) req.reply(value, { to: { host: '127.0.0.1', port: a.address().port } })
}) })
const res = await a.request({ command: 'route', value: Buffer.from('a') }, { host: '127.0.0.1', port: b.address().port }) const res = await a.request({ command: 'route', value: Buffer.from('a') }, { host: '127.0.0.1', port: b.address().port })
t.same(res.value, Buffer.from('abc')) t.alike(res.value, Buffer.from('abc'))
t.same(res.from.port, c.address().port) t.is(res.from.port, c.address().port)
t.same(res.to.port, a.address().port) t.is(res.to.port, a.address().port)
bootstrap.destroy()
a.destroy()
b.destroy()
c.destroy()
}) })
function destroy (list) {
for (const node of list) node.destroy()
}
function freePort () { function freePort () {
return new Promise(resolve => { return new Promise(resolve => {
const socket = dgram.createSocket('udp4') const socket = dgram.createSocket('udp4')
@ -371,7 +331,7 @@ function freePort () {
}) })
} }
async function makeSwarm (n) { async function makeSwarm (n, t) {
const node = DHT.bootstrapper() const node = DHT.bootstrapper()
await node.ready() await node.ready()
const all = [node] const all = [node]
@ -381,5 +341,8 @@ async function makeSwarm (n) {
await node.ready() await node.ready()
all.push(node) all.push(node)
} }
t.teardown(async function () {
for (const node of all) await node.destroy()
})
return all return all
} }

Loading…
Cancel
Save