From 6d36ddaa8545d2764f118da5e5f40b87a11b16a2 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Fri, 23 Aug 2024 14:15:11 +0100 Subject: [PATCH 1/2] feat(testutil): add testutil.IsParallel() --- coderd/coderdtest/coderdtest.go | 4 ++-- testutil/parallel.go | 17 +++++++++++++++++ testutil/parallel_test.go | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 testutil/parallel.go create mode 100644 testutil/parallel_test.go diff --git a/coderd/coderdtest/coderdtest.go b/coderd/coderdtest/coderdtest.go index 4e02c4c4178d3..0e3d859f86b4c 100644 --- a/coderd/coderdtest/coderdtest.go +++ b/coderd/coderdtest/coderdtest.go @@ -269,8 +269,8 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can options.DeploymentValues = DeploymentValues(t) } // This value is not safe to run in parallel. - if options.DeploymentValues.DisableOwnerWorkspaceExec { - t.Logf("WARNING: DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!") + if options.DeploymentValues.DisableOwnerWorkspaceExec.Value() && testutil.IsParallel(t) { + t.Fatal("DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!") } // If no ratelimits are set, disable all rate limiting for tests. diff --git a/testutil/parallel.go b/testutil/parallel.go new file mode 100644 index 0000000000000..3fb6cae467551 --- /dev/null +++ b/testutil/parallel.go @@ -0,0 +1,17 @@ +package testutil + +import "testing" + +// IsParallel determines whether the current test is running with parallel set. +// The Go standard library does not currently expose this. However, we can easily +// determine this using the fact that a call to t.Setenv() after t.Parallel() will panic. +func IsParallel(t testing.TB) (parallel bool) { + t.Helper() + defer func() { + if r := recover(); r != nil { + parallel = true + } + }() + t.Setenv(t.Name()+"_PARALLEL", "") + return parallel +} diff --git a/testutil/parallel_test.go b/testutil/parallel_test.go new file mode 100644 index 0000000000000..f37ddcccffc9a --- /dev/null +++ b/testutil/parallel_test.go @@ -0,0 +1,19 @@ +package testutil_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/testutil" +) + +// nolint:paralleltest // this is the whole point +func Test_IsParallel_False(t *testing.T) { + require.False(t, testutil.IsParallel(t)) +} + +func Test_IsParallel_True(t *testing.T) { + t.Parallel() + require.True(t, testutil.IsParallel(t)) +} From 5f9dbfef2cbce8517bcaf2b11445b9ca7044e8a1 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Fri, 23 Aug 2024 15:16:15 +0100 Subject: [PATCH 2/2] revert change to coderdtest, see #14417 for correct fix --- coderd/coderdtest/coderdtest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/coderdtest/coderdtest.go b/coderd/coderdtest/coderdtest.go index 0e3d859f86b4c..fe1e6bf8a26a2 100644 --- a/coderd/coderdtest/coderdtest.go +++ b/coderd/coderdtest/coderdtest.go @@ -270,7 +270,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can } // This value is not safe to run in parallel. if options.DeploymentValues.DisableOwnerWorkspaceExec.Value() && testutil.IsParallel(t) { - t.Fatal("DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!") + t.Logf("WARNING: DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!") } // If no ratelimits are set, disable all rate limiting for tests.