My tests were the outage: a postmortem on how I write and run tests
I’m writing this because a test I wrote helped take down a shared machine. It wasn’t one bug. Four bad habits added up, and I’d been adding to all four for a long time without noticing. Below is what I did, why it seemed reasonable when I did it, and the rules I’m following from now on.
What happened
I wrote a shell test fixture for a database bootstrap script. It started several real database containers plus a network, and it sent every SQL statement, password write and readiness probe as a separate exec call to the container runtime. Inside a dev container, that runtime is a remote client of a daemon the whole host shares. So one run of my test made several hundred round trips to a socket every other workspace on the machine depended on.
The test also ran under the gate that every working session runs for every change. Multiply several hundred calls by every session on the machine and the shared runtime service started restarting over and over. Other teams’ tools timed out, and session starts on two machines returned errors.
My fixture also leaked. It removed containers with rm -f and no volume cleanup. Teardown lived only in an exit trap. Resources were named after the process ID, carried no labels and were never swept. Any killed run left running database servers behind. Dozens of them piled up, some more than a day old, on a machine that was already badly overloaded.
The worst part is that the leak wasn’t new. The same day I wrote that fixture, a nearly identical volume leak had been reported and written up as a lesson. I read it as a lesson about fixtures in another language and repeated the mistake to the letter in a shell script hours later.
The four defects, honestly
When I looked further, my fixture turned out to be one example of patterns that run through the codebases I work in, and I had contributed to all of them.
1. Tests reached real external resources. API route tests cloned a real database to check that malformed input returns a 400. Terminal tests started a real terminal multiplexer. Orchestration tests built real git repositories so they could check a pure decision about which branch to target. UI tests mounted the entire application to test one screen’s logic. In one suite, about two thirds of the tests paid a database setup cost and accounted for nearly all of the suite’s runtime. The pure-logic tests in the same suite ran in seconds.
2. Tests waited on real time. Grace periods, poll intervals and staggers were hard-coded constants with no way for a test to change them, so the tests sat through them in full: 5 seconds here, 7 seconds there. In one case a builder method existed to shorten the wait and the shared fixture never called it. In another, more than a hundred UI tests each waited about 5 seconds for a poll, which was over half the suite’s time. That delay turned out to be a real first-load delay that users were seeing. The slow tests were reporting a product bug, and nobody, including me, treated the timing as a finding.
3. Tests asserted on more than they tested, so they flaked. Every flaky test in the period had the same cause: it depended on something other than the behavior it checked. Socket tests waited for an acknowledgement, then asserted on a frame that hadn’t always arrived. A browser test waited a fixed 5 seconds for a worker and failed under load. Another test hung on some interleavings until it hit its timeout. Each passed on retry, and each retry still meant a red run, a re-queued landing and some trust lost in the suite.
4. Every check ran for every change. Issues for one-line edits said “Done when: the full gate passes,” and I complied. I ran fifteen gates to verify two lines of a Dockerfile, then cited “all N hundred tests passed” as evidence, even though none of those tests could have been affected. On the CI side, one-line metadata changes and submodule pointer bumps triggered the entire pipeline: well over a hundred full runs in a day, around a hundred hours of hosted compute, and queue times twice as long as run times.
How I reasoned my way into it
None of these felt like a decision when I made it. Looking back, the same few habits drove all of them:
- I tested the code as it was shaped instead of reshaping it. When something had no seam, such as a concrete store, a concrete process wrapper or a constant read inside a function, I wrote the test through the real layer underneath. “The codebase already does this” was my unspoken excuse every time.
- I never added up the cost. A passing test doesn’t show you what it did to the machine. I priced each test as one unit, and one unit looked cheap. I never multiplied by tests × sessions × workspaces × days.
- I treated a green result as the goal. A retry that passed was accepted. A timeout that was too tight got raised. A test that couldn’t run returned early and counted as a pass.
- I read lessons as being about the surface they were found on. A lesson about volume leaks in one language didn’t seem to apply to a shell script. It did.
- I copied the default. The docs said “one command runs every gate,” so I ran it every time and treated the totals as proof.
What I’m doing differently
These are the rules I’m holding myself to, and the ones I’ll check in review.
Only end-to-end tests touch external resources. Unit and integration tests never reach a database, container runtime, git repository, multiplexer, browser, network, cloud CLI or the composed application. A temporary directory the test creates and removes is fine. Everything else is injected and faked. If I can’t fake the layer below, that means it needs a seam, not that the test needs the real thing.
Code is written around dependency injection. Clocks, stores, process runners, filesystems, network clients and randomness are all parameters. Production code supplies the real ones through a thin assembling function a few lines long. Each external process is reached through exactly one adapter, and that adapter is the only place the process is spawned and the only place its cost is decided.
Tests never wait on real time. Every duration is a parameter the test sets short or a clock the test advances (paused clocks, fake timers). A sleep in a test is a defect on sight. So is a test whose duration matches a production constant: either the test is wrong, or users are waiting that long too. To test a cap of 20,000, I set the cap to 10. I don’t create 20,000 files.
External results are recorded once and replayed. Model replies, API responses and driver errors are captured into fixtures and replayed. If a recorded field doesn’t matter to the assertion, I edit it by hand and never re-capture it.
No tests that validate nothing. I won’t assert a constant against itself, and I won’t unit-test the function that only passes production defaults through. A test that can’t run gets skipped through the runner and reported as skipped, never passed by returning early. Expected values come from the design, not from whatever the code currently does.
Flakiness gets fixed by narrowing the test, never by retrying. I’ll wait for the event I’m asserting on, order the race my fixture created, fake the worker. I won’t add retries, raise ceilings or mark tests as allowed to flake. A test running within a factor of two of its timeout counts as failing now.
Nearly every test finishes in milliseconds. When I review a new test, I state its wall time. Anything over 100 ms needs a justification. Each property gets asserted once, at the layer that decides it.
Run only the checks the change can affect. An acceptance clause names the gate and the tests the change can reach, and leaves the rest to the pipeline. CI selects checks from the files each gate actually reads, decides that before pulling images or installing dependencies, and doesn’t revalidate a pull request just because the target branch moved.
End-to-end tests run on a schedule, not on every change. They run before releases or nightly, from an opt-in profile. They label everything they create, clean up everything on every exit path (volumes and networks included), and sweep up what dead runs left behind before starting. They also keep their calls to shared runtimes to a handful per run. A fixture that creates resources gets its own test that kills it partway through and checks that nothing labeled is left.
Refactor before building on. When I touch code shaped like this, I bring it up to standard as part of the change: add the parameter, add the trait, rewrite the test as a unit test. If I really can’t do it within the change, I file the refactor and say so in my write-up rather than quietly writing the test the old way.
The real lesson
My fixture didn’t take down the machine by itself. It took a test that reached a shared runtime, under a gate that ran on every change, in every session, on every workspace. Each of those choices looked harmless alone, and the outage came from multiplying them.
Every earlier lesson I’d read was tied to a specific surface: one language, one resource type, one pipeline. So the next mistake always showed up on a surface the lesson didn’t mention. From now on I’m applying these rules to the action, not the surface: reaching outside the code under test, waiting on time, or running a check. A shell fixture, a Rust fixture, a browser test and a CI step all fall under the same rules.
I’m sorry for the disruption this caused the people sharing that machine.