Skip to content

Commit

Permalink
Fix tests running on explicit localhost testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Oct 5, 2024
1 parent ae27973 commit 924c1cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"compact-encoding": "^2.4.1",
"compact-encoding-net": "^1.0.1",
"debugging-stream": "^2.0.0",
"dht-rpc": "^6.14.0",
"dht-rpc": "^6.15.1",
"hypercore-crypto": "^3.3.0",
"hypercore-id-encoding": "^1.2.0",
"noise-curve-ed": "^2.0.0",
Expand Down
47 changes: 17 additions & 30 deletions test/connections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const test = require('brittle')
const { swarm } = require('./helpers')
const { swarm, createDHT } = require('./helpers')
const { encode } = require('hypercore-id-encoding')
const { once } = require('events')
const DHT = require('../')
Expand Down Expand Up @@ -142,8 +142,8 @@ test('createServer + connect - force holepunch', async function (t) {
const [boot] = await swarm(t)

const bootstrap = [{ host: '127.0.0.1', port: boot.address().port }]
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const a = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })

await a.fullyBootstrapped()
await b.fullyBootstrapped()
Expand Down Expand Up @@ -185,8 +185,8 @@ test('server choosing to abort holepunch', async function (t) {
const [boot] = await swarm(t)

const bootstrap = [{ host: '127.0.0.1', port: boot.address().port }]
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const a = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })

await a.fullyBootstrapped()
await b.fullyBootstrapped()
Expand Down Expand Up @@ -230,8 +230,8 @@ test('client choosing to abort holepunch', async function (t) {
const [boot] = await swarm(t)

const bootstrap = [{ host: '127.0.0.1', port: boot.address().port }]
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const a = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })

await a.fullyBootstrapped()
await b.fullyBootstrapped()
Expand Down Expand Up @@ -498,8 +498,8 @@ test('create many connections with reusable sockets', async function (t) {
const [boot] = await swarm(t)

const bootstrap = [{ host: '127.0.0.1', port: boot.address().port }]
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const a = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = createDHT({ bootstrap, quickFirewall: false, ephemeral: true })

await a.fullyBootstrapped()
await b.fullyBootstrapped()
Expand Down Expand Up @@ -713,8 +713,8 @@ test('connectionKeepAlive can be turned off', async function (t) {

const { bootstrap } = await swarm(t)

const a = new DHT({ bootstrap, connectionKeepAlive: false })
const b = new DHT({ bootstrap, connectionKeepAlive: false })
const a = createDHT({ bootstrap, connectionKeepAlive: false })
const b = createDHT({ bootstrap, connectionKeepAlive: false })
t.teardown(async () => {
await a.destroy()
await b.destroy()
Expand Down Expand Up @@ -742,8 +742,8 @@ test('connectionKeepAlive passed to server and connection', async function (t) {

const { bootstrap } = await swarm(t)

const a = new DHT({ bootstrap, connectionKeepAlive: 10000 })
const b = new DHT({ bootstrap, connectionKeepAlive: 20000 })
const a = createDHT({ bootstrap, connectionKeepAlive: 10000 })
const b = createDHT({ bootstrap, connectionKeepAlive: 20000 })

const server = a.createServer(async function (socket) {
socket.on('error', () => {})
Expand All @@ -768,33 +768,20 @@ test('connectionKeepAlive passed to server and connection', async function (t) {
test('bootstrap with suggested-IP', async function (t) {
const [boot] = await swarm(t, 1)
const bootstrap = ['127.0.0.1@invalid:' + boot.address().port]
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: false })
const a = createDHT({ bootstrap, quickFirewall: false, ephemeral: false })
await a.fullyBootstrapped()

t.alike(boot.toArray(), [{ host: '127.0.0.1', port: a.address().port }])

await a.destroy()
})

test('fail to bootstrap completely', async function (t) {
const [boot] = await swarm(t, 1)
const bootstrap = ['invalid:49737']
const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: false })
await a.fullyBootstrapped()

const empty = []
t.alike(a.toArray(), empty)
t.alike(boot.toArray(), empty)

await a.destroy()
})

test('Populate DHT with options.knownNodes', async function (t) {
const a = new DHT({ bootstrap: [] })
test('Populate DHT with options.nodes', async function (t) {
const a = createDHT({ bootstrap: [] })
await a.fullyBootstrapped()
const nodes = [{ host: '127.0.0.1', port: a.address().port }]

const b = new DHT({ nodes, bootstrap: [] })
const b = createDHT({ nodes, bootstrap: [] })
await b.fullyBootstrapped()

t.alike(b.toArray(), [{ host: '127.0.0.1', port: a.address().port }])
Expand Down
7 changes: 6 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const createTestnet = require('../../testnet')
const NewlineDecoder = require('newline-decoder')
const { spawn } = require('child_process')
const goodbye = require('graceful-goodbye')
const DHT = require('../../')

module.exports = { swarm, toArray, spawnFixture }
module.exports = { swarm, toArray, spawnFixture, createDHT }

async function toArray (iterable) {
const result = []
Expand All @@ -29,3 +30,7 @@ async function * spawnFixture (t, args) {

unregisterExitHandlers()
}

function createDHT (opts) {
return new DHT({ ...opts, host: '127.0.0.1' })
}

0 comments on commit 924c1cf

Please sign in to comment.