Testing · Engineering

The test that counted waits with a stopwatch

A test pins the prep-areas screen at two database waits. It counts them by timing, and under a busy suite it counted a third. The code had not changed.


One of our office screens, the one that lists a kitchen’s prep areas, is pinned by a test that says it waits on the database twice: once for the session and venue together, once for the areas. Three waits would mean a read had slipped out of the parallel block and into a queue behind it, which is the single commonest way a screen gets slow here, and the test exists so that nobody does it by accident.

The test counts waits with a stopwatch. It drives the route against a fake database where every statement costs twenty-five milliseconds, and it defines a wait as a statement that starts when nothing else is in flight. Two statements overlapping are one wait; a statement that starts after the previous one has finished is a new one. It is a neat way to measure structure without reading the code, and it had passed on every run since the route was last touched, in the spring.

On Monday the full suite ran all 7,935 tests at once and this one went red: three waits, not two. Run on its own, six of six passed. Run on its own again, four times, six of six. The route had not changed. What had changed was the load on the machine: with the whole suite competing for the processor, the session read that is supposed to overlap the venue read finished before the venue read started, so the fake clock saw nothing in flight and counted a new wait. The code was parallel. The timing was not.

The honest classification is a scheduling flake, and that is what the worklog says, with the measurements beside it. It is not a bug in the screen, and it is not quite a bug in the test either, because the test is measuring the thing it means to measure in the only way a black-box test can. A stopwatch counts time. Whether two things overlapped in time depends on the machine as well as on the code, and a test that fails when the machine is busy is telling you about the machine.

Two things follow. First, a red test is a question, not a verdict: is it the code or the test? On this codebase the answer has been the test, or the fixture, far more often than the code, and the written rule is to ask before fixing. Second, we did not loosen the test to make the red go away. Widening the overlap window would make it pass on a busy machine and also pass a genuinely serial read on a quiet one, which is the thing it is there to catch. It stays as it is, it is named as a known flake under full load, and anyone who sees it red is told to run it alone before believing it.

Operators meet the same shape when a kitchen screen is slow on a Saturday and fine on a Tuesday. Before touching the software, ask what else the machine was doing.


Try it on tonight’s service.

Nothing to install, no card. Not better by the weekend? Close the tab.