Skip to content

Commit

Permalink
daily job tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Oct 29, 2023
1 parent d5b3c26 commit 56fdadf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
3 changes: 2 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,8 @@ func (d dailyJob) next(lastRun time.Time) time.Time {
if !next.IsZero() {
return next
}
return d.nextDay(lastRun.AddDate(0, 0, int(d.interval)))
startNextDay := time.Date(lastRun.Year(), lastRun.Month(), lastRun.Day()+int(d.interval), 0, 0, 0, lastRun.Nanosecond(), lastRun.Location())
return d.nextDay(startNextDay)
}

func (d dailyJob) nextDay(lastRun time.Time) time.Time {
Expand Down
65 changes: 57 additions & 8 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,53 @@ import (
//func TestDurationJob_next(t *testing.T) {
//}

func TestDailyJob_next(t *testing.T) {
tests := []struct {
name string
interval uint
atTimes []time.Time
lastRun time.Time
expectedNextRun time.Time
expectedDurationToNextRun time.Duration
}{
{
"daily multiple at times",
1,
[]time.Time{
time.Date(0, 0, 0, 5, 30, 0, 0, time.UTC),
time.Date(0, 0, 0, 12, 30, 0, 0, time.UTC),
},
time.Date(2000, 1, 1, 5, 30, 0, 0, time.UTC),
time.Date(2000, 1, 1, 12, 30, 0, 0, time.UTC),
7 * time.Hour,
},
{
"every 2 days multiple at times",
2,
[]time.Time{
time.Date(0, 0, 0, 5, 30, 0, 0, time.UTC),
time.Date(0, 0, 0, 12, 30, 0, 0, time.UTC),
},
time.Date(2000, 1, 1, 12, 30, 0, 0, time.UTC),
time.Date(2000, 1, 3, 5, 30, 0, 0, time.UTC),
41 * time.Hour,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := dailyJob{
interval: tt.interval,
atTimes: tt.atTimes,
}

next := d.next(tt.lastRun)
assert.Equal(t, tt.expectedNextRun, next)
assert.Equal(t, tt.expectedDurationToNextRun, next.Sub(tt.lastRun))
})
}
}

func TestWeeklyJob_next(t *testing.T) {
tests := []struct {
name string
Expand All @@ -35,15 +82,17 @@ func TestWeeklyJob_next(t *testing.T) {
}

for _, tt := range tests {
w := weeklyJob{
interval: tt.interval,
daysOfWeek: tt.daysOfWeek,
atTimes: tt.atTimes,
}
t.Run(tt.name, func(t *testing.T) {
w := weeklyJob{
interval: tt.interval,
daysOfWeek: tt.daysOfWeek,
atTimes: tt.atTimes,
}

next := w.next(tt.lastRun)
assert.Equal(t, tt.expectedNextRun, next)
assert.Equal(t, tt.expectedDurationToNextRun, next.Sub(tt.lastRun))
next := w.next(tt.lastRun)
assert.Equal(t, tt.expectedNextRun, next)
assert.Equal(t, tt.expectedDurationToNextRun, next.Sub(tt.lastRun))
})
}
}

Expand Down

0 comments on commit 56fdadf

Please sign in to comment.