Tests wait for state instead of sleeping, on free ports

The relay, parity and benchmark server tests slept fixed times before
asserting, which a busy CI runner overran. They now wait for what they
need (the relay reports what it holds), take a port from the OS, and
use fresh HTTP connections. npm test -- --stress N hunts flakes.
This commit is contained in:
Frederic Jung 2026-11-05 11:38:26 +01:00
parent 02d3709944
commit 151b7786ed
No known key found for this signature in database
10 changed files with 152 additions and 59 deletions

View file

@ -8,6 +8,8 @@ const path = require('node:path');
const http = require('node:http');
const { spawn } = require('node:child_process');
const { until, freePort, kill } = require('./net');
const ROOT = path.join(__dirname, '..');
function req(port, { method = 'GET', p = '/', host, headers = {}, body } = {}) {
@ -20,11 +22,11 @@ function req(port, { method = 'GET', p = '/', host, headers = {}, body } = {}) {
}
test('the benchmark server answers only its own page', async (t) => {
const port = 20000 + Math.floor(Math.random() * 20000), out = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'bench-')), 'out.md');
const port = await freePort(), out = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'bench-')), 'out.md');
const srv = spawn(process.execPath, [path.join(ROOT, 'scripts', 'gpu-bench.js'), '--port', String(port), '--host', '127.0.0.1', '--out', out], { stdio: ['ignore', 'pipe', 'pipe'] });
let log = ''; srv.stdout.on('data', (c) => { log += c; }); srv.stderr.on('data', (c) => { log += c; });
t.after(() => srv.kill());
for (let i = 0; i < 100 && !/Ctrl\+C/.test(log); i++) await new Promise((r) => setTimeout(r, 100));
t.after(() => kill(srv));
await until(() => /Ctrl\+C|Error/.test(log) || srv.exitCode !== null);
assert.match(log, /Ctrl\+C/, 'the server started');
// DNS rebinding: a request for another host name gets nothing.