Skip to content

Commit

Permalink
Merge pull request robfig#3 from cojac/entries_bug
Browse files Browse the repository at this point in the history
Fixed bug when calling Entries()
  • Loading branch information
robfig committed Jul 19, 2013
2 parents 004a515 + eab026b commit 4832237
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (c *Cron) run() {
e.Prev = e.Next
e.Next = e.Schedule.Next(effective)
}
continue

case newEntry := <-c.add:
c.entries = append(c.entries, newEntry)
Expand All @@ -166,6 +167,9 @@ func (c *Cron) run() {
case <-c.stop:
return
}

// 'now' should be updated after newEntry and snapshot cases.
now = time.Now().Local()
}
}

Expand Down
25 changes: 25 additions & 0 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ func TestAddWhileRunning(t *testing.T) {
}
}

// Test timing with Entries.
func TestSnapshotEntries(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)

cron := New()
cron.AddFunc("@every 2s", func() { wg.Done() })
cron.Start()
defer cron.Stop()

// Cron should fire in 2 seconds. After 1 second, call Entries.
select {
case <-time.After(ONE_SECOND):
cron.Entries()
}

// Even though Entries was called, the cron should fire at the 2 second mark.
select {
case <-time.After(ONE_SECOND):
t.FailNow()
case <-wait(wg):
}

}

// Test that the entries are correctly sorted.
// Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
// that the immediate entry runs immediately.
Expand Down

0 comments on commit 4832237

Please sign in to comment.