@@ -249,22 +249,23 @@ func Test_Runner(t *testing.T) {
249
249
},
250
250
})
251
251
252
- ctx := testutil .Context (t , testutil .WaitLong )
253
- cancelCtx , cancelFunc := context .WithCancel (ctx )
252
+ runnerCtx , runnerCancel := context .WithTimeout (context .Background (), testutil .WaitLong )
254
253
255
254
done := make (chan struct {})
256
255
logs := bytes .NewBuffer (nil )
257
256
go func () {
258
- err := runner .Run (cancelCtx , "1" , logs )
257
+ err := runner .Run (runnerCtx , "1" , logs )
259
258
logsStr := logs .String ()
260
259
t .Log ("Runner logs:\n \n " + logsStr )
261
260
require .ErrorIs (t , err , context .Canceled )
262
261
close (done )
263
262
}()
264
263
265
264
// Wait for the workspace build job to be picked up.
265
+ checkJobStartedCtx := testutil .Context (t , testutil .WaitLong )
266
+ jobCh := make (chan codersdk.ProvisionerJob , 1 )
266
267
require .Eventually (t , func () bool {
267
- workspaces , err := client .Workspaces (ctx , codersdk.WorkspaceFilter {})
268
+ workspaces , err := client .Workspaces (checkJobStartedCtx , codersdk.WorkspaceFilter {})
268
269
if err != nil {
269
270
return false
270
271
}
@@ -273,65 +274,58 @@ func Test_Runner(t *testing.T) {
273
274
}
274
275
275
276
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 )
277
278
// There should be only one build at present.
278
279
if ws .LatestBuild .Transition != codersdk .WorkspaceTransitionStart {
279
280
t .Errorf ("expected build transition %s, got %s" , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition )
280
281
return false
281
282
}
282
- return ws .LatestBuild .Job .Status == codersdk .ProvisionerJobRunning
283
- }, testutil .WaitShort , testutil .IntervalMedium )
284
283
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 )
287
290
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 )
289
298
290
299
// When we run the cleanup, it should be canceled
291
300
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 )
293
303
done = make (chan struct {})
294
304
go func () {
295
305
// This will return an error as the "delete" operation will never complete.
296
- _ = runner .Cleanup (cancelCtx , "1" , cleanupLogs )
306
+ _ = runner .Cleanup (cleanupCtx , "1" , cleanupLogs )
297
307
close (done )
298
308
}()
299
309
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 )
301
312
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 ) {
304
315
return false
305
316
}
306
317
307
- if len (workspaces .Workspaces ) == 0 {
308
- return false
309
- }
318
+ t .Logf ("provisioner job id:%s status:%s" , pj .ID , pj .Status )
310
319
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 {
316
323
return false
317
324
}
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 ()
335
329
<- done
336
330
cleanupLogsStr := cleanupLogs .String ()
337
331
require .Contains (t , cleanupLogsStr , "canceling workspace build" )
0 commit comments