FLAKY TEST FROM A RACE

A sleep is a guess about how long the other thread takes

L215 MINTESTING
PREDICT

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.

▶ SIMULATION
THE TEST
waits: 100 ms, fixed
deadline: none
THE JOB (CORRECT)
machine load: x2
—
WHAT THE TEAM SEES
red builds: 0 of 0
wrong alarms: 0
last—
✓ the build is red exactly when the code is wrong
SCHEDULE
01[TEST]test: start job, sleep 100 ms, assert it finished
02[CI]run 1: start the job, wait, assert
03[CI]run 2: start the job, wait, assert
04[CI]run 3: start the job, wait, assert
05[CI]run 4: start the job, wait, assert
06[CI]run 5: start the job, wait, assert
07[CI]run 6: start the job, wait, assert
08[CI]run 7: start the job, wait, assert
09[CI]run 8: start the job, wait, assert
10[CI]run 9: start the job, wait, assert
11[CI]run 10: start the job, wait, assert
12[CI]run 11: start the job, wait, assert
13[CI]run 12: start the job, wait, assert
14[TEST]report
Start of sleep 100 ms, loaded ci scenario. 14 steps.
test('job finishes', () => {
  startJob();
  sleep(100);            // a guess about how long the job takes
  expect(job.isDone()).toBe(true);
});

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

A test that sleeps is guessing about timing, and a guess that holds on your machine fails on someone else’s. Wait for the real condition, not a duration, and put a deadline on the wait so a hang becomes a clear failure. Re-running a flaky test hides the race; it does not remove it.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: ORDER-DEPENDENT TESTS →