Skip to content

Commit 4832237

Browse files
committed
Merge pull request robfig#3 from cojac/entries_bug
Fixed bug when calling Entries()
2 parents 004a515 + eab026b commit 4832237

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cron.go

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func (c *Cron) run() {
155155
e.Prev = e.Next
156156
e.Next = e.Schedule.Next(effective)
157157
}
158+
continue
158159

159160
case newEntry := <-c.add:
160161
c.entries = append(c.entries, newEntry)
@@ -166,6 +167,9 @@ func (c *Cron) run() {
166167
case <-c.stop:
167168
return
168169
}
170+
171+
// 'now' should be updated after newEntry and snapshot cases.
172+
now = time.Now().Local()
169173
}
170174
}
171175

cron_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ func TestAddWhileRunning(t *testing.T) {
7777
}
7878
}
7979

80+
// Test timing with Entries.
81+
func TestSnapshotEntries(t *testing.T) {
82+
wg := &sync.WaitGroup{}
83+
wg.Add(1)
84+
85+
cron := New()
86+
cron.AddFunc("@every 2s", func() { wg.Done() })
87+
cron.Start()
88+
defer cron.Stop()
89+
90+
// Cron should fire in 2 seconds. After 1 second, call Entries.
91+
select {
92+
case <-time.After(ONE_SECOND):
93+
cron.Entries()
94+
}
95+
96+
// Even though Entries was called, the cron should fire at the 2 second mark.
97+
select {
98+
case <-time.After(ONE_SECOND):
99+
t.FailNow()
100+
case <-wait(wg):
101+
}
102+
103+
}
104+
80105
// Test that the entries are correctly sorted.
81106
// Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
82107
// that the immediate entry runs immediately.

0 commit comments

Comments
 (0)