From c35b560c877972d922e5b6123ac63f0ec39184af Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Mon, 18 Dec 2023 10:55:46 -0600 Subject: [PATCH] chore: fix flake, use time closer to actual test (#11240) * chore: fix flake, use time closer to actual test The tests were queued, and the autostart time was being set to the time the table was created, not when the test was actually being run. This diff was causing failures in CI --- coderd/agentapi/activitybump_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/coderd/agentapi/activitybump_test.go b/coderd/agentapi/activitybump_test.go index ceadb3e331..340be4b61d 100644 --- a/coderd/agentapi/activitybump_test.go +++ b/coderd/agentapi/activitybump_test.go @@ -43,7 +43,9 @@ func Test_ActivityBumpWorkspace(t *testing.T) { templateTTL time.Duration templateDisallowsUserAutostop bool expectedBump time.Duration - nextAutostart time.Time + // If the tests get queued, we need to be able to set the next autostart + // based on the actual time the unit test is running. + nextAutostart func(now time.Time) time.Time }{ { name: "NotFinishedYet", @@ -93,7 +95,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) { buildDeadlineOffset: ptr.Ref(-30 * time.Minute), workspaceTTL: 8 * time.Hour, expectedBump: 8*time.Hour + 30*time.Minute, - nextAutostart: time.Now().Add(time.Minute * 30), + nextAutostart: func(now time.Time) time.Time { return now.Add(time.Minute * 30) }, }, { name: "MaxDeadline", @@ -127,13 +129,13 @@ func Test_ActivityBumpWorkspace(t *testing.T) { // by the template TTL instead. name: "TemplateDisallowsUserAutostop", transition: database.WorkspaceTransitionStart, - jobCompletedAt: sql.NullTime{Valid: true, Time: dbtime.Now().Add(-7 * time.Hour)}, + jobCompletedAt: sql.NullTime{Valid: true, Time: dbtime.Now().Add(-3 * time.Hour)}, buildDeadlineOffset: ptr.Ref(-30 * time.Minute), workspaceTTL: 2 * time.Hour, templateTTL: 10 * time.Hour, templateDisallowsUserAutostop: true, expectedBump: 10*time.Hour + (time.Minute * 30), - nextAutostart: time.Now().Add(time.Minute * 30), + nextAutostart: func(now time.Time) time.Time { return now.Add(time.Minute * 30) }, }, } { tt := tt @@ -141,6 +143,10 @@ func Test_ActivityBumpWorkspace(t *testing.T) { tz := tz t.Run(tt.name+"/"+tz, func(t *testing.T) { t.Parallel() + nextAutostart := tt.nextAutostart + if tt.nextAutostart == nil { + nextAutostart = func(now time.Time) time.Time { return time.Time{} } + } var ( now = dbtime.Now() @@ -237,7 +243,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) { // Bump duration is measured from the time of the bump, so we measure from here. start := dbtime.Now() - agentapi.ActivityBumpWorkspace(ctx, log, db, bld.WorkspaceID, tt.nextAutostart) + agentapi.ActivityBumpWorkspace(ctx, log, db, bld.WorkspaceID, nextAutostart(start)) end := dbtime.Now() // Validate our state after bump