Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: fix swallowing ticks on TickerFunc #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,24 @@ func (m *mockTickerFunc) next() time.Time {

func (m *mockTickerFunc) fire(_ time.Time) {
m.mock.mu.Lock()
defer m.mock.mu.Unlock()
if m.done || m.inProgress {
if m.done {
m.mock.mu.Unlock()
return
}
m.nxt = m.nxt.Add(m.d)
m.mock.recomputeNextLocked()
// we need this check to happen after we've computed the next tick,
// otherwise it will be immediately rescheduled.
if m.inProgress {
m.mock.mu.Unlock()
return
}

m.inProgress = true
m.mock.mu.Unlock()
err := m.f()
m.mock.mu.Lock()
defer m.mock.mu.Unlock()
m.inProgress = false
m.cond.Broadcast() // wake up anything waiting for f to finish
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,13 @@ func TestTickerFunc_LongCallback(t *testing.T) {
case <-testCtx.Done():
t.Fatal("timeout waiting for tickStart")
}
// second tick completes immediately, since it doesn't actually call the
// ticker function.
mClock.Advance(time.Second).MustWait(testCtx)
// additional ticks complete immediately.
elapsed := time.Duration(0)
for elapsed < 5*time.Second {
d, wt := mClock.AdvanceNext()
elapsed += d
wt.MustWait(testCtx)
}

waitErr := make(chan error, 1)
go func() {
Expand Down