Skip to content

Commit 4d1eebf

Browse files
johnstcnaslilac
authored andcommitted
chore(scaletest/createworkspaces): address context usage (#16306)
Fixes coder/internal#324 We had been using a `testutil.Context` in combination with a separate `context.WithTimeout()` that smelled iffy to me. Also reworked part of the second `require.Eventually` loop to pull a job ID from the first one, and added some more logging to aid future debugging.
1 parent 832f949 commit 4d1eebf

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

scaletest/createworkspaces/run_test.go

+34-40
Original file line numberDiff line numberDiff line change
@@ -249,22 +249,23 @@ func Test_Runner(t *testing.T) {
249249
},
250250
})
251251

252-
ctx := testutil.Context(t, testutil.WaitLong)
253-
cancelCtx, cancelFunc := context.WithCancel(ctx)
252+
runnerCtx, runnerCancel := context.WithTimeout(context.Background(), testutil.WaitLong)
254253

255254
done := make(chan struct{})
256255
logs := bytes.NewBuffer(nil)
257256
go func() {
258-
err := runner.Run(cancelCtx, "1", logs)
257+
err := runner.Run(runnerCtx, "1", logs)
259258
logsStr := logs.String()
260259
t.Log("Runner logs:\n\n" + logsStr)
261260
require.ErrorIs(t, err, context.Canceled)
262261
close(done)
263262
}()
264263

265264
// Wait for the workspace build job to be picked up.
265+
checkJobStartedCtx := testutil.Context(t, testutil.WaitLong)
266+
jobCh := make(chan codersdk.ProvisionerJob, 1)
266267
require.Eventually(t, func() bool {
267-
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
268+
workspaces, err := client.Workspaces(checkJobStartedCtx, codersdk.WorkspaceFilter{})
268269
if err != nil {
269270
return false
270271
}
@@ -273,65 +274,58 @@ func Test_Runner(t *testing.T) {
273274
}
274275

275276
ws := workspaces.Workspaces[0]
276-
t.Logf("checking build: %s | %s", ws.LatestBuild.Transition, ws.LatestBuild.Job.Status)
277+
t.Logf("checking build: %s | %s | %s", ws.ID, ws.LatestBuild.Transition, ws.LatestBuild.Job.Status)
277278
// There should be only one build at present.
278279
if ws.LatestBuild.Transition != codersdk.WorkspaceTransitionStart {
279280
t.Errorf("expected build transition %s, got %s", codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition)
280281
return false
281282
}
282-
return ws.LatestBuild.Job.Status == codersdk.ProvisionerJobRunning
283-
}, testutil.WaitShort, testutil.IntervalMedium)
284283

285-
cancelFunc()
286-
<-done
284+
if ws.LatestBuild.Job.Status != codersdk.ProvisionerJobRunning {
285+
return false
286+
}
287+
jobCh <- ws.LatestBuild.Job
288+
return true
289+
}, testutil.WaitLong, testutil.IntervalSlow)
287290

288-
ctx = testutil.Context(t, testutil.WaitLong) // Reset ctx to avoid timeouts.
291+
t.Log("canceling scaletest workspace creation")
292+
runnerCancel()
293+
<-done
294+
t.Log("canceled scaletest workspace creation")
295+
// Ensure we have a job to interrogate
296+
runningJob := testutil.RequireRecvCtx(testutil.Context(t, testutil.WaitShort), t, jobCh)
297+
require.NotZero(t, runningJob.ID)
289298

290299
// When we run the cleanup, it should be canceled
291300
cleanupLogs := bytes.NewBuffer(nil)
292-
cancelCtx, cancelFunc = context.WithCancel(ctx)
301+
// Reset ctx to avoid timeouts.
302+
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), testutil.WaitLong)
293303
done = make(chan struct{})
294304
go func() {
295305
// This will return an error as the "delete" operation will never complete.
296-
_ = runner.Cleanup(cancelCtx, "1", cleanupLogs)
306+
_ = runner.Cleanup(cleanupCtx, "1", cleanupLogs)
297307
close(done)
298308
}()
299309

300-
// Ensure the job has been marked as deleted
310+
// Ensure the job has been marked as canceled
311+
checkJobCanceledCtx := testutil.Context(t, testutil.WaitLong)
301312
require.Eventually(t, func() bool {
302-
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
303-
if err != nil {
313+
pj, err := client.OrganizationProvisionerJob(checkJobCanceledCtx, runningJob.OrganizationID, runningJob.ID)
314+
if !assert.NoError(t, err) {
304315
return false
305316
}
306317

307-
if len(workspaces.Workspaces) == 0 {
308-
return false
309-
}
318+
t.Logf("provisioner job id:%s status:%s", pj.ID, pj.Status)
310319

311-
// There should be two builds
312-
builds, err := client.WorkspaceBuilds(ctx, codersdk.WorkspaceBuildsRequest{
313-
WorkspaceID: workspaces.Workspaces[0].ID,
314-
})
315-
if err != nil {
320+
if pj.Status != codersdk.ProvisionerJobFailed &&
321+
pj.Status != codersdk.ProvisionerJobCanceling &&
322+
pj.Status != codersdk.ProvisionerJobCanceled {
316323
return false
317324
}
318-
for i, build := range builds {
319-
t.Logf("checking build #%d: %s | %s", i, build.Transition, build.Job.Status)
320-
// One of the builds should be for creating the workspace,
321-
if build.Transition != codersdk.WorkspaceTransitionStart {
322-
continue
323-
}
324-
325-
// And it should be either failed (Echo returns an error when job is canceled), canceling, or canceled.
326-
if build.Job.Status == codersdk.ProvisionerJobFailed ||
327-
build.Job.Status == codersdk.ProvisionerJobCanceling ||
328-
build.Job.Status == codersdk.ProvisionerJobCanceled {
329-
return true
330-
}
331-
}
332-
return false
333-
}, testutil.WaitShort, testutil.IntervalMedium)
334-
cancelFunc()
325+
326+
return true
327+
}, testutil.WaitLong, testutil.IntervalSlow)
328+
cleanupCancel()
335329
<-done
336330
cleanupLogsStr := cleanupLogs.String()
337331
require.Contains(t, cleanupLogsStr, "canceling workspace build")

0 commit comments

Comments
 (0)