@@ -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
255254done := make (chan struct {})
256255logs := bytes .NewBuffer (nil )
257256go func () {
258- err := runner .Run (cancelCtx ,"1" ,logs )
257+ err := runner .Run (runnerCtx ,"1" ,logs )
259258logsStr := logs .String ()
260259t .Log ("Runner logs:\n \n " + logsStr )
261260require .ErrorIs (t ,err ,context .Canceled )
262261close (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 )
266267require .Eventually (t ,func ()bool {
267- workspaces ,err := client .Workspaces (ctx , codersdk.WorkspaceFilter {})
268+ workspaces ,err := client .Workspaces (checkJobStartedCtx , codersdk.WorkspaceFilter {})
268269if err != nil {
269270return false
270271}
@@ -273,65 +274,58 @@ func Test_Runner(t *testing.T) {
273274}
274275
275276ws := 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.
278279if ws .LatestBuild .Transition != codersdk .WorkspaceTransitionStart {
279280t .Errorf ("expected build transition %s, got %s" ,codersdk .WorkspaceTransitionStart ,ws .LatestBuild .Transition )
280281return 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
291300cleanupLogs := bytes .NewBuffer (nil )
292- cancelCtx ,cancelFunc = context .WithCancel (ctx )
301+ // Reset ctx to avoid timeouts.
302+ cleanupCtx ,cleanupCancel := context .WithTimeout (context .Background (),testutil .WaitLong )
293303done = make (chan struct {})
294304go 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 )
297307close (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 )
301312require .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 ) {
304315return 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 {
316323return 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
336330cleanupLogsStr := cleanupLogs .String ()
337331require .Contains (t ,cleanupLogsStr ,"canceling workspace build" )