diff --git a/test/connections.js b/test/connections.js index 0a151863..b43173c1 100644 --- a/test/connections.js +++ b/test/connections.js @@ -1,5 +1,5 @@ const test = require('brittle') -const { swarm, createDHT } = require('./helpers') +const { swarm, createDHT, endAndCloseSocket } = require('./helpers') const { encode } = require('hypercore-id-encoding') const { once } = require('events') const DHT = require('../') @@ -640,19 +640,21 @@ test('connect using id instead of buffer', async function (t) { const [a, b] = await swarm(t) const server = a.createServer() + server.on('connection', conn => { + conn.on('end', () => conn.end()) + }) await server.listen() const id = encode(server.publicKey) const socket = b.connect(id) - socket.on('error', () => {}) await once(socket, 'open') t.is(id.length, 52) t.pass('connects if id is given instead of buffer') - await socket.end() + await endAndCloseSocket(socket) await server.close() await a.destroy() @@ -745,20 +747,21 @@ test('connectionKeepAlive passed to server and connection', async function (t) { const a = createDHT({ bootstrap, connectionKeepAlive: 10000 }) const b = createDHT({ bootstrap, connectionKeepAlive: 20000 }) - const server = a.createServer(async function (socket) { - socket.on('error', () => {}) + const server = a.createServer((socket) => { + socket.on('end', () => socket.end()) allChecks.is(socket.keepAlive, 10000, 'keepAlive set for server') }) await server.listen() const socket = b.connect(server.publicKey) - socket.on('error', () => {}) allChecks.is(socket.keepAlive, 20000, 'keepAlive set for connection') await allChecks - await socket.end() + + await endAndCloseSocket(socket) + await server.close() await a.destroy() diff --git a/test/helpers/index.js b/test/helpers/index.js index 91bb5d61..75b3b700 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -4,7 +4,7 @@ const { spawn } = require('child_process') const goodbye = require('graceful-goodbye') const DHT = require('../../') -module.exports = { swarm, toArray, spawnFixture, createDHT } +module.exports = { swarm, toArray, spawnFixture, createDHT, endAndCloseSocket } async function toArray (iterable) { const result = [] @@ -34,3 +34,12 @@ async function * spawnFixture (t, args) { function createDHT (opts) { return new DHT({ ...opts, host: '127.0.0.1' }) } + +async function endAndCloseSocket (socket) { + // We wait on the other side to end the stream too + // So make sure a handler was added like + // socket.on('end', () => socket.end()) + socket.end() + if (socket.destroyed) return + await new Promise(resolve => socket.on('close', resolve)) +}