common/mclock: rename Chan() to C() and fix bug in Reset

This commit is contained in:
Felix Lange 2020-02-11 12:46:59 +01:00
parent caca997e5b
commit f6a760a224
3 changed files with 18 additions and 14 deletions

View file

@ -62,8 +62,8 @@ type Timer interface {
type ChanTimer interface { type ChanTimer interface {
Timer Timer
// The channel returned by Chan receives a value when the timer expires. // The channel returned by C receives a value when the timer expires.
Chan() <-chan AbsTime C() <-chan AbsTime
// Reset reschedules the timer with a new timeout. // Reset reschedules the timer with a new timeout.
// It should be invoked only on stopped or expired timers with drained channels. // It should be invoked only on stopped or expired timers with drained channels.
Reset(time.Duration) Reset(time.Duration)
@ -118,6 +118,6 @@ func (st *systemTimer) Reset(d time.Duration) {
st.Timer.Reset(d) st.Timer.Reset(d)
} }
func (st *systemTimer) Chan() <-chan AbsTime { func (st *systemTimer) C() <-chan AbsTime {
return st.ch return st.ch
} }

View file

@ -119,7 +119,7 @@ func (s *Simulated) NewTimer(d time.Duration) ChanTimer {
// After returns a channel which receives the current time after the clock // After returns a channel which receives the current time after the clock
// has advanced by d. // has advanced by d.
func (s *Simulated) After(d time.Duration) <-chan AbsTime { 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 // 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() ev.s.mu.Lock()
defer ev.s.mu.Unlock() defer ev.s.mu.Unlock()
if ev.index < 0 {
// already expired
heap.Push(&ev.s.scheduled, ev)
} else {
// hasn't fired yet, reschedule
ev.at = ev.s.now.Add(d) ev.at = ev.s.now.Add(d)
heap.Fix(&ev.s.scheduled, ev.index) if ev.index < 0 {
heap.Push(&ev.s.scheduled, ev) // already expired
} else {
heap.Fix(&ev.s.scheduled, ev.index) // hasn't fired yet, reschedule
} }
ev.s.cond.Broadcast() ev.s.cond.Broadcast()
} }
func (ev *simTimer) Chan() <-chan AbsTime { func (ev *simTimer) C() <-chan AbsTime {
if ev.ch == nil { if ev.ch == nil {
panic("mclock: Chan() on timer created by AfterFunc") panic("mclock: C() on timer created by AfterFunc")
} }
return ev.ch return ev.ch
} }

View file

@ -124,7 +124,10 @@ func TestSimulatedTimerReset(t *testing.T) {
timer := c.NewTimer(timeout) timer := c.NewTimer(timeout)
c.Run(2 * timeout) c.Run(2 * timeout)
select { 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: default:
t.Fatal("timer didn't fire") t.Fatal("timer didn't fire")
} }
@ -132,7 +135,10 @@ func TestSimulatedTimerReset(t *testing.T) {
timer.Reset(timeout) timer.Reset(timeout)
c.Run(2 * timeout) c.Run(2 * timeout)
select { 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: default:
t.Fatal("timer didn't fire again") t.Fatal("timer didn't fire again")
} }
@ -149,7 +155,7 @@ func TestSimulatedTimerStop(t *testing.T) {
t.Errorf("Stop returned true for fired timer") t.Errorf("Stop returned true for fired timer")
} }
select { select {
case <-timer.Chan(): case <-timer.C():
default: default:
t.Fatal("timer didn't fire") t.Fatal("timer didn't fire")
} }