My service test took 36 seconds because its fixture wrote a thousand fsync'd files through the whole app
A before/after comparison of our test reports showed that some tests I rewrote during a refactor had become very slow. One service test went from 0.14 s to 36.5 s, and the suite it belongs to went from 16 s to 38 s wall-clock. Three tests in a second library went from about 1 s to 12–20 s each. Each of them checks one small property of one service. Under our architecture they are unit tests and should take milliseconds.
The cause isn’t the code being tested. I wrote fixtures that do far more than the assertion needs, and every local and CI run pays for it.
What I did
Test one: trigger evaluation within a budget. The test checks that when a memory store holds its maximum of a thousand text triggers, evaluation stays within a 200 ms budget: the one matching trigger fires and none is reported as skipped.
The old version seeded the thousand triggers in a single in-memory write. It carried a comment saying so: seed in one write rather than a thousand commits, because each commit syncs the store to disk.
When I moved the test during the refactor, I rewrote it on top of a convenience fixture that:
- builds the whole application harness over temporary directories, with the scheduler, module families and a scripted model client;
- seeds each trigger through the production file-backed store, one commit at a time. Every commit rewrites the whole store file, calls
sync_all, and renames the file over the old one. The file grows with each trigger; - then runs a full session through the scheduler.
That comes to a thousand open/rewrite/fsync/rename cycles on a growing file, followed by a full session, just to check that a counter is zero. The service under test is a small part of what actually runs. I also deleted the comment that explained how to avoid this cost. The previous author had already spotted the problem and designed around it, and I put it back.
Tests two to four: splitting large appends. These check that an append bigger than the 8 MiB request limit gets split, with each request continuing from the previous answer, and that a single oversized event is refused. My fixture builds five events of about 3 MiB each (roughly 15 MiB of JSON). It runs them through a recorded-HTTP replay layer that parses both the sent and recorded bodies into JSON trees to compare them, and it streams the replies as chunked bodies on a simulated timeline. I didn’t profile exactly where the extra time goes. It doesn’t change the conclusion, because none of that work has anything to do with the splitting rule.
Why it’s wrong
Our architecture puts every effect behind an interface, and every service is built over interfaces. The reason is so a test can swap in an in-memory implementation that is isolated, deterministic and fast. A service tested over in-memory IO is a unit test, however much it orchestrates.
I went around that design in two ways.
- The memory store IO had no in-memory implementation. It had only a “fails if called” stub and the real file-backed one. A test of a service that depends on it can either touch disk or not run, so it touched disk. A missing double is the structural cause. The fsyncs are what made it visible.
- I tested services through the application’s composition root, not through their constructors. The constructor takes interfaces, and that’s where the test is supposed to plug in. Going through the full harness got me an integration test’s cost for a unit test’s assertion.
For the size-bound tests we already have a rule: a bound on a count is a parameter the test sets small. Reaching a production-sized 8 MiB constant with real megabytes is exactly what that rule exists to prevent.
What I should have done
- Write the in-memory implementation of the store IO. The interface already existed, so the double was the whole change. Service tests then seed a thousand triggers in microseconds. The file-backed implementation gets its own focused tests against a temporary directory.
- Build the service under test directly over its interfaces, with an in-memory or scripted double behind each one, and never through the app’s builder.
- Failing that, move the test as it was. When code moves, its tests move unchanged. A comment that names a cost being avoided is part of the test’s design, and deleting it should have made me stop.
- Make size limits parameters. Set the limit to a few hundred bytes, use events of a few dozen bytes, and the splitting logic is tested just as thoroughly in milliseconds.
- Read the per-test timings after each refactor chunk, before it lands. The build already produced JUnit reports after every step, and the numbers were sitting there. A service test over ~100 ms should have been treated as a problem then, not found in a later comparison.
Signals I’ll watch for (and that reviewers can too)
- A service test fixture that holds a temp directory, the full test harness, or an entire IO aggregate.
- An Arrange section with a loop of hundreds or thousands of iterations that awaits a real IO each time.
- An IO module with a “fails if called” stub and one real implementation, but no in-memory one.
- Test inputs built with
repeat(n * 1024 * 1024)or any production-sized constant just to cross a limit. - Any test under the services layer that takes over a second in the report.
- A diff that removes a test comment starting with “rather than” or “instead of”. It’s probably explaining a cost someone already avoided.
What I’m tracking now
After each refactor, I’ll record the slowest service-layer test per crate and the function where its time goes. Right now those are 36.5 s and 19.7 s, both more than a hundred times what the architecture expects. The fix is to add the missing in-memory double and put each test back at the service layer.