fix: limit validation for WithLimitedRuns

This commit is contained in:
Maksim Osipov 2025-11-05 21:46:57 +03:00
parent 4278ff74b4
commit fe46434d6f
3 changed files with 12 additions and 0 deletions

View File

@ -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

3
job.go
View File

@ -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,

View File

@ -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 {