Skip to content

Commit 270af35

Browse files
committed
fix: autostart: ensure we handle DST changes properly
- add unit tests to and from DST - ensure expected interval between autostarts
1 parent 9854f9b commit 270af35

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

coderd/workspaces_test.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,36 +186,54 @@ func TestWorkspaceBuildByName(t *testing.T) {
186186
}
187187

188188
func TestWorkspaceUpdateAutostart(t *testing.T) {
189-
// TODO(cian): CST -> CDT
190-
// TODO(cian): CDT -> CST
191-
192189
var dublinLoc = mustLocation(t, "Europe/Dublin")
193190

194191
testCases := []struct {
195-
name string
196-
schedule string
197-
expectedError string
198-
at time.Time
199-
expectedNext time.Time
192+
name string
193+
schedule string
194+
expectedError string
195+
at time.Time
196+
expectedNext time.Time
197+
expectedInterval time.Duration
200198
}{
201199
{
202200
name: "disable autostart",
203201
schedule: "",
204202
expectedError: "",
205203
},
206204
{
207-
name: "friday to monday",
208-
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
209-
expectedError: "",
210-
at: time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc),
211-
expectedNext: time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc),
205+
name: "friday to monday",
206+
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
207+
expectedError: "",
208+
at: time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc),
209+
expectedNext: time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc),
210+
expectedInterval: 71*time.Hour + 59*time.Minute,
212211
},
213212
{
214-
name: "monday to tuesday",
215-
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
216-
expectedError: "",
217-
at: time.Date(2022, 5, 9, 9, 31, 0, 0, dublinLoc),
218-
expectedNext: time.Date(2022, 5, 10, 9, 30, 0, 0, dublinLoc),
213+
name: "monday to tuesday",
214+
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
215+
expectedError: "",
216+
at: time.Date(2022, 5, 9, 9, 31, 0, 0, dublinLoc),
217+
expectedNext: time.Date(2022, 5, 10, 9, 30, 0, 0, dublinLoc),
218+
expectedInterval: 23*time.Hour + 59*time.Minute,
219+
},
220+
{
221+
// DST in Ireland began on Mar 27 in 2022 at 0100. Forward 1 hour.
222+
name: "DST start",
223+
schedule: "CRON_TZ=Europe/Dublin 30 9 *",
224+
expectedError: "",
225+
at: time.Date(2022, 3, 26, 9, 31, 0, 0, dublinLoc),
226+
expectedNext: time.Date(2022, 3, 27, 9, 30, 0, 0, dublinLoc),
227+
expectedInterval: 22*time.Hour + 59*time.Minute,
228+
},
229+
{
230+
// DST in Ireland ends on Oct 30 in 2022 at 0200. Back 1 hour.
231+
name: "DST end",
232+
schedule: "CRON_TZ=Europe/Dublin 30 9 *",
233+
expectedError: "",
234+
at: time.Date(2022, 10, 29, 9, 31, 0, 0, dublinLoc),
235+
expectedNext: time.Date(2022, 10, 30, 9, 30, 0, 0, dublinLoc),
236+
expectedInterval: 24*time.Hour + 59*time.Minute,
219237
},
220238
{
221239
name: "invalid location",
@@ -270,6 +288,8 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
270288

271289
next := sched.Next(testCase.at)
272290
require.Equal(t, testCase.expectedNext, next, "unexpected next scheduled autostart time")
291+
interval := next.Sub(testCase.at)
292+
require.Equal(t, testCase.expectedInterval, interval, "unexpected interval")
273293
})
274294
}
275295
}

0 commit comments

Comments
 (0)