Making test gates honest under machine load
Why a test gate can pass or fail for the wrong reason when the machine is busy, and the rules I use to make a green result mean something.
A gate is a command that decides whether work may go on. It runs the tests and answers yes or no. A gate that passes on an idle laptop and fails on a busy server is not honest. A gate that passes because nothing ran is worse. This post lists the ways a gate lies under load and the rules I use against each.
Waiting for time
Many flaky tests wait for a fixed time and then check a result. The sleep is a guess about how long the code needs. On an idle machine the guess is right. On a loaded machine the code runs slower, the guess is wrong, and the test fails.
Waiting for the event fixes the cause. The test subscribes to the event before it triggers the action, and then it waits for the event with a time limit. The limit is a ceiling. It does not add delay, because the wait ends the moment the event arrives.
export function waitForEvent<T>(
target: EventTarget,
name: string,
timeoutMs: number,
): Promise<T> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
target.removeEventListener(name, onEvent);
reject(new Error(`timed out after ${timeoutMs} ms waiting for ${name}`));
}, timeoutMs);
function onEvent(e: Event) {
clearTimeout(timer);
resolve((e as CustomEvent<T>).detail);
}
target.addEventListener(name, onEvent, { once: true });
});
}
A test uses it in this order:
const done = waitForEvent<number>(job, "finished", 5000);
job.start();
expect(await done).toBe(3);
The order matters. If job.start() ran first, a fast job could finish before the test listens, and the test would wait for an event that already happened.
A timeout is a failure
Under load, a test that hits its time limit tells me something. I do not skip it, and I do not rerun it until it passes. The gate counts a timeout as a failure and prints the name of the test.
A retry hides the information. If a test fails once and passes on the second run, the code or the test has a race. That is a bug with a name, and I fix it. A retry setting in the runner only delays the day when the race hurts a real change.
Green must mean that tests ran
Some runners exit with success when zero tests ran. Bun exits with code 1 in that case. A wrong filter or a skipped suite can still leave nothing checked. The gate must check that tests actually ran.
#!/usr/bin/env bash
set -uo pipefail
out=$(timeout 600 bun test 2>&1)
code=$?
echo "$out"
echo "load: $(cut -d' ' -f1-3 /proc/loadavg) commit: $(git rev-parse --short HEAD)"
if [ "$code" -ne 0 ]; then echo "GATE_FAIL: exit code $code"; exit 1; fi
echo "$out" | grep -Eq '^ *[1-9][0-9]* pass' || { echo "GATE_FAIL: no tests passed"; exit 1; }
echo "GATE_OK"
The script has four parts. It wraps the run in timeout, so a hang becomes exit code 124 and a failure. It prints the load average and the commit, so a red result can be read in context. It fails when the exit code is not zero. It fails when the output shows no passing test.
The same idea applies to the list of checks in a merge gate. An empty list of checks is not green. I wrote about that in the merge gate post.
Tie the result to a commit
A green result belongs to the exact commit that ran. If someone pushes after the gate, the old result says nothing about the new code. The gate prints the commit hash, and whoever merges compares it with the head of the branch.
Do not run two heavy gates at once
Two test suites on one host slow each other down. Each looks like a timing failure to the other. On my server I run one build or one suite at a time, and a lock can enforce that rule.
flock "$HOME/.cache/heavy.lock" ./gate.sh
The second caller waits for the lock. The wait is visible in the log, and the run itself stays fast.
Mocks keep the behavior under test
Mocks can also make a gate dishonest. A test that replaces the whole system under test with a mock can never fail. When I mock a dependency, I keep the real behavior that the test claims to check. A database mock that returns what the test expects proves nothing about the query.
What the gate prints
At the end of a run the gate prints four facts. They are the commit, the load average, the number of passing tests and a line that says GATE_OK or GATE_FAIL with a reason. With those facts, a reader can tell whether a green result means anything. I never delete a failing test to turn the gate green.