Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Oct 31, 2023
1 parent 293843f commit 6ef1d01
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 3 deletions.
1 change: 1 addition & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func ExampleScheduler_RemoveJob() {

_ = s.RemoveJob(j.ID())

time.Sleep(5 * time.Millisecond)
fmt.Println(len(s.Jobs()))
// Output:
// 1
Expand Down
45 changes: 45 additions & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gocron

import (
"log"
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -264,6 +265,50 @@ func TestMonthlyJob_next(t *testing.T) {
}
}

func TestDurationRandomJob_next(t *testing.T) {
tests := []struct {
name string
min time.Duration
max time.Duration
lastRun time.Time
expectedMin time.Time
expectedMax time.Time
}{
{
"min 1s, max 5s",
time.Second,
5 * time.Second,
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC),
time.Date(2000, 1, 1, 0, 0, 5, 0, time.UTC),
},
{
"min 100ms, max 1s",
100 * time.Millisecond,
1 * time.Second,
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2000, 1, 1, 0, 0, 0, 100000000, time.UTC),
time.Date(2000, 1, 1, 0, 0, 1, 0, time.UTC),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rj := durationRandomJob{
min: tt.min,
max: tt.max,
rand: rand.New(rand.NewSource(time.Now().UnixNano())), // nolint:gosec
}

for i := 0; i < 100; i++ {
next := rj.next(tt.lastRun)
assert.GreaterOrEqual(t, next, tt.expectedMin)
assert.LessOrEqual(t, next, tt.expectedMax)
}
})
}
}

func TestJob_LastRun(t *testing.T) {
testTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local)
fakeClock := clockwork.NewFakeClockAt(testTime)
Expand Down
10 changes: 7 additions & 3 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ func NewScheduler(options ...SchedulerOption) (Scheduler, error) {
func (s *scheduler) stopScheduler() {
s.exec.cancel()
s.started = false
for id, j := range s.jobs {
for _, j := range s.jobs {
j.stop()
}
for id, j := range s.jobs {
<-j.ctx.Done()

j.ctx, j.cancel = context.WithCancel(s.shutdownCtx)
Expand Down Expand Up @@ -319,8 +321,10 @@ func (s *scheduler) addOrUpdateJob(id uuid.UUID, definition JobDefinition, taskW
j.id = uuid.New()
} else {
currentJob := requestJobCtx(s.shutdownCtx, id, s.jobOutRequestCh)
s.removeJobCh <- id
<-currentJob.ctx.Done()
if currentJob != nil && currentJob.id != uuid.Nil {
s.removeJobCh <- id
<-currentJob.ctx.Done()
}

j.id = id
}
Expand Down
176 changes: 176 additions & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,179 @@ func TestScheduler_Shutdown(t *testing.T) {
assert.ErrorIs(t, err, ErrJobNotFound)
})
}

func TestScheduler_NewJob(t *testing.T) {
tests := []struct {
name string
jd JobDefinition
tsk Task
opts []JobOption
}{
{
"cron with timezone",
CronJob(
"CRON_TZ=America/Chicago * * * * * *",
true,
),
NewTask(
func() {},
),
nil,
},
{
"cron with timezone, no seconds",
CronJob(
"CRON_TZ=America/Chicago * * * * *",
false,
),
NewTask(
func() {},
),
nil,
},
{
"random duration",
DurationRandomJob(
time.Second,
time.Second*5,
),
NewTask(
func() {},
),
nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := NewScheduler()
require.NoError(t, err)

_, err = s.NewJob(tt.jd, tt.tsk, tt.opts...)
require.NoError(t, err)

s.Start()
require.NoError(t, s.Shutdown())
})
}
}

func TestScheduler_NewJobErrors(t *testing.T) {
tests := []struct {
name string
jd JobDefinition
err error
}{
{
"cron with timezone",
CronJob(
"bad cron",
true,
),
ErrCronJobParse,
},
{
"random with bad min/max",
DurationRandomJob(
time.Second*5,
time.Second,
),
ErrDurationRandomJobMinMax,
},
{
"daily job at times nil",
DailyJob(
1,
nil,
),
ErrDailyJobAtTimesNil,
},
{
"daily job at time nil",
DailyJob(
1,
NewAtTimes(nil),
),
ErrDailyJobAtTimeNil,
},
{
"daily job hours out of range",
DailyJob(
1,
NewAtTimes(NewAtTime(100, 0, 0)),
),
ErrDailyJobHours,
},
{
"daily job minutes out of range",
DailyJob(
1,
NewAtTimes(NewAtTime(1, 100, 0)),
),
ErrDailyJobMinutesSeconds,
},
{
"daily job seconds out of range",
DailyJob(
1,
NewAtTimes(NewAtTime(1, 0, 100)),
),
ErrDailyJobMinutesSeconds,
},
{
"weekly job at times nil",
WeeklyJob(
1,
NewWeekdays(time.Monday),
nil,
),
ErrWeeklyJobAtTimesNil,
},
{
"weekly job at time nil",
WeeklyJob(
1,
NewWeekdays(time.Monday),
NewAtTimes(nil),
),
ErrWeeklyJobAtTimeNil,
},
{
"weekly job hours out of range",
WeeklyJob(
1,
NewWeekdays(time.Monday),
NewAtTimes(NewAtTime(100, 0, 0)),
),
ErrWeeklyJobHours,
},
{
"weekly job minutes out of range",
WeeklyJob(
1,
NewWeekdays(time.Monday),
NewAtTimes(NewAtTime(1, 100, 0)),
),
ErrWeeklyJobMinutesSeconds,
},
{
"weekly job seconds out of range",
WeeklyJob(
1,
NewWeekdays(time.Monday),
NewAtTimes(NewAtTime(1, 0, 100)),
),
ErrWeeklyJobMinutesSeconds,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := NewScheduler()
require.NoError(t, err)

_, err = s.NewJob(tt.jd, NewTask(func() {}))
assert.ErrorIs(t, err, tt.err)
})
}
}

0 comments on commit 6ef1d01

Please sign in to comment.