By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
This lab converts a pile of ad-hoc assertions into a table-driven suite that reads like a specification. The subject is Relay's aggregation: counts by service, invalid lines not counted, and deterministic output. Each case gets a name, an input and an expected result; the test runner reports exactly which case failed and why.
Once the table is green, run it with -race. The detector only finds races that actually execute, so the suite needs to exercise concurrent paths: add a parallel subtest, or call the aggregator from several goroutines with separate inputs and merge the results. A race found by -race in CI is a bug that would have been a production heisenbug.
name, input and want fields; keep the case list readable.t.Run(tt.name, ...) subtest per case and a failure message that prints the input.t.Parallel() to subtests once the inputs are independent, then run with -race.-count=10 to shake out order-dependent behavior.package analyze_test
import (
"testing"
"relay/internal/analyze"
)
func TestCountEvents(t *testing.T) {
tests := []struct {
name string
events []analyze.Event
want int
}{
{name: "empty", events: nil, want: 0},
{name: "one valid", events: []analyze.Event{{Service: "api", Level: "info"}}, want: 1},
{
name: "mixed",
events: []analyze.Event{
{Service: "api", Level: "error"},
{Service: "worker", Level: "warn"},
},
want: 2,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := analyze.CountEvents(tt.events); got != tt.want {
t.Fatalf("CountEvents(%v) = %d, want %d", tt.events, got, tt.want)
}
})
}
}# The full suite under the race detector (the CI gate).
go test ./... -race -count=1
# Repeat ten times to catch order dependence.
go test ./internal/analyze -race -count=10
# Verbose subtests for the one suite you are changing.
go test ./internal/analyze -run TestCountEvents -race -v
# A race report is long; save it and read the two goroutine stacks carefully.
go test ./internal/analyze -race -count=1 > race.log 2>&1 || true| Habit | Why it matters |
|---|---|
| Named cases in a table | A failure names the behavior, not a line number |
t.Fatalf with inputs | The message contains everything needed to reproduce |
| Independent cases | Safe to run in parallel and in any order |
t.Parallel() plus -race | Exercises concurrent paths and reports unsynchronized access |
-count=10 | Catches tests that depend on order or shared state |
-run filtering | Fast inner loop while editing one suite |
Key takeaways
Table-driven tests with named subtests turn assertions into a readable specification.
Include empty, invalid and boundary cases; not just the happy path.
t.Parallel() plus -race is how you exercise concurrent paths in tests.
Run -count=10 to detect order dependence and shared state.
The race detector is necessary but not sufficient — it only sees executed code.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.