Check the throttling test against elapsed time, not how many calls slept

Merged
RO
2mo ago
+22-8

Problem

TestGateQueuesTenParallelWorkersThroughTheDefaults failed three times on 1 October, on three unrelated PRs (#815, #816, #791), and passed each time on a rerun. It counted how many of the 30 calls the starting token bucket can't cover slept for a refill, and wanted at least 20. On a loaded runner, under the race detector, the calls themselves are slow, refills land between them, and a call that finds a token waiting is served without sleeping. That's correct limiter behaviour, so the count was measuring the runner (it came in at 7, 17 and 19).

Solution

The test now checks the throttling by time: 80 calls against a 50-token bucket refilling at 10 a second can't finish in under 3 seconds on any machine, and a slow one only takes longer. It asserts the run took at least 90% of that, and that at least one call slept (so the wait path and its counter are exercised). The bulkhead and success assertions are unchanged.

To check it still catches a real bug, I broke the limiter to refill five times too fast: the test fails with "30 calls past the starting bucket cannot finish before 30 tokens refill (3s); it took 623ms". It passed 40 runs under the race detector with four copies competing for the CPU. I couldn't reproduce the original flake locally (the old version passed those runs too), so the CI failures above are the evidence for it.


Summary by cubic

Fixes the flaky TestGateQueesTenParallelWorkersThroughTheDefaults test, which throttling assertions counted calls that slept for a refill and required at least 20 of 30. On a loaded runner, refills land between slow calls so fewer calls need to sleep, and the test failed three times in a day on unrelated PRs.

The test now measures throttling by elapsed time instead. It asserts the run takes at least 90% of the time 30 tokens take to refill (3 seconds at 10 per second) — true on any machine, slower ones only take longer — and that at least one call slept, so the wait path is exercised.