You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
testing: Deferred functions run before parallel subtests are executed #17791
The test should pass, as according to the documentation t.Run should block until all parallel subtests are completed, and the deferred function should run only after t.Run returns.
What did you see instead?
The test fails:
% go test -v .=== RUN TestBug=== RUN TestBug/asdf--- FAIL: TestBug (0.00s) --- FAIL: TestBug/asdf (0.00s) foo_test.go:14: wanted 1337, got 7331FAILexit status 1FAIL github.com/adamwg/testbug 0.007s
Interesting test. Not sure it's a bug however. Depends on the definition of Parallel function and what sort of contract or agreement it has with the compiler.
% go test -v .=== RUN TestBug=== RUN TestBug/asdf--- FAIL: TestBug (0.00s) --- FAIL: TestBug/asdf (0.00s) foo_test.go:11: wanted 1337, got 7331FAILexit status 1FAIL github.com/adamwg/testbug 0.007s
However, based on the documentation (https://golang.org/pkg/testing/#T.Run) it's not clear that's necessary. I'd suggest either this should be fixed, or the documentation should be updated to make it clear that a call to t.Run does not block if t.Parallel is used in its test function.
t.Parallel is making the t.Run itself a parallel subtest of TestBug. There is no parallel subtest of the t.Run subtest. TestBug will not complete until the parallel test completes. But TestBug's body has to finish because that is what triggers running the parallel tests in the first place.
The group has one parallel subtest, asdf. Once the group's body returns, the parallel subtests are allowed to run in parallel (there's only one so that's not saying much), and once the parallel subtest finishes, t.Run("group"...) returns.
Activity
mrjrieke commentedon Nov 4, 2016
Interesting test. Not sure it's a bug however. Depends on the definition of Parallel function and what sort of contract or agreement it has with the compiler.
adamwg commentedon Nov 4, 2016
Note that this issue isn't actually limited to deferred functions. Any code after the parallel subtests runs before they do:
0xmohit commentedon Nov 4, 2016
The following works:
(Adapted from the example in "Cleaning up after a group of parallel tests" in the blog.)
adamwg commentedon Nov 4, 2016
@0xmohit Thanks, I can do that as a workaround.
However, based on the documentation (https://golang.org/pkg/testing/#T.Run) it's not clear that's necessary. I'd suggest either this should be fixed, or the documentation should be updated to make it clear that a call to
t.Rundoes not block ift.Parallelis used in its test function.rsc commentedon Nov 11, 2016
This is working correctly.
The docs for t.Run say:
In this code:
t.Parallel is making the t.Run itself a parallel subtest of TestBug. There is no parallel subtest of the t.Run subtest. TestBug will not complete until the parallel test completes. But TestBug's body has to finish because that is what triggers running the parallel tests in the first place.
In this code:
The group has one parallel subtest, asdf. Once the group's body returns, the parallel subtests are allowed to run in parallel (there's only one so that's not saying much), and once the parallel subtest finishes, t.Run("group"...) returns.