Skip to content

Commit 46660bb

Browse files
OsipovMaxMaksim OsipovJohnRoesler
authored
fix: limit validation for WithLimitedRuns (#893)
Co-authored-by: Maksim Osipov <[email protected]> Co-authored-by: John Roesler <[email protected]>
1 parent 291adb8 commit 46660bb

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var (
5959
ErrStartTimeLaterThanEndTime = errors.New("gocron: WithStartDateTime: start must not be later than end")
6060
ErrStopTimeEarlierThanStartTime = errors.New("gocron: WithStopDateTime: end must not be earlier than start")
6161
ErrWithStopTimeoutZeroOrNegative = errors.New("gocron: WithStopTimeout: timeout must be greater than 0")
62+
ErrWithLimitedRunsZero = errors.New("gocron: WithLimitedRuns: limit must be greater than 0")
6263
)
6364

6465
// internal errors

job.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ func WithEventListeners(eventListeners ...EventListener) JobOption {
643643
// Upon reaching the limit, the job is removed from the scheduler.
644644
func WithLimitedRuns(limit uint) JobOption {
645645
return func(j *internalJob, _ time.Time) error {
646+
if limit == 0 {
647+
return ErrWithLimitedRunsZero
648+
}
646649
j.limitRunsTo = &limitRunsTo{
647650
limit: limit,
648651
runCount: 0,

scheduler_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,14 @@ func TestScheduler_NewJobErrors(t *testing.T) {
10561056
[]JobOption{WithIdentifier(uuid.Nil)},
10571057
ErrWithIdentifierNil,
10581058
},
1059+
{
1060+
"WithLimitedRuns is zero",
1061+
DurationJob(
1062+
time.Second,
1063+
),
1064+
[]JobOption{WithLimitedRuns(0)},
1065+
ErrWithLimitedRunsZero,
1066+
},
10591067
}
10601068

10611069
for _, tt := range tests {

0 commit comments

Comments
 (0)