Skip to content

Commit 5db2d84

Browse files
committed
chore(scaletest/createworkspaces): address context usage
1 parent b44ae40 commit 5db2d84

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

scaletest/createworkspaces/run_test.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func Test_Runner(t *testing.T) {
263263
}()
264264

265265
// Wait for the workspace build job to be picked up.
266+
jobCh := make(chan codersdk.ProvisionerJob, 1)
266267
require.Eventually(t, func() bool {
267268
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
268269
if err != nil {
@@ -273,64 +274,56 @@ 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

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)
290+
291+
t.Log("canceling scaletest workspace creation")
285292
cancelFunc()
286293
<-done
287-
288-
ctx = testutil.Context(t, testutil.WaitLong) // Reset ctx to avoid timeouts.
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+
cancelCtx, cancelFunc = 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.
296306
_ = runner.Cleanup(cancelCtx, "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
301311
require.Eventually(t, func() bool {
302-
workspaces, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
303-
if err != nil {
312+
pj, err := client.OrganizationProvisionerJob(ctx, runningJob.OrganizationID, runningJob.ID)
313+
if !assert.NoError(t, err) {
304314
return false
305315
}
306316

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

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 {
319+
if pj.Status != codersdk.ProvisionerJobFailed &&
320+
pj.Status != codersdk.ProvisionerJobCanceling &&
321+
pj.Status != codersdk.ProvisionerJobCanceled {
316322
return false
317323
}
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)
324+
325+
return true
326+
}, testutil.WaitLong, testutil.IntervalSlow)
334327
cancelFunc()
335328
<-done
336329
cleanupLogsStr := cleanupLogs.String()

0 commit comments

Comments
 (0)