Skip to content

Commit

Permalink
Test for server.listen() hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
freeall committed Apr 9, 2024
1 parent a9cef54 commit 8f1bafd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/integration/fixtures/start-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const DHT = require('../../../')

/*
This test is put into a fixture to make sure that each run is its own.
It will exit with 0 if able to start server, and 1 if there was an error when starting.
It will hang if, for some reason, `await server.listen()` never returns [TBC: this is the bug I am hunting]
*/

async function run () {
const node = new DHT()
const server = node.createServer(() => { })
await server.listen()
}

run()
.then(() => {
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})
30 changes: 30 additions & 0 deletions test/integration/server-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { spawn } = require('child_process')
const test = require('brittle')

const COUNT = 10000

test.skip(`Start a server ${COUNT} times`, { timeout: 0 }, async t => {
t.plan(COUNT)

for (let i = 0; i < COUNT; i++) {
const startTime = Date.now()
await t.test(async serverTest => {
serverTest.plan(1)

await new Promise((resolve, reject) => {
const process = spawn('node', ['fixtures/start-server.js', i])
process.stdout.on('data', data => serverTest.fail(data.toString()))
process.stderr.on('data', data => serverTest.fail(data.toString()))
process.on('exit', (code) => {
if (code === 0) {
serverTest.pass(`Took ${Date.now() - startTime} ms`)
resolve()
} else {
serverTest.fail(`Test ${i} failed with code ${code}`)
reject()
}
})
})
})
}
})

0 comments on commit 8f1bafd

Please sign in to comment.