FLAKY TEST FROM A RACE
A sleep is a guess about how long the other thread takes
A test starts a background job, sleeps 100 ms, then asserts the job finished. On your laptop the job takes 30 to 90 ms. The CI machine is busy and the job takes up to 200 ms. The code is correct. What does CI show over 100 runs?
A flaky test is a race between two things the test never ordered: the job finishing, and the assert reading its result. The sleep does not order them. It bets that the job wins. Under load the job runs slower and the bet loses. It is a race on ordering, not on shared memory, but it is a close relative of the data race: the outcome depends on which side gets there first.
The fix is to wait for the thing you need. Poll the real condition, or wait for the event that announces it, and give the wait a deadline. A slow run then passes, a fast run does not waste time, and a job that never finishes fails with a message that says so.
test('job finishes', () => {
startJob();
sleep(100); // a guess about how long the job takes
expect(job.isDone()).toBe(true);
});test('job finishes', async () => {
startJob();
await waitFor(() => job.isDone(), { timeout: 5000 }); // the real condition, with a deadline
expect(job.result()).toBe('ok');
}); Watch the invariant strip and the WRONG ALARMS count. In both sleep scenarios the code is right every run. On the quiet laptop the strip stays green. On the loaded CI it goes red the first time a job takes longer than 100 ms. In FIX the load is the same and no run is red. In the last scenario the code really is broken, and the red build is correct.
The code under test is correct. Raise the CI load until the sleep-based test turns at least 7 of its 12 runs red.
GOAL: 7 or more of 12 runs red
Write waitForJob(env, timeoutMs). It must return as soon as the job is done and throw an Error whose message says it timed out if the job is not done after timeoutMs. The grader runs the job at many seeded latencies, including a slow one (4.9 s) and one that never finishes, in simulated time. A wait that never ends fails.
UI tests that sleep before a click, integration tests that wait for a queue consumer, tests that read a file another process writes, anything with Thread.sleep, time.sleep or setTimeout before an assert. Flakes cluster on shared CI runners under load. Fowler's advice in 'Eradicating Non-Determinism in Tests' is never to use bare sleeps to wait for asynchronous responses: use a callback or polling. Order and shared state cause flakes too: see order-dependent tests.
“This test fails one run in fifty in CI. What do you do?” Do not add a retry or a longer sleep. Reproduce it by controlling the schedule or loading the machine, find what the test assumes about ordering, then wait on the real condition with a deadline. Say how you would tell a flaky test from a real race in the product: run the test many times under load, and read what the red runs have in common.
USED IN
Chapters that build on this lesson.
- End-to-end and browser testsSoftware Testing and Quality
- Flaky tests: causes, quarantine and cureSoftware Testing and Quality
- Testing time, randomness and concurrencySoftware Testing and Quality