From fe46434d6fd8e1321eb5ea965a38abf2b62a4528 Mon Sep 17 00:00:00 2001 From: Maksim Osipov Date: Wed, 5 Nov 2025 21:46:57 +0300 Subject: [PATCH] fix: limit validation for WithLimitedRuns --- errors.go | 1 + job.go | 3 +++ scheduler_test.go | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/errors.go b/errors.go index f7fbc9a..36f7455 100644 --- a/errors.go +++ b/errors.go @@ -59,6 +59,7 @@ var ( ErrStartTimeLaterThanEndTime = errors.New("gocron: WithStartDateTime: start must not be later than end") ErrStopTimeEarlierThanStartTime = errors.New("gocron: WithStopDateTime: end must not be earlier than start") ErrWithStopTimeoutZeroOrNegative = errors.New("gocron: WithStopTimeout: timeout must be greater than 0") + ErrWithLimitedRunsZero = errors.New("gocron: WithLimitedRuns: limit must be greater than 0") ) // internal errors diff --git a/job.go b/job.go index b89f10d..8bdb3c6 100644 --- a/job.go +++ b/job.go @@ -643,6 +643,9 @@ func WithEventListeners(eventListeners ...EventListener) JobOption { // Upon reaching the limit, the job is removed from the scheduler. func WithLimitedRuns(limit uint) JobOption { return func(j *internalJob, _ time.Time) error { + if limit == 0 { + return ErrWithLimitedRunsZero + } j.limitRunsTo = &limitRunsTo{ limit: limit, runCount: 0, diff --git a/scheduler_test.go b/scheduler_test.go index f6b5240..1ae7431 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1056,6 +1056,14 @@ func TestScheduler_NewJobErrors(t *testing.T) { []JobOption{WithIdentifier(uuid.Nil)}, ErrWithIdentifierNil, }, + { + "WithLimitedRuns is zero", + DurationJob( + time.Second, + ), + []JobOption{WithLimitedRuns(0)}, + ErrWithLimitedRunsZero, + }, } for _, tt := range tests {