Limits of race and stress tests
Limits of race and stress tests
A race-free run is not proof of correct concurrency. A stress test repeats executions to exercise more schedules and inputs, but cannot enumerate all interleavings. Use go test -race -count=20 -cpu=1,2,4 -timeout=60s on a small suite to vary GOMAXPROCS and repeat tests. Combine this with deterministic assertions, cancellation checks, ownership and invariants.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "sync" "sync/atomic" "testing")
func TestLesson(t *testing.T) { var balance atomic.Int64 balance.Store(1) ready := make(chan struct{}, 2) release := make(chan struct{}) var wg sync.WaitGroup for i := 0; i < 2; i++ { wg.Add(1) go func() { defer wg.Done(); old := balance.Load(); ready <- struct{}{}; <-release; balance.Store(old - 1) }() } <-ready <-ready close(release) wg.Wait() if balance.Load() != 0 { t.Fatal("unexpected balance") }}Both workers read 1 and store 0; the final balance is zero although two withdrawals were logically attempted from one available unit. This deliberate logical-bug illustration uses only atomic accesses, so a clean race detector and this weak assertion do not establish correctness. A real test must also count successful reservations. The second example repairs this using a compare-and-swap.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonAssert the complete invariant
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "sync" "sync/atomic" "testing")
func TestLesson(t *testing.T) { var balance atomic.Int64 balance.Store(1) var successes atomic.Int64 var wg sync.WaitGroup for i := 0; i < 2; i++ { wg.Add(1) go func() { defer wg.Done() if balance.CompareAndSwap(1, 0) { successes.Add(1) } }() } wg.Wait() if balance.Load() != 0 || successes.Load() != 1 { t.Fatal("invariant failed") }}For this one-unit reservation, exactly one CAS may succeed because no worker restores the value to 1. A general balance requires a retry loop or a mutex guarding the condition and update together. Races, logical invariants, deadlocks, fairness and latency are distinct properties. Stress tests can reveal failures without proving their absence.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Use a mutex to protect a balance of 2. Start four workers each attempting to reserve one unit; count successful reservations under the same lock. Assert zero balance and exactly two successes. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Concurrency testing 08: Limits of race and stress tests
Check the lesson objective before moving on.
What does a clean -race stress run prove?
What assertion catches the two-withdrawal bug?