Skip to content

Commit 6d36dda

Browse files
committed
feat(testutil): add testutil.IsParallel()
1 parent d4adfa3 commit 6d36dda

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

coderd/coderdtest/coderdtest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
269269
options.DeploymentValues = DeploymentValues(t)
270270
}
271271
// This value is not safe to run in parallel.
272-
if options.DeploymentValues.DisableOwnerWorkspaceExec {
273-
t.Logf("WARNING: DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!")
272+
if options.DeploymentValues.DisableOwnerWorkspaceExec.Value() && testutil.IsParallel(t) {
273+
t.Fatal("DisableOwnerWorkspaceExec is set, this is not safe in parallel tests!")
274274
}
275275

276276
// If no ratelimits are set, disable all rate limiting for tests.

testutil/parallel.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package testutil
2+
3+
import "testing"
4+
5+
// IsParallel determines whether the current test is running with parallel set.
6+
// The Go standard library does not currently expose this. However, we can easily
7+
// determine this using the fact that a call to t.Setenv() after t.Parallel() will panic.
8+
func IsParallel(t testing.TB) (parallel bool) {
9+
t.Helper()
10+
defer func() {
11+
if r := recover(); r != nil {
12+
parallel = true
13+
}
14+
}()
15+
t.Setenv(t.Name()+"_PARALLEL", "")
16+
return parallel
17+
}

testutil/parallel_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testutil_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/v2/testutil"
9+
)
10+
11+
// nolint:paralleltest // this is the whole point
12+
func Test_IsParallel_False(t *testing.T) {
13+
require.False(t, testutil.IsParallel(t))
14+
}
15+
16+
func Test_IsParallel_True(t *testing.T) {
17+
t.Parallel()
18+
require.True(t, testutil.IsParallel(t))
19+
}

0 commit comments

Comments
 (0)