Skip to content
Open
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
18 changes: 18 additions & 0 deletions clockwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,21 @@ func (fc *FakeClock) setExpirer(e expirer, d time.Duration) {
return false
})
}

// NextWakeup returns the time of the next expirer to wake up.
//
// If there are no waiters, it returns the zero time.
//
// This can be used with Advance to wake subsequent blockers no matter
// what time they are blocked on.
func (fc *FakeClock) NextWakeup() time.Time {
fc.l.RLock()
defer fc.l.RUnlock()
var end time.Time
for i, s := range fc.waiters {
if i == 0 || s.expiration().Before(end) {
end = s.expiration()
}
}
return end
}
27 changes: 27 additions & 0 deletions clockwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,30 @@ func TestFakeClockRace(t *testing.T) {
go func() { fc.NewTimer(d) }()
go func() { fc.Sleep(d) }()
}

func TestNextWakeup(t *testing.T) {
t.Parallel()
zero := time.Time{}
fc := NewFakeClockAt(zero)
if got := fc.NextWakeup(); got != zero {
t.Errorf("fakeClock.nextWakeup() returned %v, want zero", got)
}

ch := fc.After(time.Minute)

if got := fc.NextWakeup(); got != zero.Add(time.Minute) {
t.Errorf("fakeClock.nextWakeup() returned %v, want 1 minute", got)
}

fc.Advance(fc.NextWakeup().Sub(fc.Now()))
<-ch

if got := fc.NextWakeup(); got != zero {
t.Errorf("fakeClock.nextWakeup() returned %v, want zero", got)
}

// Check now
if got := fc.Now(); got != zero.Add(time.Minute) {
t.Errorf("fakeClock.Now() returned %v, want zero+1 minute", got)
}
}