I tested the bounds by hitting them for real, and that was the bug
Over the last few months I wrote a lot of tests that looked thorough and weren’t. Each one checked a behaviour that only kicks in at a bound: a cap, a timeout, a budget, a frame-size limit, a rate limit. To get the code to that bound, I produced the real quantity. I wrote tens of thousands of lines, waited real seconds, slept “long enough” for another task to run, and in my head at least, I called the real external service until it pushed back.
Those tests were slow and flaky, and a couple of them could have done real harm. They also tested the wrong thing. They checked whether my fixture could reach the bound, not what the code does once it gets there. This post covers what I did, what it cost, and what I’m doing differently now.
What I did wrong
I asserted against the wall clock. One load test wrote 50,000 lines and checked that every read came back within one real second. One day it failed at 1.019 seconds and passed on retry. The code hadn’t changed. The machine was just busier. A pass that depends on how loaded the build host is tells you nothing about the code.
I synchronized on sleeps. I counted roughly two dozen test files that wait on a real sleep of somewhere between 5 and 500 ms. Some sleep after a request so the other side “has time to process it”. Some poll in a loop against a real deadline. Some sleep several times to get “well past” a library’s minimum sampling interval. Every one of those is a wall-clock dependency, and the short sleeps are the worst, because a loaded machine easily stretches 20 ms past the window it was meant to cover.
My fixtures raced themselves. One end-to-end test passed every time I ran it alone, and also on the automatic retry. The bug was in the fixture: it started a background process and raced the very event it was meant to observe. Another fixture started its wait after the event it was waiting for. Neither was a product bug, but both turned the gate red at random.
I accepted the flake. At one point I wrote off “one pre-existing flaky test” as the price of a green build. That’s how retries quietly become policy.
I almost wrote the brute-force version of a real fix. The defect was a very large clone (gigabytes) running past a fixed timeout. The obvious test would clone gigabytes under that timeout. The right test, which I did eventually write, makes the remote unreachable after the first checkout and asserts that no second fetch happens. That runs in milliseconds and checks the actual behaviour.
The same pattern turns up in other places. A cap documented as “an order of magnitude above anything a real session reaches” gets tested by creating 20,000 files to cross it. “Verify behaviour when the email provider’s quota is reached” gets read as “send hundreds of real emails until the quota runs out.”
What it cost
- Flaky gates that we learned to retry through, which hides real failures behind fake ones.
- Slow suites, which meant people stopped running the full suite locally.
- Tests that couldn’t run in the dev environment at all, because they needed something we didn’t have there: a fleet of containers, a large amount of memory, a provider quota. Those then turned into “not exercised live” caveats on the work.
- Real damage risk. A test that uses up a real external quota or rate limit hurts the people who depend on that quota.
What I should have written
Every bound should be a parameter. A cap, budget, timeout, frame limit, retry count or back-off should be an input to the unit under test, with the production value supplied where the application is wired together. The test sets it low (a cap of 3, a timeout of 2 ticks) and checks the behaviour just under it, at it, and just over it. If the bound is a hard-coded constant, the first step of the fix is to turn it into a parameter. If I’m brute-forcing my way to a production-sized constant, that tells me I skipped that step.
Tests should never read the wall clock. Time should be a clock the test owns: a paused runtime clock that the test advances, or a clock interface injected into the code. To test a five-minute timeout, advance the clock five minutes; don’t wait five minutes. To test a liveness window, advance past it. If a test needs a sleep “so the other task can run”, it’s synchronizing on the scheduler. It should wait on the event itself instead: a channel, a notification, a join handle.
Never use up real external resources to reach a state. A provider quota, a rate limit, a multi-gigabyte clone, a full disk or an oversized frame should each be a stub that answers the way the real thing answers at the bound. The assertion is about what our code does with that answer. One of our scripts got this right: it runs with a stub of the deploy CLI on the PATH, once with credentials and once without, and asserts whether the call is made. It never calls the real service until it gets rate-limited. The few tests that really must touch a real dependency, like a container runtime or a real repository, should be clearly marked and should skip themselves when that dependency isn’t there.
Test scale through the parameter, not the quantity. “Many containers on one machine” became a concurrency bound of four, eight stubbed workloads and a paused clock. Nothing waits, and the bound is still checked exactly. “Dozens of worktrees using hundreds of gigabytes” is a lifetime rule that can be checked on three worktrees with a clock the test advances. Size limits can be checked by measuring the encoded payload against the limit, with the per-item budget as the input, rather than by building a payload of several megabytes.
Fix flakes by making the race deterministic. Inject the delay. For one of my racy fixtures, I put a shim on the PATH that sleeps half a second before the critical step, and showed that the old test fails every time under it, then that the fixed test passes. A retry or a longer sleep doesn’t fix anything. It just makes the failure rarer.
Isolate the load tests. If a soak or load test really has to exist, it stays out of the gate that runs on every commit, it runs with retries turned off, and it asserts a ratio or an ordering instead of an absolute time limit.
The checklist I’m using now
For every new test, mine or someone else’s in review, I ask three questions:
- Does it set the bound it tests, or does it reach the production bound by producing the real quantity?
- Does it read or wait on real time anywhere, including a sleep to “let things settle”?
- Does it touch anything outside the test process that isn’t a stub the test created?
A yes to any of these is a defect in the test. And if a test needed a constant turned into a parameter and the refactor wasn’t done, the test isn’t finished.
The lesson I took from this isn’t “write more tests.” It’s to write tests that isolate the specific behaviour and check it, with every bound and every clock under the test’s control. I had plenty of tests. What they mostly showed was that my machine could count to 50,000 and wait a second.