Manager: fix sleep scheduler unit test
The unit test was using a mocked "now" in a hardcoded timezone (UTC+2), while the code under test was actually using the local timezone of the computer. Also the schedule computations are now explicitly only in local time.
This commit is contained in:
parent
01a97862db
commit
8d2dd5f355
@ -18,6 +18,9 @@ func scheduledWorkerStatus(now time.Time, sched *persistence.SleepSchedule) api.
|
|||||||
return api.WorkerStatusAwake
|
return api.WorkerStatusAwake
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function should always work with localized time.
|
||||||
|
now = now.In(time.Local)
|
||||||
|
|
||||||
tod := time_of_day.MakeTimeOfDay(now)
|
tod := time_of_day.MakeTimeOfDay(now)
|
||||||
|
|
||||||
if !sched.IsActive {
|
if !sched.IsActive {
|
||||||
@ -58,10 +61,13 @@ func cleanupDaysOfWeek(daysOfWeek string) string {
|
|||||||
|
|
||||||
// Return a timestamp when the next scheck for this schedule is due.
|
// Return a timestamp when the next scheck for this schedule is due.
|
||||||
func calculateNextCheck(now time.Time, schedule persistence.SleepSchedule) time.Time {
|
func calculateNextCheck(now time.Time, schedule persistence.SleepSchedule) time.Time {
|
||||||
|
// This function should always work with localized time.
|
||||||
|
now = now.In(time.Local)
|
||||||
|
|
||||||
// calcNext returns the given time of day on "today" if that hasn't passed
|
// calcNext returns the given time of day on "today" if that hasn't passed
|
||||||
// yet, otherwise on "tomorrow".
|
// yet, otherwise on "tomorrow".
|
||||||
calcNext := func(tod time_of_day.TimeOfDay) time.Time {
|
calcNext := func(tod time_of_day.TimeOfDay) time.Time {
|
||||||
nextCheck := tod.OnDate(now).In(time.Local)
|
nextCheck := tod.OnDate(now)
|
||||||
if nextCheck.Before(now) {
|
if nextCheck.Before(now) {
|
||||||
nextCheck = nextCheck.AddDate(0, 0, 1)
|
nextCheck = nextCheck.AddDate(0, 0, 1)
|
||||||
}
|
}
|
||||||
|
@ -280,8 +280,7 @@ func testFixtures(t *testing.T) (*SleepScheduler, TestMocks, context.Context) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
mockedClock := clock.NewMock()
|
mockedClock := clock.NewMock()
|
||||||
mockedNow, err := time.Parse(time.RFC3339, "2022-06-07T11:14:47+02:00")
|
mockedNow := time.Date(2022, 6, 7, 11, 14, 47, 0, time.Local)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
mockedClock.Set(mockedNow)
|
mockedClock.Set(mockedNow)
|
||||||
if !assert.Equal(t, time.Tuesday.String(), mockedNow.Weekday().String()) {
|
if !assert.Equal(t, time.Tuesday.String(), mockedNow.Weekday().String()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user