Skip to content

Commit 7f10f7f

Browse files
authored
fix: fix swallowing ticks on TickerFunc (#8)
Turns out #7 is bugged. When we refused to call the TickerFunc callback, we didn't advance the next event time, so the Mock kept re-scheduling `fire()` and we could never finish processing on the tick time.
1 parent abe75f0 commit 7f10f7f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

mock.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,24 @@ func (m *mockTickerFunc) next() time.Time {
480480

481481
func (m *mockTickerFunc) fire(_ time.Time) {
482482
m.mock.mu.Lock()
483-
defer m.mock.mu.Unlock()
484-
if m.done || m.inProgress {
483+
if m.done {
484+
m.mock.mu.Unlock()
485485
return
486486
}
487487
m.nxt = m.nxt.Add(m.d)
488488
m.mock.recomputeNextLocked()
489+
// we need this check to happen after we've computed the next tick,
490+
// otherwise it will be immediately rescheduled.
491+
if m.inProgress {
492+
m.mock.mu.Unlock()
493+
return
494+
}
489495

490496
m.inProgress = true
491497
m.mock.mu.Unlock()
492498
err := m.f()
493499
m.mock.mu.Lock()
500+
defer m.mock.mu.Unlock()
494501
m.inProgress = false
495502
m.cond.Broadcast() // wake up anything waiting for f to finish
496503
if err != nil {

mock_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,13 @@ func TestTickerFunc_LongCallback(t *testing.T) {
292292
case <-testCtx.Done():
293293
t.Fatal("timeout waiting for tickStart")
294294
}
295-
// second tick completes immediately, since it doesn't actually call the
296-
// ticker function.
297-
mClock.Advance(time.Second).MustWait(testCtx)
295+
// additional ticks complete immediately.
296+
elapsed := time.Duration(0)
297+
for elapsed < 5*time.Second {
298+
d, wt := mClock.AdvanceNext()
299+
elapsed += d
300+
wt.MustWait(testCtx)
301+
}
298302

299303
waitErr := make(chan error, 1)
300304
go func() {

0 commit comments

Comments
 (0)