diff --git a/common/mclock/mclock.go b/common/mclock/mclock.go index 7df76d3133..3aca257cb3 100644 --- a/common/mclock/mclock.go +++ b/common/mclock/mclock.go @@ -62,8 +62,8 @@ type Timer interface { type ChanTimer interface { Timer - // The channel returned by Chan receives a value when the timer expires. - Chan() <-chan AbsTime + // The channel returned by C receives a value when the timer expires. + C() <-chan AbsTime // Reset reschedules the timer with a new timeout. // It should be invoked only on stopped or expired timers with drained channels. Reset(time.Duration) @@ -118,6 +118,6 @@ func (st *systemTimer) Reset(d time.Duration) { st.Timer.Reset(d) } -func (st *systemTimer) Chan() <-chan AbsTime { +func (st *systemTimer) C() <-chan AbsTime { return st.ch } diff --git a/common/mclock/simclock.go b/common/mclock/simclock.go index dd680dfa32..766ca0f873 100644 --- a/common/mclock/simclock.go +++ b/common/mclock/simclock.go @@ -119,7 +119,7 @@ func (s *Simulated) NewTimer(d time.Duration) ChanTimer { // After returns a channel which receives the current time after the clock // has advanced by d. func (s *Simulated) After(d time.Duration) <-chan AbsTime { - return s.NewTimer(d).Chan() + return s.NewTimer(d).C() } // AfterFunc runs fn after the clock has advanced by d. Unlike with the system @@ -161,20 +161,18 @@ func (ev *simTimer) Reset(d time.Duration) { ev.s.mu.Lock() defer ev.s.mu.Unlock() + ev.at = ev.s.now.Add(d) if ev.index < 0 { - // already expired - heap.Push(&ev.s.scheduled, ev) + heap.Push(&ev.s.scheduled, ev) // already expired } else { - // hasn't fired yet, reschedule - ev.at = ev.s.now.Add(d) - heap.Fix(&ev.s.scheduled, ev.index) + heap.Fix(&ev.s.scheduled, ev.index) // hasn't fired yet, reschedule } ev.s.cond.Broadcast() } -func (ev *simTimer) Chan() <-chan AbsTime { +func (ev *simTimer) C() <-chan AbsTime { if ev.ch == nil { - panic("mclock: Chan() on timer created by AfterFunc") + panic("mclock: C() on timer created by AfterFunc") } return ev.ch } diff --git a/common/mclock/simclock_test.go b/common/mclock/simclock_test.go index 41ec846aab..94aa4f2b39 100644 --- a/common/mclock/simclock_test.go +++ b/common/mclock/simclock_test.go @@ -124,7 +124,10 @@ func TestSimulatedTimerReset(t *testing.T) { timer := c.NewTimer(timeout) c.Run(2 * timeout) select { - case <-timer.Chan(): + case ftime := <-timer.C(): + if ftime != AbsTime(timeout) { + t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(timeout)) + } default: t.Fatal("timer didn't fire") } @@ -132,7 +135,10 @@ func TestSimulatedTimerReset(t *testing.T) { timer.Reset(timeout) c.Run(2 * timeout) select { - case <-timer.Chan(): + case ftime := <-timer.C(): + if ftime != AbsTime(3*timeout) { + t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(3*timeout)) + } default: t.Fatal("timer didn't fire again") } @@ -149,7 +155,7 @@ func TestSimulatedTimerStop(t *testing.T) { t.Errorf("Stop returned true for fired timer") } select { - case <-timer.Chan(): + case <-timer.C(): default: t.Fatal("timer didn't fire") }