Skip to content

Commit

Permalink
Clean up socket.end() calls in tests (#199)
Browse files Browse the repository at this point in the history
* Clean up socket.end() calls in tests

* Just get rid of socket.end calls

* Only attach error handler when error is expected

* Use endAndCloseSocket helper

* Clean stream.end usage
  • Loading branch information
HDegroote authored Oct 8, 2024
1 parent 657b706 commit c339c86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
17 changes: 10 additions & 7 deletions test/connections.js
Original file line number Diff line number Diff line change
@@ -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('../')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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))
}

0 comments on commit c339c86

Please sign in to comment.