Why the code-analysis truncation test wrote 20,050 files
I wrote a test that created 20,050 files on disk and then walked the tree twice. It lived for about seven weeks, and for the last two of those it carried a dedicated 120-second timeout override with a well-measured comment explaining why the cost was “inherent”. The cost was not inherent. It was a consequence of an API I had shaped so that no caller could ever set the limit the test was trying to cross.
This is the post-mortem, reconstructed from the commit history rather than from memory.
What I wrote
The static analyzer crate has a set of tree-wide caps: a maximum file count, a maximum number of bytes to parse, a maximum symbol count. When a cap fires, the result is marked truncated and names which cap did it. The property worth testing is that truncation is deterministic: files are visited in sorted order, so which files get dropped is a function of the tree’s contents, not of whatever order the filesystem hands back directory entries.
So I wrote a test called a_truncated_analysis_is_deterministic_and_says_so. To make the file cap fire, it looped to MAX_FILES + 50 — 20,050 — and wrote a small markdown file for each one. Then it ran the analysis twice and asserted the two results were equal, that the result said it was truncated, that it named the file-count cap, and that the last surviving file was the one sorted order would predict.
It worked. It also took 18.6 seconds alone on an idle 18-core container, 50.9 seconds when scheduled beside the sandbox tests, and eventually crossed the 60-second default once those sandbox tests each started spawning a type checker. At that point it was killed mid-assertion, which in CI reads as a broken analysis rather than as a slow test.
Why I wrote it that way
Four pressures, none of them individually wrong.
The caps had no seam. The walk function read the file-count constant directly from inside its own body. The analysis function initialised its parse and symbol budgets from bare constants. There was no parameter anywhere that let a caller run under a different bound. Given that API, the only way to make the cap fire was to exceed it for real. I tested the code in the shape I had built it, instead of reshaping the code to be testable.
Worse, this was deliberate. The design doc says a cap change is a definition change, because it changes which files contribute to the result, and the cap values are part of what the analyzer version stamps. I read “these values are load-bearing” as “these values must be constants”. Those are not the same claim, and the eventual fix proves it: a budget struct whose Default equals the production constants, a production entry point that only ever passes the default, and a one-line test pinning the default’s values. The definition stays exactly as load-bearing as before.
I applied a product constraint to the test. The design doc has a section arguing that the analyzer must have no wall-clock budget and that every bound must be content-derived — otherwise an “analyse it twice, assert equal” test would pass and prove nothing. That is correct, and it is a statement about the product. I carried it into the test and concluded, as I actually wrote down later, that “the only honest way to assert what a cap drops is to exceed the real cap.”
That sentence is the central error. Exceeding a real cap tests the constant’s value. Exceeding a configured cap tests the truncation mechanism. My own doc comment said the subject was the mechanism. The number 20,000 is a separate, cheap fact that deserved its own cheap test.
I was aiming at the half that could plausibly fail. I had correctly identified the real risk — does truncation depend on directory iteration order? — and wanted the most convincing possible evidence. A production-scale tree felt like stronger proof. It was actually weaker. I wrote the files in ascending numeric order, so sorted order and creation order agreed, which means the assertion about the last surviving file would have passed even if the walk had never sorted at all. The fixed version writes 15 files in reverse specifically so those two orders disagree. Fifteen files in reverse test the ordering claim; 20,050 files in order test the filesystem.
The constant was justified as “pathological”, and I didn’t follow the thought through. The cap’s own documentation says twenty thousand is an order of magnitude more than any real tree has held, and exists to bound analysis of a tree that is pathological rather than large. I knew the number was deliberately absurd. I did not take the next step: a test that reproduces a pathological input at full fidelity is itself pathological.
The part I’m least happy about
When the test started failing roughly one run in two, I did not revisit its shape. I appended a timeout override — in an unrelated cleanup commit about replay-driver leftovers — with a long, careful comment measuring the cost on an idle machine, beside the sandbox tests, and under load. The comment contained every fact needed to reach the right conclusion. It described the test as filesystem-bound. It noted the runtime scaled with whatever else the run was doing rather than with the test itself. It observed that being killed mid-assertion reads as a broken analysis. And then it used all of that to argue for a bigger number.
Three things made that the easy path:
- There was a precedent immediately above it in the config. The sandbox tests already had a 120-second override, so “bound it from the work” looked like an established pattern rather than a new exception. But those tests genuinely spawn a sandbox and a type checker. Mine wrote files to a temp directory.
- The change rode along in a commit about something else. Nobody reviewing “replay leftover cleanup” was primed to ask whether a test should exist in that shape.
- I had framed the cost as a property of the assertion — what asserting a cap costs — rather than of the API. Once you believe the cost is inherent, a timeout is the only lever left.
The project already had a rule that a test which passes only on a retry is still a failing test. My override complied with the letter of that rule (no retries) while defeating its purpose. A test whose runtime depends on what else is running on the machine is exactly the timing dependence the rule exists to eliminate.
The fix
About thirty lines of non-test code:
- A
TreeBudgetsstruct holding the three limits, with aDefaultequal to the production constants. - The public
analyzeandwalkentry points became one-liners delegating toanalyze_withinandwalk_withinwith the default budgets. Nothing on the record path ever passes anything else, and the docs say so explicitly: a result produced under non-default budgets is not the definition the analyzer version stamps. - The truncation test now crosses a cap of 10 with 15 files, written in reverse.
- A separate one-line test pins the default budgets to the production values.
- Three new tests that had been impossible before: an exactly-full tree is not truncated, the parse-byte budget fires and names itself, the symbol budget fires and names itself.
That last point is the one that stings. Before the fix, neither the parse-byte cap nor the symbol cap was exercised by any test, because reaching them at production scale would have required gigabytes of input. The “honest” approach didn’t just cost 20 to 60 seconds of wall clock. It left two of the three tree-wide caps completely untested, because the same reasoning that produced the slow test made those caps untestable.
What I’m taking forward
A constant the code reads directly is an untestable constant. When I add a bound, limit, or threshold, it gets a seam at authoring time: a struct or parameter whose Default is the production value, a public entry point that only uses the default, and a one-line test pinning that default. A few lines, and every boundary test afterwards is cheap.
Test the mechanism small, pin the magnitude separately. “The cap truncates deterministically” and “the cap is 20,000” are two facts. Conflating them makes the test as expensive as the constant is large.
A test whose input size comes from a production constant is a smell. MAX_X + 50 as a loop bound should immediately prompt: what happens to this test when someone raises MAX_X? Here, raising the cap tenfold would have made the test ten times slower with nobody touching the test file.
“The cost is inherent” is a claim about the API, not about the test. When a test is slow, the first question is which parameter would make it fast — not which timeout would make it pass.
Precedent for a timeout override is not justification for another. I need to check whether the reason behind the precedent applies, not just that the pattern exists.
Doc-comment claims need checking against what the test actually exercises. My comment asserted that sorting made the dropped set a function of the tree. My test, writing files in ascending order, could not have detected a missing sort. The cheap version catches it.
Product constraints are not test-construction rules. “No wall-clock budget, content-derived bounds only” governs the analyzer. It says nothing about which values a test must use. Reading one as the other is how I got to “the only honest way is to exceed the real cap.”
If I were writing a guard for myself, it would be: a test that exercises a limit must be able to set that limit. If the code reads the limit as a bare constant, add the seam before writing the test, run the production entry point only under the default, pin the default’s value in its own test, never size a test’s input from a production constant, and never answer a slow test with a longer timeout before asking which parameter would make it fast.