ADR 0028 — The test site paces the stack by its counted in-flight work, and paced code holds no wall-clock deadline¶
- Status: Accepted (2026-10-25; brief CG, pull request 262)
- Date: 2026-10-25
- Related: ADR 0026 — architecture principles
(clock ownership, the logger rule), concept v4 (the test foundation,
docs/history/concept-v4-test-foundation.md),docs/guides/testing-in-process.md(the pacing model as a host reads it),notes/briefs/2026-10-24-CG-m4-flake-sweep.mdand its report,notes/parity/by_design.mdentriesBD-Matter-SitePacingByCountedWork,BD-Matter-SiteBuildWithoutBudget,BD-Matter-MdnsSocketReadDeadline,BD-Matter-BridgetestWallClock.
Context¶
fabrictest runs a device and a controller of this module in one process
on a fake clock and advances that clock only when the stack is quiet, the
way matter.js's MockTime.resolve moves time only when no task is ready
(packages/testing/src/mocks/time.ts at 862ad75f). Until brief CG the
site judged "quiet" by the process: ten milliseconds with no datagram sent,
nothing pending in the in-memory network and no runnable or running
goroutine beyond a standing count read from the Go runtime's scheduler
metrics. Two things were wrong with that on a loaded CI runner.
First, paced production code still held deadlines on the machine's clock:
fifteen context.WithTimeout sites in bridge/ (the store, advertising,
the OTA link, group messaging, the subscription re-establishment, the
handshake) and the node's stop. While the site paced the fake clock slowly
under load, those real deadlines expired, and a re-establishment or a stop
failed that passed on a quiet machine.
Second, the process is the wrong unit. Another test's goroutines made a
site look busy forever (starvation), and a goroutine of the stack that was
blocked on something the fake clock did not own was invisible, so the clock
moved past the instant it needed. The failures of 2026-10-24 were of this
family: a read sent on a session the device had just closed, reports that
never arrived in thirty seconds, a transition's end not observed in ten.
Each passed locally and under go test -count=50; each failed under eight
parallel race-enabled processes.
JavaScript can observe "nothing is ready" because it has one thread. Go cannot observe it per test. What the stack can state about itself is the work it has accepted and not finished.
Decision¶
- Paced code holds no deadline on the wall clock. Every component with
an injected
clock.Clockbuilds its deadlines withclock.WithTimeout/clock.WithDeadlineon that clock. A guard (clock/nowalldeadline_test.go, run with theclockpackage's tests) refusescontext.WithTimeout,context.WithDeadline,time.After,time.NewTimer,time.NewTicker,time.Sleepandtime.AfterFuncin the non-test code of the paced packages. Its allow list names the kept sites with their reason: the two mDNS socket read deadlines (the kernel's) andbridge/bridgetest(a wall-clock test helper over real sockets). - The stack counts its work.
internal/inflightis a counter group a component enters and leaves at the points where work is accepted and finished: a datagram from read to handler return, an exchange delivered to its handler, a report queued and not yet sent, a device callback queued, a node start, stop and build. Production cost is a nil check; the group travels with the clock (inflight.WithGroup), so a component needs no new parameter. - The site judges quiet by the counted work.
fabrictestdeclares the stack quiet when the counts and the in-memory network stood still forSettle(ten milliseconds). Counted work that stands still holds the clock for at most one second (holdBudget, the role of matter.js'sMAX_HOST_ASYNC_MS), then the site logs what it abandons and moves on;node.buildis exempt, because a construction waits for the CPU, not for a clock. The process-wide goroutine check survives only as a fifty-millisecond grace on an in-memory site and as the rule on aLoopbacksite, whose real sockets do work the counters cannot see. - A test moves the clock through the site.
Site.Step(d)advances under the quiet protocol;Site.RunandSite.Eventuallyadvance to the next timer. A test never callsFake().Advancewhile the stack may be working. - Flakes are reproduced, not retried.
make stressruns a test undergolang.org/x/tools/cmd/stresswith eight parallel race-enabled processes;make stress-flakesruns the named list, and the nightlyflakesjob keeps it green. A test that fails under stress has a root cause in the code or the test, never a retry.
Consequences¶
- A host that passes its own
Clocknow also paces the deadlines that were wall-clock before:subscription.ReestablishTimeout, the store timeouts, the advertise timeouts, the stop timeout. Onclock.System{}nothing changes. fabrictesttests that advanced the fake clock from inside a condition moved toSite.Step/Eventually. The guide names the two rules.- The counted set is a contract between the stack and the site: a new
path that accepts work and waits on a clock enters a counter, or the
site may move the clock past it. The
inflightpackage doc lists the counters and what each catches; a flake that survives stress is the first place to look for a missing one. - The runtime-metrics check is no longer the arbiter, so parallel tests of
one package no longer starve each other's sites;
holdSitestays only because sites still share one process's CPU. - A loopback site on a fake clock cannot see its sockets' work and judges
quiet by the process's goroutines, so two in parallel read each other's
work as their own.
fabrictesttherefore runs such sites one test tree at a time across the process — the loopback slot (fabrictest/loopback_slot.go,loopbackSlot), taken byNewSite(fabrictest/site.go): a site of a test outside the holder's tree waits until the holding test ends (holdLoopbackSlot, which releases the slot in the holder's cleanup). The node package'sholdSitedoes the same for that package's sites. A test that opens a loopback site and then callst.Parallelkeeps the slot while it is paused, so a serial test waiting for the slot would block it (inferred from the code, not run). - Divergences from matter.js are recorded in
by_design.md: the site counts work where MockTime observes a single thread; the build is exempt from the budget; the mDNS socket deadlines and bridgetest keep the wall clock.
Alternatives considered¶
- Keep the process heuristic and raise the quiet period. Slower and still wrong: a blocked goroutine is invisible at any period, and a neighbour test's goroutines still starve a site.
- Serialise every site test in the process. Hides the starvation, not the invisibility; halves the throughput of the suites that matter most.
- Run CI with fewer parallel packages. Moves the failure rate, not the cause; the fix belongs in the code, not in the runner flags.
- Per-test goroutine tracking through the runtime. Go offers no
per-goroutine ownership the site could read without instrumenting every
gostatement; counting the work at its seams is the smaller contract.