One green run tells you almost nothing about a flaky test. A failure rate tells you more.
This post covers three flaky tests, their failure rates before and after each fix, and how many clean runs it takes to verify a fix.
Everything ran on Windows 11 with Go 1.26 and Node 24, and all numbers come from real runs.
1. A data race in Go: 85 of 300 runs failed
This test starts goroutines that each call Inc() on a shared counter, then checks the total.
type Counter struct{ n int } func (c *Counter) Inc() { c.n++ } func (c *Counter) Value() int { return c.n } func TestConcurrentInc(t *testing.T) { c := &Counter{} var wg sync.WaitGroup for i := 0; i < 30; i++ { wg.Add(1) go func() { defer wg.Done(); c.Inc() }() } wg.Wait() if c.Value() != 30 { t.Fatalf("got %d want 30", c.Value()) } } With 30 goroutines, 85 of 300 runs failed (28%). With 1000 goroutines, 299 of 300 failed. Output looked like got 993 want 1000 and got 969 want 1000.
A 28% flake often passes on a re-run, so it is easy to blame CI. The failure pattern shows the real cause: the total is always slightly under the target, and the failure rate rises with the goroutine count.
The race detector would confirm it, but go test -race stopped with:
go: -race requires cgo; enable cgo by setting CGO_ENABLED=1 On Windows, the race detector requires cgo and a C compiler. This machine had cgo disabled and no gcc, so the proof came from repeated runs instead.
The fix is an atomic counter:
type Counter struct{ n atomic.Int64 } func (c *Counter) Inc() { c.n.Add(1) } func (c *Counter) Value() int { return int(c.n.Load()) } After the fix: 0 of 300 runs failed.
2. Test order: shuffle, then replay the seed
Here, one test depends on another running first.
var ready bool func TestOrderA(t *testing.T) { ready = true } func TestOrderB(t *testing.T) { if !ready { t.Fatal("TestOrderB depends on TestOrderA having run first") } } go test -shuffle=on -v prints the seed it used. It was run 4 times. Three passed and one failed. The failing run printed seed 1795465939597397000.
Replaying it with go test -shuffle=1795465939597397000 -v failed 3 times out of 3. A random failure is now reproducible on demand.
The fix is to have TestOrderB set up its own state. The fixed version was not re-run for this post.
3. A timing test: 21 of 40 runs failed
This Node test sleeps 100 ms and asserts the elapsed time is between 99 and 105 ms.
import { test } from 'node:test'; import assert from 'node:assert/strict'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); test('waits about 100ms', async () => { const t0 = performance.now(); await sleep(100); const dt = performance.now() - t0; assert.ok(dt >= 99 && dt <= 105, `elapsed ${dt.toFixed(1)}ms`); }); It failed 21 of 40 runs (52%), with output like elapsed 107.7ms. Timers fire late. A tight window on a real clock will eventually fail.
The fix is a fake clock. Node's test runner includes one:
import { test, mock } from 'node:test'; import assert from 'node:assert/strict'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); test('waits 100ms (fake timers)', async () => { mock.timers.enable({ apis: ['setTimeout'] }); let done = false; const p = sleep(100).then(() => { done = true; }); mock.timers.tick(99); await Promise.resolve(); assert.equal(done, false); mock.timers.tick(1); await p; assert.equal(done, true); mock.timers.reset(); }); After the fix: 0 of 40 runs failed.
How many clean runs is enough?
Use the rule of three. With zero failures in n runs, the failure rate is below about 3/n with 95% confidence.
- 300 clean runs: below 1%
- 3000 clean runs: below 0.1%
For a 28% flake, 300 clean runs is conclusive. For a 1% flake, you need about 300 runs just to be 95% sure of seeing it fail once, so run well past that.
What to do next time
- Reproduce first. Run the single test 100 times and count the failures.
- Name the flake type: real clock, shared state, test order, data race, randomness.
- Fix the cause, not the symptom. Retries and longer sleeps hide the bug.
- Prove the fix with before and after failure rates.
Disclosure
I turned this method into an agent skill called Flaky Test Hunter. It is for sale on Agensi for $3. Everything above works without it.