tstest: add AssertNotParallel helper
For tests to loudly declare (and panic on violation) when they're doing something that's not safe in a parallel test. Fixes #19385 Change-Id: If79693b0c235c146871a05ed74fa9ea75bb500f9 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
50b8cfbde2
commit
dbd19e4b65
@@ -20,8 +20,22 @@ import (
|
|||||||
"tailscale.com/util/cibuild"
|
"tailscale.com/util/cibuild"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AssertNotParallel asserts that t has not been marked as parallel.
|
||||||
|
// It panics (via t.Setenv) if t.Parallel has already been called.
|
||||||
|
//
|
||||||
|
// Use this when a test modifies package-level globals or other shared
|
||||||
|
// state that would be unsafe to modify concurrently with other tests.
|
||||||
|
func AssertNotParallel(t testing.TB) {
|
||||||
|
t.Helper()
|
||||||
|
t.Setenv("ASSERT_NOT_PARALLEL_TEST", "1") // panics if t.Parallel was called
|
||||||
|
}
|
||||||
|
|
||||||
// Replace replaces the value of target with val.
|
// Replace replaces the value of target with val.
|
||||||
// The old value is restored when the test ends.
|
// The old value is restored when the test ends.
|
||||||
|
//
|
||||||
|
// When target is a package-level variable, the caller should also call
|
||||||
|
// [AssertNotParallel] to ensure the test is not running in parallel with
|
||||||
|
// other tests that may access the same variable.
|
||||||
func Replace[T any](t testing.TB, target *T, val T) {
|
func Replace[T any](t testing.TB, target *T, val T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if target == nil {
|
if target == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user