Testing · Engineering

A test that held each read open for eight milliseconds

Counting how many times a route waits is easy against a real database and wrong against an instant one. The fix is to make the fake take as long as the real.


We test how many times a route waits on its database by running the route against a real SQLite file wearing the same face as our production database, with every read tapped. A read that starts while another is still in flight is part of the same wave; a read that starts after everything before it has finished is a new wave. Count the waves and you have the number of round trips a visitor on the far side of an ocean will pay.

That worked for the menu route, where the first read must finish before the rest can start. It gave the wrong answer for the status route, which we had just rewritten to start all three of its reads at once. The test reported two waves, not one, and for a minute it looked as though the rewrite had not landed.

It had. The session lookup hashes the token before it touches the database, which takes a moment, and in that moment the other two reads against an instant SQLite had already started and finished. By the time the session read began, nothing was in flight, so the tap counted it as a second wave. The route had waited for none of its siblings’ answers; the harness could not tell the difference between waiting for an answer and merely starting later than something that was already done.

A real database read takes tens of milliseconds, which is exactly what makes the waves overlap in production. So the tap now holds each read open for eight milliseconds after it answers. The brand and count reads are still in flight when the session batch starts, the three overlap the way they do on the real thing, and the count comes back as one. Against the old handler the same test reports three waves signed in and two as a stranger, which is the proof that it bites.

The first draft of the test also failed on the new code for a second reason, and that one was the test’s fault too: it asserted the resolved brand carried a non-empty stylesheet string, and for a venue using the default palette that string is correctly empty. The route was right both times. When a test fails, the first question is whether the test or the code is wrong, and twice in one morning the answer was the test.


Try it on tonight’s service.

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