fix(cli): scope context per subtest to fix flake test in prebuilt workspace delete (#18872)

## Description

This PR fixes a flaky test in
`TestDelete/Prebuilt_workspace_delete_permissions`:
https://github.com/coder/internal/issues/764

Previously, all subtests used the same context created at the top level.
Since the subtests run in parallel, they could run for too long and
cause the shared context to expire. This sometimes led to context
deadline exceeded errors, especially during the `testutil.Eventually`
check for running prebuilt workspaces.

The fix is to create a fresh context per subtest, ensuring they are
isolated and not prematurely cancelled due to other subtests' durations.
This commit is contained in:
Susana Ferreira
2025-07-15 10:21:11 +01:00
committed by GitHub
parent c643214b47
commit f1eec2d267
+3 -3
View File
@@ -233,9 +233,6 @@ func TestDelete(t *testing.T) {
t.Skip("this test requires postgres")
}
clock := quartz.NewMock(t)
ctx := testutil.Context(t, testutil.WaitSuperLong)
// Setup
db, pb := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure())
client, _ := coderdtest.NewWithProvisionerCloser(t, &coderdtest.Options{
@@ -301,6 +298,9 @@ func TestDelete(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
clock := quartz.NewMock(t)
ctx := testutil.Context(t, testutil.WaitSuperLong)
// Create one prebuilt workspace (owned by system user) and one normal workspace (owned by a user)
// Each workspace is persisted in the DB along with associated workspace jobs and builds.
dbPrebuiltWorkspace := setupTestDBWorkspace(t, clock, db, pb, orgID, database.PrebuildsSystemUserID, template.ID, version.ID, preset.ID)