Skip to content

Commit c35b560

Browse files
authored
chore: fix flake, use time closer to actual test (coder#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
1 parent 213b768 commit c35b560

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

coderd/agentapi/activitybump_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
4343
templateTTL time.Duration
4444
templateDisallowsUserAutostop bool
4545
expectedBump time.Duration
46-
nextAutostart time.Time
46+
// If the tests get queued, we need to be able to set the next autostart
47+
// based on the actual time the unit test is running.
48+
nextAutostart func(now time.Time) time.Time
4749
}{
4850
{
4951
name: "NotFinishedYet",
@@ -93,7 +95,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
9395
buildDeadlineOffset: ptr.Ref(-30 * time.Minute),
9496
workspaceTTL: 8 * time.Hour,
9597
expectedBump: 8*time.Hour + 30*time.Minute,
96-
nextAutostart: time.Now().Add(time.Minute * 30),
98+
nextAutostart: func(now time.Time) time.Time { return now.Add(time.Minute * 30) },
9799
},
98100
{
99101
name: "MaxDeadline",
@@ -127,20 +129,24 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
127129
// by the template TTL instead.
128130
name: "TemplateDisallowsUserAutostop",
129131
transition: database.WorkspaceTransitionStart,
130-
jobCompletedAt: sql.NullTime{Valid: true, Time: dbtime.Now().Add(-7 * time.Hour)},
132+
jobCompletedAt: sql.NullTime{Valid: true, Time: dbtime.Now().Add(-3 * time.Hour)},
131133
buildDeadlineOffset: ptr.Ref(-30 * time.Minute),
132134
workspaceTTL: 2 * time.Hour,
133135
templateTTL: 10 * time.Hour,
134136
templateDisallowsUserAutostop: true,
135137
expectedBump: 10*time.Hour + (time.Minute * 30),
136-
nextAutostart: time.Now().Add(time.Minute * 30),
138+
nextAutostart: func(now time.Time) time.Time { return now.Add(time.Minute * 30) },
137139
},
138140
} {
139141
tt := tt
140142
for _, tz := range timezones {
141143
tz := tz
142144
t.Run(tt.name+"/"+tz, func(t *testing.T) {
143145
t.Parallel()
146+
nextAutostart := tt.nextAutostart
147+
if tt.nextAutostart == nil {
148+
nextAutostart = func(now time.Time) time.Time { return time.Time{} }
149+
}
144150

145151
var (
146152
now = dbtime.Now()
@@ -237,7 +243,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
237243

238244
// Bump duration is measured from the time of the bump, so we measure from here.
239245
start := dbtime.Now()
240-
agentapi.ActivityBumpWorkspace(ctx, log, db, bld.WorkspaceID, tt.nextAutostart)
246+
agentapi.ActivityBumpWorkspace(ctx, log, db, bld.WorkspaceID, nextAutostart(start))
241247
end := dbtime.Now()
242248

243249
// Validate our state after bump

0 commit comments

Comments
 (0)