cmn: fix repeate timer test with manual ticker

This commit is contained in:
Ethan Buchman 2017-12-21 11:15:17 -05:00
parent a25ed5ba1b
commit b0b740210c
2 changed files with 114 additions and 53 deletions

View File

@ -5,6 +5,72 @@ import (
"time" "time"
) )
// Ticker is a basic ticker interface.
type Ticker interface {
Chan() <-chan time.Time
Stop()
Reset()
}
// DefaultTicker wraps the stdlibs Ticker implementation.
type DefaultTicker struct {
t *time.Ticker
dur time.Duration
}
// NewDefaultTicker returns a new DefaultTicker
func NewDefaultTicker(dur time.Duration) *DefaultTicker {
return &DefaultTicker{
time.NewTicker(dur),
dur,
}
}
// Implements Ticker
func (t *DefaultTicker) Chan() <-chan time.Time {
return t.t.C
}
// Implements Ticker
func (t *DefaultTicker) Stop() {
t.t.Stop()
t.t = nil
}
// Implements Ticker
func (t *DefaultTicker) Reset() {
t.t = time.NewTicker(t.dur)
}
// ManualTicker wraps a channel that can be manually sent on
type ManualTicker struct {
ch chan time.Time
}
// NewManualTicker returns a new ManualTicker
func NewManualTicker(ch chan time.Time) *ManualTicker {
return &ManualTicker{
ch: ch,
}
}
// Implements Ticker
func (t *ManualTicker) Chan() <-chan time.Time {
return t.ch
}
// Implements Ticker
func (t *ManualTicker) Stop() {
// noop
}
// Implements Ticker
func (t *ManualTicker) Reset() {
// noop
}
//---------------------------------------------------------------------
/* /*
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period. RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
It's good for keeping connections alive. It's good for keeping connections alive.
@ -15,30 +81,35 @@ type RepeatTimer struct {
mtx sync.Mutex mtx sync.Mutex
name string name string
ticker *time.Ticker ticker Ticker
quit chan struct{} quit chan struct{}
wg *sync.WaitGroup wg *sync.WaitGroup
dur time.Duration
} }
// NewRepeatTimer returns a RepeatTimer with the DefaultTicker.
func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer { func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
ticker := NewDefaultTicker(dur)
return NewRepeatTimerWithTicker(name, ticker)
}
// NewRepeatTimerWithTicker returns a RepeatTimer with the given ticker.
func NewRepeatTimerWithTicker(name string, ticker Ticker) *RepeatTimer {
var t = &RepeatTimer{ var t = &RepeatTimer{
Ch: make(chan time.Time), Ch: make(chan time.Time),
ticker: time.NewTicker(dur), ticker: ticker,
quit: make(chan struct{}), quit: make(chan struct{}),
wg: new(sync.WaitGroup), wg: new(sync.WaitGroup),
name: name, name: name,
dur: dur,
} }
t.wg.Add(1) t.wg.Add(1)
go t.fireRoutine(t.ticker) go t.fireRoutine(t.ticker)
return t return t
} }
func (t *RepeatTimer) fireRoutine(ticker *time.Ticker) { func (t *RepeatTimer) fireRoutine(ticker Ticker) {
for { for {
select { select {
case t_ := <-ticker.C: case t_ := <-ticker.Chan():
t.Ch <- t_ t.Ch <- t_
case <-t.quit: case <-t.quit:
// needed so we know when we can reset t.quit // needed so we know when we can reset t.quit
@ -55,7 +126,7 @@ func (t *RepeatTimer) Reset() {
t.mtx.Lock() // Lock t.mtx.Lock() // Lock
defer t.mtx.Unlock() defer t.mtx.Unlock()
t.ticker = time.NewTicker(t.dur) t.ticker.Reset()
t.quit = make(chan struct{}) t.quit = make(chan struct{})
t.wg.Add(1) t.wg.Add(1)
go t.fireRoutine(t.ticker) go t.fireRoutine(t.ticker)
@ -80,7 +151,6 @@ func (t *RepeatTimer) Stop() bool {
} }
close(t.quit) close(t.quit)
t.wg.Wait() // must wait for quit to close else we race Reset t.wg.Wait() // must wait for quit to close else we race Reset
t.ticker = nil
} }
return exists return exists
} }

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"sync"
"testing" "testing"
"time" "time"
@ -9,69 +8,61 @@ import (
asrt "github.com/stretchr/testify/assert" asrt "github.com/stretchr/testify/assert"
) )
type rCounter struct { // NOTE: this only tests with the ManualTicker.
input chan time.Time // How do you test a real-clock ticker properly?
mtx sync.Mutex
count int
}
func (c *rCounter) Increment() {
c.mtx.Lock()
c.count++
c.mtx.Unlock()
}
func (c *rCounter) Count() int {
c.mtx.Lock()
val := c.count
c.mtx.Unlock()
return val
}
// Read should run in a go-routine and
// updates count by one every time a packet comes in
func (c *rCounter) Read() {
for range c.input {
c.Increment()
}
}
func TestRepeat(test *testing.T) { func TestRepeat(test *testing.T) {
assert := asrt.New(test) assert := asrt.New(test)
dur := time.Duration(50) * time.Millisecond ch := make(chan time.Time, 100)
short := time.Duration(20) * time.Millisecond // tick fires cnt times on ch
// delay waits for cnt durations, an a little extra tick := func(cnt int) {
delay := func(cnt int) time.Duration { for i := 0; i < cnt; i++ {
return time.Duration(cnt)*dur + time.Millisecond ch <- time.Now()
} }
t := NewRepeatTimer("bar", dur) }
tock := func(test *testing.T, t *RepeatTimer, cnt int) {
for i := 0; i < cnt; i++ {
after := time.After(time.Second * 2)
select {
case <-t.Ch:
case <-after:
test.Fatal("expected ticker to fire")
}
}
done := true
select {
case <-t.Ch:
done = false
default:
}
assert.True(done)
}
ticker := NewManualTicker(ch)
t := NewRepeatTimerWithTicker("bar", ticker)
// start at 0 // start at 0
c := &rCounter{input: t.Ch} tock(test, t, 0)
go c.Read()
assert.Equal(0, c.Count())
// wait for 4 periods // wait for 4 periods
time.Sleep(delay(4)) tick(4)
assert.Equal(4, c.Count()) tock(test, t, 4)
// keep reseting leads to no firing // keep reseting leads to no firing
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
time.Sleep(short) time.Sleep(time.Millisecond)
t.Reset() t.Reset()
} }
assert.Equal(4, c.Count()) tock(test, t, 0)
// after this, it still works normal // after this, it still works normal
time.Sleep(delay(2)) tick(2)
assert.Equal(6, c.Count()) tock(test, t, 2)
// after a stop, nothing more is sent // after a stop, nothing more is sent
stopped := t.Stop() stopped := t.Stop()
assert.True(stopped) assert.True(stopped)
time.Sleep(delay(7)) tock(test, t, 0)
assert.Equal(6, c.Count())
// close channel to stop counter // close channel to stop counter
close(t.Ch) close(t.Ch)