Skip to content

Commit 3ce4455

Browse files
committed
fix: handle negative time.durations with error
1 parent e7510e1 commit 3ce4455

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ var (
1414
ErrDailyJobZeroInterval = errors.New("gocron: DailyJob: interval must be greater than 0")
1515
ErrDailyJobMinutesSeconds = errors.New("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
1616
ErrDurationJobIntervalZero = errors.New("gocron: DurationJob: time interval is 0")
17+
ErrDurationJobIntervalNegative = errors.New("gocron: DurationJob: time interval must be greater than 0")
18+
ErrDurationRandomJobPositive = errors.New("gocron: DurationRandomJob: minimum and maximum durations must be greater than 0")
1719
ErrDurationRandomJobMinMax = errors.New("gocron: DurationRandomJob: minimum duration must be less than maximum duration")
1820
ErrEventListenerFuncNil = errors.New("gocron: eventListenerFunc must not be nil")
1921
ErrJobNotFound = errors.New("gocron: job not found")

job.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ func (d durationJobDefinition) setup(j *internalJob, _ *time.Location, _ time.Ti
225225
if d.duration == 0 {
226226
return ErrDurationJobIntervalZero
227227
}
228+
if d.duration < 0 {
229+
return ErrDurationJobIntervalNegative
230+
}
228231
j.jobSchedule = &durationJob{duration: d.duration}
229232
return nil
230233
}
@@ -248,6 +251,10 @@ func (d durationRandomJobDefinition) setup(j *internalJob, _ *time.Location, _ t
248251
return ErrDurationRandomJobMinMax
249252
}
250253

254+
if d.min <= 0 || d.max <= 0 {
255+
return ErrDurationRandomJobPositive
256+
}
257+
251258
j.jobSchedule = &durationRandomJob{
252259
min: d.min,
253260
max: d.max,

scheduler_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,12 @@ func TestScheduler_NewJobErrors(t *testing.T) {
704704
nil,
705705
ErrDurationJobIntervalZero,
706706
},
707+
{
708+
"duration job time interval is negative",
709+
DurationJob(-1 * time.Second),
710+
nil,
711+
ErrDurationJobIntervalNegative,
712+
},
707713
{
708714
"random with bad min/max",
709715
DurationRandomJob(
@@ -713,6 +719,24 @@ func TestScheduler_NewJobErrors(t *testing.T) {
713719
nil,
714720
ErrDurationRandomJobMinMax,
715721
},
722+
{
723+
"random with negative min",
724+
DurationRandomJob(
725+
-time.Second,
726+
time.Second,
727+
),
728+
nil,
729+
ErrDurationRandomJobPositive,
730+
},
731+
{
732+
"random with negative max",
733+
DurationRandomJob(
734+
-2*time.Second,
735+
-time.Second,
736+
),
737+
nil,
738+
ErrDurationRandomJobPositive,
739+
},
716740
{
717741
"daily job at times nil",
718742
DailyJob(

0 commit comments

Comments
 (0)