Skip to content

Commit

Permalink
cron: fix: removing a job causes the next scheduled job to run too late
Browse files Browse the repository at this point in the history
Add a test to detect it and fix the bug.

Fixes issue robfig#206
  • Loading branch information
Rob Figueiredo committed Jul 11, 2019
1 parent e4fa405 commit 1cba5e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func (c *Cron) run() {

case id := <-c.remove:
timer.Stop()
now = c.now()
c.removeEntry(id)
c.logVerbosef("removed entry %d", id)
}
Expand Down
50 changes: 50 additions & 0 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,56 @@ func TestJob(t *testing.T) {
}
}

// Issue #206
// Ensure that the next run of a job after removing an entry is accurate.
func TestScheduleAfterRemoval(t *testing.T) {
var wg1 sync.WaitGroup
var wg2 sync.WaitGroup
wg1.Add(1)
wg2.Add(1)

// The first time this job is run, set a timer and remove the other job
// 750ms later. Correct behavior would be to still run the job again in
// 250ms, but the bug would cause it to run instead 1s later.

var calls int
var mu sync.Mutex

cron := newWithSeconds()
hourJob := cron.Schedule(Every(time.Hour), FuncJob(func() {}))
cron.Schedule(Every(time.Second), FuncJob(func() {
mu.Lock()
defer mu.Unlock()
switch calls {
case 0:
wg1.Done()
calls++
case 1:
time.Sleep(750 * time.Millisecond)
cron.Remove(hourJob)
calls++
case 2:
calls++
wg2.Done()
case 3:
panic("unexpected 3rd call")
}
}))

cron.Start()
defer cron.Stop()

// the first run might be any length of time 0 - 1s, since the schedule
// rounds to the second. wait for the first run to true up.
wg1.Wait()

select {
case <-time.After(2 * OneSecond):
t.Error("expected job fires 2 times")
case <-wait(&wg2):
}
}

type ZeroSchedule struct{}

func (*ZeroSchedule) Next(time.Time) time.Time {
Expand Down

0 comments on commit 1cba5e6

Please sign in to comment.