Skip to content
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

Add WithDeadlineTimer interface to clock #312

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ type Ticker interface {

var _ = WithTicker(RealClock{})

// WithDeadlineTimer extends Clock to add NewDeadlineTimer which fires at a given moment of time.
type WithDeadlineTimer interface {
Clock
// NewDeadlineTimer returns a new Timer which fires at ts.
NewDeadlineTimer(ts time.Time) Timer
}

var _ = WithDeadlineTimer(RealClock{})

// RealClock really calls time.Now()
type RealClock struct{}

Expand Down Expand Up @@ -108,6 +117,13 @@ func (RealClock) NewTimer(d time.Duration) Timer {
}
}

// NewDeadlineTimer is the same as time.NewTimer(time.Until(ts))
func (RealClock) NewDeadlineTimer(ts time.Time) Timer {
return &realTimer{
timer: time.NewTimer(time.Until(ts)),
}
}

// AfterFunc is the same as time.AfterFunc(d, f).
func (RealClock) AfterFunc(d time.Duration, f func()) Timer {
return &realTimer{
Expand Down
17 changes: 17 additions & 0 deletions clock/testing/fake_clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
var (
_ = clock.PassiveClock(&FakePassiveClock{})
_ = clock.WithTicker(&FakeClock{})
_ = clock.WithDeadlineTimer(&FakeClock{})
_ = clock.Clock(&IntervalClock{})
)

Expand Down Expand Up @@ -117,6 +118,22 @@ func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
return timer
}

// NewDeadlineTimer constructs a fake timer which fires at ts.
func (f *FakeClock) NewDeadlineTimer(ts time.Time) clock.Timer {
f.lock.Lock()
defer f.lock.Unlock()
ch := make(chan time.Time, 1) // Don't block!
timer := &fakeTimer{
fakeClock: f,
waiter: fakeClockWaiter{
targetTime: ts,
destChan: ch,
},
}
f.waiters = append(f.waiters, &timer.waiter)
return timer
}

// AfterFunc is the Fake version of time.AfterFunc(d, cb).
func (f *FakeClock) AfterFunc(d time.Duration, cb func()) clock.Timer {
f.lock.Lock()
Expand Down
26 changes: 26 additions & 0 deletions clock/testing/fake_clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,32 @@ func TestTimerNegative(t *testing.T) {
}
}

func TestDeadlineTimerFakeStop(t *testing.T) {
tc := NewFakeClock(time.Now())
timer := tc.NewDeadlineTimer(tc.Now().Add(time.Second))
if !tc.HasWaiters() {
t.Errorf("expected a waiter to be present, but it is not")
}
timer.Stop()
if tc.HasWaiters() {
t.Errorf("expected existing waiter to be cleaned up, but it is still present")
}
}

func TestDeadlineTimerNegative(t *testing.T) {
tc := NewFakeClock(time.Now())
timer := tc.NewDeadlineTimer(tc.Now().Add(-1 * time.Second))
if !tc.HasWaiters() {
t.Errorf("expected a waiter to be present, but it is not")
}
// force waiters to be called
tc.Step(0)
tick := assertReadTime(t, timer.C())
if tick != tc.Now() {
t.Errorf("expected -1s to turn into now: %v != %v", tick, tc.Now())
}
}

func TestTickNegative(t *testing.T) {
// The stdlib 'Tick' returns nil for negative and zero values, so our fake
// should too.
Expand Down