@@ -271,15 +271,14 @@ func TestExecutorAutostopAlreadyStopped(t *testing.T) {
271
271
workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
272
272
cwr .AutostartSchedule = nil
273
273
})
274
- ttl = * workspace .TTL
275
274
)
276
275
277
276
// Given: workspace is stopped
278
277
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
279
278
280
279
// When: the autobuild executor ticks past the TTL
281
280
go func () {
282
- tickCh <- time . Now (). UTC () .Add (ttl + time .Minute )
281
+ tickCh <- workspace . LatestBuild . Deadline .Add (time .Minute )
283
282
close (tickCh )
284
283
}()
285
284
@@ -313,7 +312,7 @@ func TestExecutorAutostopNotEnabled(t *testing.T) {
313
312
314
313
// When: the autobuild executor ticks past the TTL
315
314
go func () {
316
- tickCh <- time . Now (). UTC () .Add (time .Minute )
315
+ tickCh <- workspace . LatestBuild . Deadline .Add (time .Minute )
317
316
close (tickCh )
318
317
}()
319
318
@@ -401,7 +400,7 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
401
400
require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
402
401
}
403
402
404
- func TestExecutorWorkspaceTTLTooEarly (t * testing.T ) {
403
+ func TestExecutorWorkspaceAutostopBeforeDeadline (t * testing.T ) {
405
404
t .Parallel ()
406
405
407
406
var (
@@ -416,7 +415,7 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
416
415
417
416
// When: the autobuild executor ticks before the TTL
418
417
go func () {
419
- tickCh <- time . Now (). UTC ( )
418
+ tickCh <- workspace . LatestBuild . Deadline . Add ( - 1 * time . Minute )
420
419
close (tickCh )
421
420
}()
422
421
@@ -427,6 +426,38 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
427
426
require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
428
427
}
429
428
429
+ func TestExecutorWorkspaceAutostopNoWaitChangedMyMind (t * testing.T ) {
430
+ t .Parallel ()
431
+
432
+ var (
433
+ ctx = context .Background ()
434
+ tickCh = make (chan time.Time )
435
+ client = coderdtest .New (t , & coderdtest.Options {
436
+ AutobuildTicker : tickCh ,
437
+ IncludeProvisionerD : true ,
438
+ })
439
+ // Given: we have a user with a workspace
440
+ workspace = mustProvisionWorkspace (t , client )
441
+ )
442
+
443
+ // Given: the user changes their mind and decides their workspace should not auto-stop
444
+ err := client .UpdateWorkspaceTTL (ctx , workspace .ID , codersdk.UpdateWorkspaceTTLRequest {TTL : nil })
445
+ require .NoError (t , err )
446
+
447
+ // When: the autobuild executor ticks after the deadline
448
+ go func () {
449
+ tickCh <- workspace .LatestBuild .Deadline .Add (time .Minute )
450
+ close (tickCh )
451
+ }()
452
+
453
+ // Then: the workspace should still stop - sorry!
454
+ <- time .After (5 * time .Second )
455
+ ws := mustWorkspace (t , client , workspace .ID )
456
+ require .NotEqual (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected a workspace build to occur" )
457
+ require .Equal (t , codersdk .ProvisionerJobSucceeded , ws .LatestBuild .Job .Status , "expected provisioner job to have succeeded" )
458
+ require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
459
+ }
460
+
430
461
func TestExecutorAutostartMultipleOK (t * testing.T ) {
431
462
if os .Getenv ("DB" ) == "" {
432
463
t .Skip (`This test only really works when using a "real" database, similar to a HA setup` )
0 commit comments