Commit 91203d7a authored by Kyle Clarke's avatar Kyle Clarke 💬

On implementation I found that the cap logic did no breach when expected....

On implementation I found that the cap logic did no breach  when expected. Fixed code and test to confirm.
parent 39b3c24e
Pipeline #4214748 passed with stage
in 28 seconds
......@@ -80,9 +80,9 @@ func (r *runner) CanBegin() bool {
return r.job.Begins.IsZero() || r.job.Begins.Before(time.Now())
}
// CapBreached will return true if this Job has a Cap and it is over the running count.
// CapBreached will return true if this Job has a Cap and it is over or equal to the running count.
func (r *runner) CapBreached() bool {
return (r.job.Cap != 0 && r.runningCount > r.job.Cap)
return (r.job.Cap != 0 && r.runningCount >= r.job.Cap)
}
// Expired will return true if an expiration time is set and is before now.
......@@ -146,14 +146,15 @@ func do(r *runner) {
r.started = time.Now().UTC()
}
// Increment the safety and running counts.
safety++
r.runningCount++
// Assign the running status
r.status = StatusRunning
// Make the call...
r.callFn.Call(r.callFnParams)
// Increment the safety and running counts.
safety++
r.runningCount++
if safety == math.MaxUint64 {
r.status = StatusError
r.err = ErrJobMaximumTickBreach
......
......@@ -56,6 +56,7 @@ func TestRunner_CapBreached(t *testing.T) {
Reset()
j := newSimpleJob()
j.Cap = 0
j.Every = time.Duration(1 * time.Millisecond)
s := DefaultSchedule()
s.Add(j)
r, _ := s.FindByJobID(j.ID)
......@@ -68,16 +69,23 @@ func TestRunner_CapBreached(t *testing.T) {
for i := 0; i < 5; i++ {
switch true {
case 0 == r.Info().RunningCount:
case 1 == r.Info().RunningCount:
if r.CapBreached() {
t.Error("Cap should not be breached with 0 or 1 running count.")
}
case 1 == r.Info().RunningCount:
case 1 < r.Info().RunningCount:
t.Error("Cap should have breached on execution!")
if !r.CapBreached() {
t.Error("CapBreached() method should return true.")
}
default:
t.Error("Unknown case.")
}
r.Run()
time.Sleep(10 * time.Millisecond)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment