summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@tailscale.com>2026-04-13 22:59:44 +0000
committerBrad Fitzpatrick <brad@danga.com>2026-04-13 16:14:33 -0700
commitdbd19e4b65b94e3cc428e4e178da6fc710b97e25 (patch)
treef753f5816f693bc0f2e353d0ce137ecbc388ccdc
parent50b8cfbde2fc548a6c5b33b3c7021997ca14a0d9 (diff)
downloadtailscale-dbd19e4b65b94e3cc428e4e178da6fc710b97e25.tar.xz
tailscale-dbd19e4b65b94e3cc428e4e178da6fc710b97e25.zip
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>
-rw-r--r--tstest/tstest.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/tstest/tstest.go b/tstest/tstest.go
index 87cb46f90..7e25ce8a0 100644
--- a/tstest/tstest.go
+++ b/tstest/tstest.go
@@ -20,8 +20,22 @@ import (
"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.
// 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) {
t.Helper()
if target == nil {