New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: check for concurrent use of T.FatalX and T.SkipX #15674

Open
dsnet opened this Issue May 13, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@dsnet
Member

dsnet commented May 13, 2016

The testing package is explicit about the following:

A test ends when its Test function returns or calls any of the methods FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as the Parallel method, must be called only from the goroutine running the Test function.

However, this is easy to overlook and it is not immediately obvious that the code below is a bad test:

func TestFoo(t *testing.T) {
    go func() {
        t.Fatal("fatal") // Called from outside the Test function
    }()
    time.Sleep(1 * time.Second)
}

This outputs (what a user expects):

--- FAIL: TestFoo (1.00s)
    foo_test.go:10: fatal

Giving the user a false sense of having written correct test.

However, when running the test with the -race flag, it becomes obvious this is not okay:

==================
WARNING: DATA RACE
....
==================
--- FAIL: TestFoo (1.00s)
    foo_test.go:10: fatal

Perhaps the vet tool can check for these cases.

@josharian

This comment has been minimized.

Contributor

josharian commented May 13, 2016

I think if the race detector catches the problem then vet doesn't need to check too.

@dsnet dsnet added this to the Unplanned milestone May 14, 2016

@dsnet

This comment has been minimized.

Member

dsnet commented May 14, 2016

The difficulty is that the Fatal case isn't always triggered, so the race detector isn't able to detect them. But when Fatal is actually hit, funky things can sometimes happen.

Go vet offers the ability to detect these abuses of the testing package earlier on.

It comes down to static analysis vs dynamic analysis.

dominikh added a commit to dominikh/go-staticcheck that referenced this issue Nov 12, 2016

Detect usage of T.FailNow in a goroutine
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>

dominikh added a commit to dominikh/go-staticcheck that referenced this issue Nov 13, 2016

Detect usage of T.FailNow in a goroutine
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>

dominikh added a commit to dominikh/go-tools that referenced this issue Jan 24, 2017

Detect usage of T.FailNow in a goroutine
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>

@ALTree ALTree added the help wanted label Jul 18, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment