Calling my test doubles "test tooling" did not exempt them from the IO layer's rules
During a refactor of two services onto our layered architecture, I wrote the in-memory test doubles for one service’s IO interfaces, and I broke one of the architecture’s basic rules to do it. My reasoning was that test code didn’t count. It does.
What I did
The IO layer has a basic dependency rule: an IO implementation depends on no IO of another interface. Each implementation implements exactly one interface and owns the state for one subject.
In one service I did not write a double per interface. I wrote a single “records” hub instead. It held about eighteen in-memory tables (resources, blobs, manifests, tags, uploads, grants, credentials, audit and others) behind one mutex. Then I wrote sixteen transient IO doubles, and each one’s only field was that hub. Every call on any double locked the state of all of them. The hub also imported all sixteen doubles, several production initializers, a set of production “exploding” implementations and the full IO aggregator, so it could build a whole aggregator over itself.
The result was a 14-module import cycle, the largest in any crate we refactored.
At the top of every file I put an ignore comment saying the testing module is test tooling outside the layers, compiled only under test. The comment suppressed a different rule, the one that says a project adds no top-level source folders of its own. It said nothing about the dependency rule I was actually breaking. Because the checker only looks for that violation under io/ folders and my code was under testing/, it never flagged anything. Reviewers saw an ignore comment that looked deliberate and moved on.
In the other service I made a smaller version of the same mistake. A double under testing/ implemented an IO interface under a name that didn’t include the interface’s name, so a reader couldn’t tell what it stood in for.
Why it matters
- None of the sixteen doubles can be built, tested or reused on its own.
- A test of one IO’s behaviour depends on the state layout of every other IO. A change to the uploads table can break a grants test.
- The
testingmodule claims to be a leaf, but it reaches back into production IO code and the aggregator. - The violation was hidden from the tooling by where the files lived and from people by a comment that named the wrong rule.
The operator’s ruling was short: being test-only does not let these types break the rule. Keeping IOs of one interface from depending on IOs of another is a foundational rule of the layer.
Where my reasoning went wrong
I had absorbed an earlier lesson: code that only tests call is test tooling and lives outside the layer folders. I stretched that into “outside the layer rules”. The lesson says nothing of the kind. A transient implementation is the architecture’s own named test double for a stateful IO. It is an IO implementation, and the IO rules were written with it in mind. Compiling it under a test flag doesn’t change what it is.
There’s an easy check I skipped: take the question “may a test double depend on another test double?” and replace “test double” with “IO implementation”. The rule answers it immediately.
I also followed the earlier placement advice to the letter: test-only code goes in a testing module behind a feature. It didn’t help, because that advice is about where code goes, and my defect was about what it depends on. Those are separate questions, and answering one doesn’t answer the other.
What I should have written
- One transient implementation per IO interface, each owning only the state for its own subject, placed in that interface’s folder and compiled always or behind the testing feature.
- No shared hub. If several doubles need to agree on a fact for a test, the test sets it up through the services, or passes the shared value to each double when it’s constructed.
- Names that follow the convention: an implementation’s name is its interface’s name with the implementation’s name in front, such as
TransientGrantsIO. - Ignore comments only for the rule the code actually breaks. If I’m suppressing a folder rule, I have to ask what other rules the code in that folder breaks, then fix them or name them openly.
What I’ll do differently
- I’ll treat “test-only” as a fact about when code runs, not as a category with its own rules. The architecture has no such category.
- Before I write an ignore comment, I’ll list every rule the file could break, not only the one the tool flagged.
- When I measure a cycle or dependency in a refactored crate, I’ll report it next to the rule it breaks, or say explicitly that it breaks none. A bare count isn’t enough. The analysis that first found this cycle listed it without naming a rule, and the operator had to point the rule out before every crate was checked.
Signals for reviewers
- Two or more transient or in-memory IO doubles sharing one field type.
- A file under
testing/ordoubles/importing from production IO, initializers or the aggregator. - An ignore comment for a placement rule on a file that also imports a sibling implementation.
- An IO double whose name doesn’t include its interface’s name.
The number I’m tracking after each refactor is how many IO implementations, production and test, import an IO of another interface. This time it was sixteen in one crate through the hub, one misnamed double in another, and zero among the other crates’ doubles, which were all written one per interface with their own state. The target is zero everywhere.