Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cron/jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func DefaultJitterConfig() JitterConfig {
// The returned duration is in [cfg.MinSec, cfg.MaxSec]. Minute-mark avoidance
// is applied separately at fire time via avoidMinuteMarks.
func computeJitter(cfg JitterConfig, jobID, schedule string) time.Duration {
// Sanity: if disabled or range is zero, return zero.
if !cfg.Enabled || cfg.MinSec >= cfg.MaxSec {
// Sanity: if disabled or the range is invalid (min greater than max, or negative min), return zero.
if !cfg.Enabled || cfg.MinSec > cfg.MaxSec || cfg.MinSec < 0 {
return 0
}

Expand Down
18 changes: 15 additions & 3 deletions internal/cron/jitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,27 @@ func TestComputeJitter_Disabled(t *testing.T) {
}
}

func TestComputeJitter_ZeroRange(t *testing.T) {
func TestComputeJitter_EqualBounds(t *testing.T) {
cfg := DefaultJitterConfig()
cfg.MinSec = 300
cfg.MaxSec = 300

// When MinSec >= MaxSec, should return 0 (sanity check).
// When MinSec == MaxSec, should return exactly MinSec seconds.
jitter := computeJitter(cfg, "job-1", "*/5 * * * *")
if jitter != time.Duration(300)*time.Second {
t.Fatalf("expected jitter of 300s when MinSec == MaxSec, got %v", jitter)
}
}

func TestComputeJitter_MinGreaterThanMax(t *testing.T) {
cfg := DefaultJitterConfig()
cfg.MinSec = 400
cfg.MaxSec = 300

// When MinSec > MaxSec, should return 0.
jitter := computeJitter(cfg, "job-1", "*/5 * * * *")
if jitter != 0 {
t.Fatalf("expected zero jitter when MinSec >= MaxSec, got %v", jitter)
t.Fatalf("expected zero jitter when MinSec > MaxSec, got %v", jitter)
}
}

Expand Down
Loading