proposal: testing: add a ParallelCount
func that reports the go test -p
flag value
#50719
Labels
Milestone
ParallelCount
func that reports the go test -p
flag value
#50719
Summary:
Add a helper to the
testing
package that exposes the value of thego test -p
flag. This is similar totesting.Verbose()
ortesting.Short()
functions that expose the values of other flags.Problem:
By default,
go test
runs with with the-p
flag defaulting toGOMAXPROCS
, normally the number of CPUs available. This results in tests within separate packages running in parallel, which is great, I get fast test runs.However, occasionally I am forced to write tests that are racy with concurrent tests. For example when two tests in separate packages each call
t.Setenv()
to set the same env var.The workaround is to run
go test -p=1
to force the tests to run sequentially. But that drastically slows down the tests, plus most of the time I actually want tests to run concurrently to catch unexpected races etc.Often there's just two or three tests out of hundreds within a package that need to be run sequentially. So it's not feasible to use the
-run
flag to exclude all the other tests... instead I end up with something like this in my code:which I then have to invoke in CI via something like:
The
TESTS_ARE_BEING_RUN_SERIALLY
env var is clunky, and certainly confusing if you're not familiar with the code.So it'd be great if the
testing
package had a helper similar totesting.Verbose()
ortesting.Short()
functions that exposed the value of the-p
flag:which I would use in my test like this:
Notes:
-p
flag controls.go test -p=n
andt.Parallel()
helper. Sincet.Parallel()
does nothing if-p=1
(at least that's my understanding), then for my purposes, I only care about the-p
flag setting.testing.ParallelCount()
helper, it's easy to think it's related to thet.Parallel()
helper... But I struggled to come up with a more appropriate name. Perhaps the scoping of a pkg level helper (testing
) vs a test-level helper (t
) is enough disambiguation?The text was updated successfully, but these errors were encountered: