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 {
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
}

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
// 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()
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)
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()
}
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
}

View file

@ -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")
}