Skip to content

Commit

Permalink
another month test, fix cases around month without day, feb
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Oct 22, 2023
1 parent ecf4971 commit efd5f8c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ name: "CodeQL"

on:
push:
branches: [ main ]
branches: [ v2 ]
branches-ignore: "dependabot/**"
pull_request:
paths-ignore:
- '**.md'
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [ v2 ]
schedule:
- cron: '34 7 * * 1'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/file_formatting.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
on:
push:
branches:
- main
- v2
pull_request:
branches:
- main
- v2

name: formatting
jobs:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
on:
push:
branches:
- main
- v2
pull_request:
branches:
- main
- v2

name: golangci-lint
jobs:
Expand Down
35 changes: 18 additions & 17 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func ExampleDailyJob() {
_, _ = gocron.NewScheduler()
}

func ExampleMinuteJob() {
_, _ = gocron.NewScheduler()
}

func ExampleDurationJob() {
_, _ = gocron.NewScheduler()
}
Expand All @@ -42,10 +38,6 @@ func ExampleDurationRandomJob() {
_, _ = gocron.NewScheduler()
}

func ExampleHourlyJob() {
_, _ = gocron.NewScheduler()
}

func ExampleJob_ID() {
_, _ = gocron.NewScheduler()
}
Expand All @@ -58,8 +50,22 @@ func ExampleJob_NextRun() {
_, _ = gocron.NewScheduler()
}

func ExampleMillisecondJob() {
_, _ = gocron.NewScheduler()
func ExampleMonthlyJob() {
s, _ := gocron.NewScheduler()

_, _ = s.NewJob(
gocron.MonthlyJob(
1,
gocron.NewDaysOfTheMonth(3, -5, -1),
gocron.NewAtTimes(
gocron.NewAtTime(10, 30, 0),
gocron.NewAtTime(11, 15, 0),
),
gocron.NewTask(
func() {},
),
),
)
}

func ExampleNewScheduler() {
Expand Down Expand Up @@ -89,26 +95,21 @@ func ExampleScheduler_RemoveJob() {
_, _ = gocron.NewScheduler()
}

func ExampleWithShutdownTimeout() {
func ExampleWithStopTimeout() {
_, _ = gocron.NewScheduler()
}

func ExampleScheduler_Start() {
_, _ = gocron.NewScheduler()
}

func ExampleScheduler_Stop() {
func ExampleScheduler_StopJobs() {
_, _ = gocron.NewScheduler()
}

func ExampleScheduler_Update() {
_, _ = gocron.NewScheduler()
}

func ExampleSecondJob() {
_, _ = gocron.NewScheduler()
}

func ExampleWeeklyJob() {
_, _ = gocron.NewScheduler()
}
Expand Down
12 changes: 11 additions & 1 deletion job.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,24 @@ func (m monthlyJob) next(lastRun time.Time) time.Time {
if !next.IsZero() {
return next
}
return m.nextMonthDayAtTime(firstDayNextMonth, days)
for next.IsZero() {
next = m.nextMonthDayAtTime(firstDayNextMonth, days)
firstDayNextMonth = firstDayNextMonth.AddDate(0, 1, 0)
}

return next
}

func (m monthlyJob) nextMonthDayAtTime(lastRun time.Time, days []int) time.Time {
for _, day := range days {
if day >= lastRun.Day() {
for _, at := range m.atTimes {
atDate := time.Date(lastRun.Year(), lastRun.Month(), day, at.Hour(), at.Minute(), at.Second(), lastRun.Nanosecond(), lastRun.Location())
// this check handles if we're setting a day not in the current month
// e.g. setting day 31 in Feb results in March 2nd
if atDate.Month() != lastRun.Month() {
continue
}
if atDate.After(lastRun) {
return atDate
}
Expand Down
11 changes: 11 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func TestMonthlyJob_next(t *testing.T) {
time.Date(2000, 1, 31, 5, 30, 0, 0, time.UTC),
2 * 24 * time.Hour,
},
{
"day not in current month, runs next month",
[]int{31},
nil,
[]time.Time{
time.Date(0, 0, 0, 5, 30, 0, 0, time.UTC),
},
time.Date(2000, 1, 31, 5, 30, 0, 0, time.UTC),
time.Date(2000, 3, 31, 5, 30, 0, 0, time.UTC),
29*24*time.Hour + 31*24*time.Hour,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit efd5f8c

Please sign in to comment.