TestRaceAttributedToPassingTest covers the case where go test attributes a data race report to a test that itself reports PASS (e.g. when the racing goroutines outlive the test that spawned them and TSan prints during a different test's window). Without the race-detection fix, the WARNING: DATA RACE
(t *testing.T)
| 277 | // stuck in a passing test's log buffer and dropped on the floor. |
| 278 | // See https://github.com/tailscale/tailscale/issues/19603. |
| 279 | func TestRaceAttributedToPassingTest(t *testing.T) { |
| 280 | if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { |
| 281 | t.Skip("test requires the race detector, which needs linux/amd64") |
| 282 | } |
| 283 | t.Parallel() |
| 284 | |
| 285 | testfile := filepath.Join(t.TempDir(), "race_test.go") |
| 286 | code := []byte(`package race_test |
| 287 | |
| 288 | import ( |
| 289 | "sync" |
| 290 | "testing" |
| 291 | ) |
| 292 | |
| 293 | var counter int |
| 294 | var wg sync.WaitGroup |
| 295 | |
| 296 | func TestSpawn(t *testing.T) { |
| 297 | wg.Add(2) |
| 298 | go func() { defer wg.Done(); counter++ }() |
| 299 | go func() { defer wg.Done(); counter++ }() |
| 300 | } |
| 301 | |
| 302 | func TestWait(t *testing.T) { |
| 303 | wg.Wait() |
| 304 | } |
| 305 | `) |
| 306 | if err := os.WriteFile(testfile, code, 0o644); err != nil { |
| 307 | t.Fatalf("writing package: %s", err) |
| 308 | } |
| 309 | |
| 310 | out, err := cmdTestwrapper(t, testfile, "-race").CombinedOutput() |
| 311 | if code, ok := errExitCode(err); !ok || code != 1 { |
| 312 | t.Fatalf("testwrapper %s -race: expected exit code 1, got %v; output was:\n%s", testfile, err, out) |
| 313 | } |
| 314 | if want := "WARNING: DATA RACE"; !bytes.Contains(out, []byte(want)) { |
| 315 | t.Fatalf("expected race report in output, got:\n%s", out) |
| 316 | } |
| 317 | |
| 318 | if testing.Verbose() { |
| 319 | t.Logf("success - output:\n%s", out) |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func TestBuildError(t *testing.T) { |
| 324 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…