test: wait for the async log line instead of racing it

Under the fan-out a test that greps server.log right after an ack can lose:
the logger writes the line after the ack. waitForLog polls to a deadline and
the assertions are unchanged (the line must still appear; absence checks still
run on the same snapshot). Fixes the one observed assertion failure
(fault.test.mjs's intent_failed line) and the same latent race in the
opaque_refused and startup-line checks.
This commit is contained in:
2026-11-21 21:42:34 +01:00
parent 514104f897
commit 100df38230
7 changed files with 44 additions and 19 deletions
+16
View File
@@ -98,6 +98,22 @@ export async function startServer(opts = {}) {
return handle
}
// Wait for a line the server writes asynchronously (the log is not flushed before the ack that
// triggered it). A test that greps the log right after an ack is otherwise racing the logger: under
// the parallel fan-out that race is lost. Returns the log when the needle matches, or the last read
// at the deadline so the test's own assertion reports the content it saw.
export async function waitForLog(handle, needle, timeoutMs = 15000) {
const matches = log => typeof needle === 'function' ? needle(log)
: needle instanceof RegExp ? needle.test(log) : log.includes(needle)
const deadline = Date.now() + timeoutMs
let log = handle.log()
while (!matches(log) && Date.now() < deadline) {
await sleep(25)
log = handle.log()
}
return log
}
export async function stopServer(handle, logFd = handle.logFd) {
// A process killed by a signal keeps exitCode null and sets signalCode, so test both.
if (!hasExited(handle.child)) {