A time limit with no test seam, and a fix that kept the real clock
I recently wrote a design for a time limit in one of our tools and got it wrong twice: first in the design, and then in my correction. No test had been written yet, so the problem was caught at the design stage. But implementation had already started from that design, and work had to be interrupted twice while I fixed it. This post covers what I did and what I’m changing.
What I did wrong
The requirement was simple: flag any task that runs longer than one threshold as slow, and stop any task that reaches a higher one. Both thresholds were a few seconds.
I wrote the thresholds as fixed constants. The design stated the figures directly in the requirements. Nothing made them values the library accepts, and nothing let a test control the time being measured. Anyone building from that design could test the stop in only one way: run something that really consumes the full limit. The limit was on compute time, not elapsed time, so that test would have been a busy loop on a shared build machine, which we already prohibit.
I noticed the problem and turned it into a question. I saw that testing the stop would mean burning the real limit, and that this broke a standing rule. Instead of fixing the design, I asked the project owner to choose between three options:
- a configurable limit
- a test that burns the full limit
- leaving the stop untested
Two of those three were defects. I recommended the right one, but I still presented it as a choice. The question had also been settled already. We have hit this pattern before with other bounds, and the rule that came out of it is that every bound is a parameter and no test’s run time should equal a production constant. The owner had to restate a decision they had already made, while the build went ahead on the flawed design.
My correction kept the real clock. After the owner objected, I made the thresholds configurable and wrote that tests could either set limits of a few milliseconds or drive a fake time source. I treated these as equivalent, and they aren’t. A test with a real millisecond limit still asks the machine how long something took:
- A task meant to finish under the limit can go over when the machine is busy.
- A task meant to go over has to do real work for at least that long.
That test is flaky by construction. I had removed the waiting, which was the literal complaint, and kept the dependence on real time, which was the reason for the complaint. The owner had to correct me a second time, minutes after the first.
What I should have done
The first draft should have contained all of this, with nothing put to the owner:
- Configurable thresholds. The thresholds are configuration, with the production values as defaults. Requirements refer to “the slow mark” and “the limit”, and state the defaults once.
- An injected time source. Measured time is read through an interface the library is given. Production supplies the real source. Tests supply a fake and script its readings, so a task “took” just under or just over a threshold because the fake says so.
- A stated testing rule. The testing requirements name the slow flag and the stop as behaviours that have a test, and say that test drives the fake. No test reads real time to decide either outcome, and a tiny real limit is not an accepted substitute.
The two seams do different jobs, and both are needed. Configurability keeps the figures out of the code. The fake time source makes the test deterministic. For a bound on time, configurability alone only changes how much real time the test depends on.
The design is now being amended on that basis, with the fake time source as the only permitted approach.
What I’m changing
- I’ll settle testability at design time. Earlier occurrences of this pattern were found in tests that already existed. This time the specification left no good test to write. When I put a duration or a count into a requirement, the parameter goes into the same change.
- I won’t turn settled rules into open questions. A question of the form “testing X needs the real N; decide whether N is configurable” has one acceptable answer. I’ll fix the design and report the fix as done.
- I’ll test counts and durations differently. A count limit can be set small and crossed deterministically. A time limit needs a fake time source. Some of our written testing guidance says to set a duration short or advance a clock, and my flawed correction followed that wording exactly. I’ll propose removing the “or”.
- I’ll apply a correction by its reason. The owner objected to idle tests because tests tied to real time are slow and flaky. I should have checked whether my fix still read real time, not only whether it still waited.
In review, I’ll treat these as warning signs:
- a literal duration or count in a requirement with no matching parameter in the same change
- “short timeout” or “millisecond limit” in a test plan
- a time-dependent component whose design names no time-source interface