gotestyourself / gotestsum

'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.
Apache License 2.0
2.04k stars 119 forks source link

race detected output inconsistently reported #310

Open edaniels opened 1 year ago

edaniels commented 1 year ago

With the following test and gotestsum -- -race, the race detector output is inconsistently output.

package main

import (
    "fmt"
    "testing"
)

func TestRace(t *testing.T) {
    a := 1
    go func() {
        a = 2
    }()
    fmt.Println(a)
}

Expected (sometimes):

✖  . (112ms)

=== Failed
=== FAIL: . TestRace (0.00s)
1
==================
WARNING: DATA RACE
Write at 0x00c000126178 by goroutine 8:
  foo.TestRace.func1()
      /Users/eric/Downloads/temp/main_test.go:11 +0x2c

Previous read at 0x00c000126178 by goroutine 7:
  foo.TestRace()
      /Users/eric/Downloads/temp/main_test.go:13 +0xac
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1493 +0x40

Goroutine 8 (running) created at:
  foo.TestRace()
      /Users/eric/Downloads/temp/main_test.go:10 +0xa0
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1493 +0x40

Goroutine 7 (running) created at:
  testing.(*T).Run()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1493 +0x55c
  testing.runTests.func1()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1846 +0x90
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1446 +0x188
  testing.runTests()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1844 +0x6c0
  testing.(*M).Run()
      /opt/homebrew/Cellar/go/1.19.6/libexec/src/testing/testing.go:1726 +0x870
  main.main()
      _testmain.go:47 +0x2fc
==================

DONE 1 tests, 1 failure in 0.277s

Actual (sometimes):

✖  . (115ms)

=== Failed
=== FAIL: .  (0.00s)
=== CONT  
    testing.go:1319: race detected during execution of test
FAIL
FAIL    foo 0.115s

DONE 1 tests, 1 failure in 0.281s
dnephin commented 1 year ago

Thank you for the detailed bug report and the minimal reproduction!

I ran this test using go test -race -count=1 -json . and I noticed that sometimes the data race output has Test: "", and other times it has Test: "TestRace". This confirms the problem is an issue with test2json. The data race output is being attributed to the package instead of the test.

It looks like there is an open issue about this problem: https://github.com/golang/go/issues/58634

298 added a workaround for a similar bug and seems like it almost fixes the problem. That change is included in gotestsum v1.9.0. When I run the test case with v1.9.0 I see that sometimes the data race is attributed to the test, and sometimes it attributed the package, but very rarely it's neither. In those rare cases it must be missing from the go test output entirely.

Upgrading to gotestsum v1.9.0 should mostly work around the problem until it's fixed in the go toolchain.

edaniels commented 1 year ago

That does not appear to help the issue on v1.9.0

edaniels commented 1 year ago

Thank you for checking if it would though!

mitar commented 10 months ago

It is still an issue for me in v1.11.0.

epot commented 9 months ago

Just to confirm: I am using junit as output and not json and see the same issue, is it because the junit output is relying on json internally?

szykol commented 3 weeks ago

I also bumped into this issue using following command:

go run gotest.tools/gotestsum@latest -- -timeout 10m -race -v -count=1 ./...

any news on this issue?