Workbench / The TiffinBox Build
S55 · V3·54
Tests That Bite
Volume 3, chapter 54. TiffinBox has 78 server tests, a golden master, a contract test and a browser suite; this stage tests the tests. A hand-rolled mutation run first reported every mutant killed, because the suite couldn't run from where it was started; with a control in place, 18 of 38 changes to the access rules survived, including any kitchen opening any kitchen's screen. An access matrix test kills them. A test over every route found that the kitchen-event route told anonymous callers what was wrong with their JSON before asking who they were. A property test runs 200 random sequences of orders and events against stage 54's cache, a settable test clock replaces ad hoc clocks, and twenty runs of the suite measure its flakiness.
Stage 55: tests that bite
The brief
What you'll build.
exp/55/mutate.py: one mutation per mutant (comparisons,&&/||, negations, booleans,ALLOWinto a denial), compiled over a copy of the server's classes, the server suite run against each fromserver/; the unmutated classes first, as a control.AccessTest: placing, reading and changing orders, kitchens and admin routes, as rows of who × what × whose → status; what each mode sends (401 withWWW-Authenticate, a redirect for HTML GETs only, a 404 that says nothing, report mode's memory).- Two
TokensTestcases: expiry at exactlyexp; the key file'scurrentand its keys. - Routes that say who may call them:
Server.Routegainsaccess;RoutesTestrequires a sample path for every route, the listed public routes exactly, and a 401 from every other route (and the event stream) to an anonymous caller when access is enforced. - Authentication before validation on
POST /orders/{id}/events. OrdersPropertyTest: 200 random sequences of 60 placements and events, each with a printed seed and shrinking to the shortest failing sequence.TestClock: a settable clock for tests.exp/55/repeat.sh: the server suite N times, failures counted per test.- Measurements in
exp/55:mutate-no-control.out,mutate-before.out,mutate-after.out,routes-before.out,repeat.out,repeat-after.out.
Why a real company needs this. Test suites decay into ritual: they pass, so they're trusted. Measuring what they notice turns "we have tests" into a list of what's actually protected.
Why now. Chapter 54 is backend testing. Stages 38, 51 and 54 promised a settable clock, a test that every route declares its access, and a property test of what the cache relies on.
What you'll have at the end.
- access rules whose every branch is pinned by a test;
- a test that fails when a route is added without a rule;
- a property test guarding the cache's invariant;
- a flakiness number for the suite;
- ADR-0057: how TiffinBox tests.
Theory to own
- Controls. How do you know a mutation run, or a flake detector, can tell good from bad? (Companion, Gaps 1 and Theory 1.)
- Mutation testing. What does a surviving mutant tell you, and when can you ignore one? (The chapter, "Mutation testing"; Companion, Gaps 2 and Theory 2.)
- Rules over sets. How do you test "every route checks access"? (Companion, Gaps 3 and Theory 3.)
- Flakiness. Why is it a rate, and where does it come from? (The chapter, "Flaky tests"; Companion, Gaps 4 and Theory 4.)
Build it
Step 1: mutate, with and without a control
Write exp/55/mutate.py and run it on Access.java and Tokens.java. Then add the control and run it again.
Checkpoint. exp/55/mutate-no-control.out, mutate-before.out.
Step 2: the matrix
Write AccessTest (and Access.decisionForTest), the two token tests, and run the mutation again.
Checkpoint. exp/55/mutate-after.out.
Step 3: every route
Give each Route an access description, write RoutesTest, and run the server suite.
Checkpoint. exp/55/routes-before.out; then the fix in OrdersApi.event, and the suite passes.
Step 4: properties, a clock, and twenty runs
Write TestClock and OrdersPropertyTest; run exp/55/repeat.sh 20, fix what it finds, run it again, and run the release check.
Checkpoint. exp/55/repeat.out; core 57, server 88 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes.
Senior and architect lens
1. Measure the suite, not just the code. A mutation run on the riskiest code (access, money, pricing) gives a list of untested behaviours; that list is the testing backlog.
2. ADR-0057: how TiffinBox tests. Write it with these points:
- Levels: unit tests for rules (
AccessTest,OrdersTest), properties for invariants (OrdersPropertyTest), in-process HTTP for the server (ServerTest,RoutesTest), the golden master for compatibility, contract tests for the API, the browser suite for the screens, drills for failure (stage 53). - Rules over sets: every route declares access and is checked; new routes fail the test until added.
- Mutation:
mutate.pyonAccess,Tokens,Bodiesand pricing before each release; survivors are reviewed. - Determinism:
TestClockfor time, printed seeds for randomness, no sleeps where a signal is possible. - Flakiness:
repeat.shweekly; any test failing more than once in 100 runs is fixed or quarantined with an owner.
3. A test nobody can make fail isn't a test. Every new test is checked once against a broken version of the code it protects.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
mutate.py runs the whole suite per mutant |
47 mutants take about 15 minutes | stage 56 (projects: run only the tests that cover the mutated class) |
| the browser suite isn't mutation-tested | the web client's logic is only example-tested | Volume 8 (delivery: mutation testing in CI for both stacks) |
| the slow-disk drill runs by hand | overload regressions go unnoticed | stage 56 (projects: the drill in the release check) |
| sleeps remain in a few tests | timing flakiness | stage 56 (projects: replace them with signals) |
Evidence
exp/55/mutate-no-control.out,mutate-before.out,mutate-after.out,routes-before.out,repeat.out,repeat-after.out.mutate.pywith a control,AccessTest, the token tests,Route.accessandRoutesTest, the event route's order of checks,OrdersPropertyTest,TestClock,repeat.sh.- Core 57, server 88 and tbx 21 tests;
golden master: 47 exchanges, identical; the release check passes. - ADR-0057 is committed.
Carry forward
- Every rule has a matrix test, and a mutation run says it bites.
- New routes, new rows:
RoutesTestwon't let a route ship unlisted. - Time and randomness are inputs to every test.