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

Fix cancelling timers in FakeClock package #37

Merged
merged 2 commits into from
Jun 21, 2018
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
20 changes: 10 additions & 10 deletions clock/testing/fake_clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type FakeClock struct {
time time.Time

// waiters are waiting for the fake time to pass their specified time
waiters []fakeClockWaiter
waiters []*fakeClockWaiter
}

type fakeClockWaiter struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (f *FakeClock) After(d time.Duration) <-chan time.Time {
defer f.lock.Unlock()
stopTime := f.time.Add(d)
ch := make(chan time.Time, 1) // Don't block!
f.waiters = append(f.waiters, fakeClockWaiter{
f.waiters = append(f.waiters, &fakeClockWaiter{
targetTime: stopTime,
destChan: ch,
})
Expand All @@ -91,7 +91,7 @@ func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
destChan: ch,
},
}
f.waiters = append(f.waiters, timer.waiter)
f.waiters = append(f.waiters, &timer.waiter)
return timer
}

Expand All @@ -100,7 +100,7 @@ func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
defer f.lock.Unlock()
tickTime := f.time.Add(d)
ch := make(chan time.Time, 1) // hold one tick
f.waiters = append(f.waiters, fakeClockWaiter{
f.waiters = append(f.waiters, &fakeClockWaiter{
targetTime: tickTime,
stepInterval: d,
skipIfBlocked: true,
Expand All @@ -127,9 +127,9 @@ func (f *FakeClock) SetTime(t time.Time) {
// Actually changes the time and checks any waiters. f must be write-locked.
func (f *FakeClock) setTimeLocked(t time.Time) {
f.time = t
newWaiters := make([]fakeClockWaiter, 0, len(f.waiters))
newWaiters := make([]*fakeClockWaiter, 0, len(f.waiters))
for i := range f.waiters {
w := &f.waiters[i]
w := f.waiters[i]
if !w.targetTime.After(t) {

if w.skipIfBlocked {
Expand All @@ -147,7 +147,7 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
for !w.targetTime.After(t) {
w.targetTime = w.targetTime.Add(w.stepInterval)
}
newWaiters = append(newWaiters, *w)
newWaiters = append(newWaiters, w)
}

} else {
Expand Down Expand Up @@ -226,11 +226,11 @@ func (f *fakeTimer) Stop() bool {
f.fakeClock.lock.Lock()
defer f.fakeClock.lock.Unlock()

newWaiters := make([]fakeClockWaiter, 0, len(f.fakeClock.waiters))
newWaiters := make([]*fakeClockWaiter, 0, len(f.fakeClock.waiters))
for i := range f.fakeClock.waiters {
w := &f.fakeClock.waiters[i]
w := f.fakeClock.waiters[i]
if w != &f.waiter {
newWaiters = append(newWaiters, *w)
newWaiters = append(newWaiters, w)
}
}

Expand Down
12 changes: 12 additions & 0 deletions clock/testing/fake_clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,15 @@ func TestFakeTick(t *testing.T) {
t.Errorf("unexpected number of accumulated ticks: %d", accumulatedTicks)
}
}

func TestFakeStop(t *testing.T) {
tc := NewFakeClock(time.Now())
timer := tc.NewTimer(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")
}
}