Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit279c08e

Browse files
authored
chore(scaletest/createworkspaces): address context usage (#16306)
Fixescoder/internal#324We 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 jobID from the first one, and added some more logging to aid futuredebugging.
1 parentbb69054 commit279c08e

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

‎scaletest/createworkspaces/run_test.go

Lines changed: 34 additions & 40 deletions
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(chanstruct{})
256255
logs:=bytes.NewBuffer(nil)
257256
gofunc() {
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
iferr!=nil {
269270
returnfalse
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
ifws.LatestBuild.Transition!=codersdk.WorkspaceTransitionStart {
279280
t.Errorf("expected build transition %s, got %s",codersdk.WorkspaceTransitionStart,ws.LatestBuild.Transition)
280281
returnfalse
281282
}
282-
returnws.LatestBuild.Job.Status==codersdk.ProvisionerJobRunning
283-
},testutil.WaitShort,testutil.IntervalMedium)
284283

285-
cancelFunc()
286-
<-done
284+
ifws.LatestBuild.Job.Status!=codersdk.ProvisionerJobRunning {
285+
returnfalse
286+
}
287+
jobCh<-ws.LatestBuild.Job
288+
returntrue
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(chanstruct{})
294304
gofunc() {
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-
iferr!=nil {
313+
pj,err:=client.OrganizationProvisionerJob(checkJobCanceledCtx,runningJob.OrganizationID,runningJob.ID)
314+
if!assert.NoError(t,err) {
304315
returnfalse
305316
}
306317

307-
iflen(workspaces.Workspaces)==0 {
308-
returnfalse
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-
iferr!=nil {
320+
ifpj.Status!=codersdk.ProvisionerJobFailed&&
321+
pj.Status!=codersdk.ProvisionerJobCanceling&&
322+
pj.Status!=codersdk.ProvisionerJobCanceled {
316323
returnfalse
317324
}
318-
fori,build:=rangebuilds {
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-
ifbuild.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-
ifbuild.Job.Status==codersdk.ProvisionerJobFailed||
327-
build.Job.Status==codersdk.ProvisionerJobCanceling||
328-
build.Job.Status==codersdk.ProvisionerJobCanceled {
329-
returntrue
330-
}
331-
}
332-
returnfalse
333-
},testutil.WaitShort,testutil.IntervalMedium)
334-
cancelFunc()
325+
326+
returntrue
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)

[8]ページ先頭

©2009-2025 Movatter.jp