-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Fix incorrect use of loop variable in parallel test #11082
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Bug fixes
Non-trivial changes
New/Existing features
Backward compatibility
|
go/vt/topo/topoproto/tablet_test.go
Outdated
@@ -118,6 +118,7 @@ func TestIsTabletsInList(t *testing.T) { | |||
} | |||
|
|||
for _, testcase := range testcases { | |||
testcase := testcase // capture range variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @renatolabs !
Can you please add a comment here as well just to be sure nobody comes along later and removes this "unnecessary" line. 🙂 Something like this:
// We create an explicit copy of the range variable for each parallel runner
// to be sure that they each run as expected. You can see more information on
// this here: https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks
// The problem is covered by govet's rangeloop check:
// https://github.com/golangci/govet/blob/master/rangeloop.go
Otherwise it LGTM. Thank you for contributing to Vitess! ❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing!
I included the comment as you suggested, except for the last two lines, as vet
does not capture these types of bugs right now (golang/go#16520).
As a side note, the dynamic race detector that ships with Go (go test -race
) is also not able to detect bugs of this type because of golang/go#35670.
This commit fixes an occurrence of a loop variable being captured in a parallel test. With very high probability, only the last test case will actually be exercised. To work around this problem, we create a local copy of the range variable before the parallel test, as advised by the Go documentation at: https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks Issue was found automatically using the `loopvarcapture` linter. Signed-off-by: Renato Costa <renato@cockroachlabs.com>
bcad024
to
9662074
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again! ❤️
This commit fixes an occurrence of a loop variable being captured in a
parallel test. With very high probability, only the last test case
will actually be exercised. To work around this problem, we create a
local copy of the range variable before the parallel test, as advised
by the Go documentation at:
https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks
Issue was found automatically using the
loopvarcapture
linter.