Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from jpicht/feat/add-func-to-wait-for-all-timers
Browse files Browse the repository at this point in the history
Add method to forward time until all timers are expired
  • Loading branch information
djmitche authored Apr 23, 2023
2 parents 8f7eecd + 419081f commit 2fbf0cb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ func (m *Mock) Set(t time.Time) {
gosched()
}

// WaitForAllTimers sets the clock until all timers are expired
func (m *Mock) WaitForAllTimers() time.Time {
// Continue to execute timers until there are no more
for {
m.mu.Lock()
if len(m.timers) == 0 {
m.mu.Unlock()
return m.Now()
}

sort.Sort(m.timers)
next := m.timers[len(m.timers)-1].Next()
m.mu.Unlock()
m.Set(next)
}
}

// runNextTimer executes the next timer in chronological order and moves the
// current time to the timer's next tick time. The next time is not executed if
// its next time is after the max time. Returns true if a timer was executed.
Expand Down

0 comments on commit 2fbf0cb

Please sign in to comment.