mirror of https://github.com/go-co-op/gocron.git
fix: handle negative time.durations with error
This commit is contained in:
parent
e7510e13a3
commit
3ce44552d7
|
|
@ -14,6 +14,8 @@ var (
|
|||
ErrDailyJobZeroInterval = errors.New("gocron: DailyJob: interval must be greater than 0")
|
||||
ErrDailyJobMinutesSeconds = errors.New("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
|
||||
ErrDurationJobIntervalZero = errors.New("gocron: DurationJob: time interval is 0")
|
||||
ErrDurationJobIntervalNegative = errors.New("gocron: DurationJob: time interval must be greater than 0")
|
||||
ErrDurationRandomJobPositive = errors.New("gocron: DurationRandomJob: minimum and maximum durations must be greater than 0")
|
||||
ErrDurationRandomJobMinMax = errors.New("gocron: DurationRandomJob: minimum duration must be less than maximum duration")
|
||||
ErrEventListenerFuncNil = errors.New("gocron: eventListenerFunc must not be nil")
|
||||
ErrJobNotFound = errors.New("gocron: job not found")
|
||||
|
|
|
|||
7
job.go
7
job.go
|
|
@ -225,6 +225,9 @@ func (d durationJobDefinition) setup(j *internalJob, _ *time.Location, _ time.Ti
|
|||
if d.duration == 0 {
|
||||
return ErrDurationJobIntervalZero
|
||||
}
|
||||
if d.duration < 0 {
|
||||
return ErrDurationJobIntervalNegative
|
||||
}
|
||||
j.jobSchedule = &durationJob{duration: d.duration}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -248,6 +251,10 @@ func (d durationRandomJobDefinition) setup(j *internalJob, _ *time.Location, _ t
|
|||
return ErrDurationRandomJobMinMax
|
||||
}
|
||||
|
||||
if d.min <= 0 || d.max <= 0 {
|
||||
return ErrDurationRandomJobPositive
|
||||
}
|
||||
|
||||
j.jobSchedule = &durationRandomJob{
|
||||
min: d.min,
|
||||
max: d.max,
|
||||
|
|
|
|||
|
|
@ -704,6 +704,12 @@ func TestScheduler_NewJobErrors(t *testing.T) {
|
|||
nil,
|
||||
ErrDurationJobIntervalZero,
|
||||
},
|
||||
{
|
||||
"duration job time interval is negative",
|
||||
DurationJob(-1 * time.Second),
|
||||
nil,
|
||||
ErrDurationJobIntervalNegative,
|
||||
},
|
||||
{
|
||||
"random with bad min/max",
|
||||
DurationRandomJob(
|
||||
|
|
@ -713,6 +719,24 @@ func TestScheduler_NewJobErrors(t *testing.T) {
|
|||
nil,
|
||||
ErrDurationRandomJobMinMax,
|
||||
},
|
||||
{
|
||||
"random with negative min",
|
||||
DurationRandomJob(
|
||||
-time.Second,
|
||||
time.Second,
|
||||
),
|
||||
nil,
|
||||
ErrDurationRandomJobPositive,
|
||||
},
|
||||
{
|
||||
"random with negative max",
|
||||
DurationRandomJob(
|
||||
-2*time.Second,
|
||||
-time.Second,
|
||||
),
|
||||
nil,
|
||||
ErrDurationRandomJobPositive,
|
||||
},
|
||||
{
|
||||
"daily job at times nil",
|
||||
DailyJob(
|
||||
|
|
|
|||
Loading…
Reference in New Issue