Skip to content

Commit bf7e698

Browse files
committed
Add more unit tests
1 parent 7523edf commit bf7e698

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

coderd/schedule/autostop_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,88 @@ func TestCalculateAutoStop(t *testing.T) {
401401
expectedMaxDeadline: time.Time{},
402402
errContains: "",
403403
},
404+
{
405+
// Same as AutostopCrossAutostartBorder, but just misses the autostart.
406+
name: "AutostopCrossMissAutostartBorder",
407+
// Starting at 8:45pm, with the autostart at 9am.
408+
now: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day(), 20, 30, 0, 0, chicago),
409+
templateAllowAutostop: false,
410+
templateDefaultTTL: time.Hour * 12,
411+
workspaceTTL: time.Hour * 12,
412+
// At 9am every morning
413+
wsAutostart: "CRON_TZ=America/Chicago 0 9 * * *",
414+
415+
// No quiet hours
416+
templateAutoStart: schedule.TemplateAutostartRequirement{
417+
// Just allow all days of the week
418+
DaysOfWeek: 0b01111111,
419+
},
420+
templateAutostopRequirement: schedule.TemplateAutostopRequirement{},
421+
userQuietHoursSchedule: "",
422+
423+
expectedDeadline: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day()+1, 8, 30, 0, 0, chicago),
424+
expectedMaxDeadline: time.Time{},
425+
errContains: "",
426+
},
427+
{
428+
// Same as AutostopCrossAutostartBorderMaxEarlyDeadline with max deadline to limit it.
429+
// The autostop deadline is before the autostart threshold.
430+
name: "AutostopCrossAutostartBorderMaxEarlyDeadline",
431+
// Starting at 9:45pm, with the autostart at 9am.
432+
now: pastDateNight,
433+
templateAllowAutostop: false,
434+
templateDefaultTTL: time.Hour * 12,
435+
workspaceTTL: time.Hour * 12,
436+
// At 9am every morning
437+
wsAutostart: "CRON_TZ=America/Chicago 0 9 * * *",
438+
439+
// No quiet hours
440+
templateAutoStart: schedule.TemplateAutostartRequirement{
441+
// Just allow all days of the week
442+
DaysOfWeek: 0b01111111,
443+
},
444+
templateAutostopRequirement: schedule.TemplateAutostopRequirement{
445+
// Autostop every day
446+
DaysOfWeek: 0b01111111,
447+
Weeks: 0,
448+
},
449+
// 6am quiet hours
450+
userQuietHoursSchedule: "CRON_TZ=America/Chicago 0 6 * * *",
451+
452+
expectedDeadline: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day()+1, 6, 0, 0, 0, chicago),
453+
expectedMaxDeadline: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day()+1, 6, 0, 0, 0, chicago),
454+
errContains: "",
455+
},
456+
{
457+
// Same as AutostopCrossAutostartBorder with max deadline to limit it.
458+
// The autostop deadline is after autostart threshold.
459+
// So the deadline is > 12 hours, but stops at the max deadline.
460+
name: "AutostopCrossAutostartBorderMaxDeadline",
461+
// Starting at 9:45pm, with the autostart at 9am.
462+
now: pastDateNight,
463+
templateAllowAutostop: false,
464+
templateDefaultTTL: time.Hour * 12,
465+
workspaceTTL: time.Hour * 12,
466+
// At 9am every morning
467+
wsAutostart: "CRON_TZ=America/Chicago 0 9 * * *",
468+
469+
// No quiet hours
470+
templateAutoStart: schedule.TemplateAutostartRequirement{
471+
// Just allow all days of the week
472+
DaysOfWeek: 0b01111111,
473+
},
474+
templateAutostopRequirement: schedule.TemplateAutostopRequirement{
475+
// Autostop every day
476+
DaysOfWeek: 0b01111111,
477+
Weeks: 0,
478+
},
479+
// 11am quiet hours, yea this is werid case.
480+
userQuietHoursSchedule: "CRON_TZ=America/Chicago 0 11 * * *",
481+
482+
expectedDeadline: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day()+1, 11, 0, 0, 0, chicago),
483+
expectedMaxDeadline: time.Date(pastDateNight.Year(), pastDateNight.Month(), pastDateNight.Day()+1, 11, 0, 0, 0, chicago),
484+
errContains: "",
485+
},
404486
}
405487

406488
for _, c := range cases {

0 commit comments

Comments
 (0)