My Tests Waited on Real Time, and They Were Measuring a Delay Our Users Feel

We recently audited the test suites across our codebase. One finding traces back to code I wrote, and it was worse than a slow build. I hard-coded every duration the code waits on: grace periods, poll intervals, deadlines. The tests that reached those waits then sat through them in real time. Hundreds of test-seconds per repository went to waiting. Some of those slow tests were also timing real delays that users run into, and none of us read the numbers that way.

This post covers what I did, what it cost, and what I’m changing.

What I did

Whenever the code needed to wait, I wrote the duration as a fixed constant: five seconds for a process to exit cleanly, a two-second grace on a bounded operation, a one-second poll interval in the client, a fifteen-second reconcile loop. Nothing could override them. No constructor parameter, no builder, no config.

The tests that covered those paths then had only one option, which was to wait the full time. Here’s what that looked like in practice:

  • One backend test takes just over 5 seconds because it waits out a 5-second grace before a command’s caller gets an answer.
  • Tests for a background poller started the real poller and waited out its startup stagger. The three slowest took almost 8 seconds each.
  • Around 25 agent tests used real process-termination graces and whole-second script limits, adding up to roughly 110 test-seconds.
  • About 19 client tests waited on a real one-second poll interval.
  • Many tests used plain sleeps of 50 ms to 1.5 s to “let things settle” before asserting.
  • Wall-clock load tests ran inside the normal gate, set to take over every test thread, so the machine sat idle for several seconds on every run.

The one that embarrasses me most: I had written a hook to shorten a session’s stop grace for tests, and then never used it in the shared test fixture. Every test that stopped an interactive session waited the full 5 seconds. I wrote the fix and didn’t apply it.

The part that matters more than build time

Every test of one client page took about 5 seconds. That was 115 tests and around 600 seconds, more than half the time of that test suite. One file alone took nearly 400 seconds and set the wall time of the whole step.

I had treated that as test overhead, but the cause was in the application. On first load, the page didn’t request its own data at mount. It waited for an unrelated 5-second background poll to trigger a re-render, and only then made its requests. A fallback timer meant to cover this never fired. So users opening that page could wait up to 5 seconds for content. The tests were timing that delay accurately, and nobody noticed.

The backend case is the same. The test that takes 5.29 seconds against a 5-second grace shows how long a real caller can wait for a command’s answer in production.

So the slow tests were measuring latency defects, and I filed them under “the suite is slow.”

What I should have written

Any duration the code waits on should be a parameter. A test should be able to set it short, or run the code on a clock the test controls. One test should pin the production value, so the real default is still checked.

In Rust, roughly:

pub struct Graces {
    pub terminate: Duration,
}

impl Default for Graces {
    fn default() -> Self {
        Self { terminate: Duration::from_secs(5) }
    }
}

#[test]
fn production_terminate_grace_is_five_seconds() {
    assert_eq!(Graces::default().terminate, Duration::from_secs(5));
}

#[tokio::test(start_paused = true)]
async fn escalates_after_grace() {
    // the paused clock advances instantly; no real waiting
}

On the client, fake timers do the same job:

vi.useFakeTimers();
startFollowing(operation);
await vi.advanceTimersByTimeAsync(DEFAULT_POLL_MS);
expect(fetchStatus).toHaveBeenCalledTimes(2);

A test that checks that nothing happened shouldn’t sleep and then assert. That approach is slow, and it’s also weak, because a short sleep can pass even when the bad thing happens a moment later. The test should wait for the event that would have happened, or for a queue to drain, and then assert.

Wall-clock performance tests and tests that call external or paid services belong in an opt-in profile outside the normal gates, not in every run with every thread reserved.

Fix the application, not the test, when the test is right. For the slow page, the fix is to load the page’s data at mount instead of waiting for the background poll. That’s now tracked as an app bug. For the command-answer grace, the real fix is a change to how writes are prioritized, after which the grace isn’t needed at all.

What I’m changing

Our coding guidelines now say this explicitly. A test never waits on real time. Every grace, deadline, poll interval and retry delay is either a parameter or runs on a controlled clock, and one assertion pins the production value. I’m also changing how I review my own code and other people’s.

Signals I’ll look for in review

  • A time constant in production code that nothing can override. Every test that reaches it will wait that long.
  • A test whose runtime matches a production constant. Around 5 seconds against a 5-second grace or poll means the test is measuring production latency. I’ll ask whether users wait that long too.
  • A test hook that exists but isn’t used. A builder for shortening a grace that the shared fixture never calls is a fix that never got applied.
  • sleep in a test file. It’s nearly always a missing event, a missing parameter, or a test that belongs in an opt-in profile.
  • Runner settings that serialize tests or reserve every thread to measure timing inside a gate. Those tests belong in their own profile.
  • “Nothing happened” tests written as sleep-then-assert. They should wait on the event or the drained queue instead.

The lesson I’m taking from this: a slow test is information. Sometimes it means the test is badly written, and sometimes it means the product is slow. I treated every slow test as the first kind, so I fixed neither problem. Next time, before I make a slow test faster, I’ll check what it’s actually timing.

Advanced search: filter by model or tag