From 26abd65e34910994e921020335ed0d5c01a113a9 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 5 Dec 2017 15:01:07 +0100 Subject: [PATCH] Add tests for repeat timer --- common/repeat_timer_test.go | 78 +++++++++++++++++++++++++++++++++++ common/throttle_timer_test.go | 10 ++--- 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 common/repeat_timer_test.go diff --git a/common/repeat_timer_test.go b/common/repeat_timer_test.go new file mode 100644 index 000000000..9f03f41df --- /dev/null +++ b/common/repeat_timer_test.go @@ -0,0 +1,78 @@ +package common + +import ( + "sync" + "testing" + "time" + + // make govet noshadow happy... + asrt "github.com/stretchr/testify/assert" +) + +type rCounter struct { + input chan time.Time + 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) { + assert := asrt.New(test) + + dur := time.Duration(50) * time.Millisecond + short := time.Duration(20) * time.Millisecond + // delay waits for cnt durations, an a little extra + delay := func(cnt int) time.Duration { + return time.Duration(cnt)*dur + time.Millisecond + } + t := NewRepeatTimer("bar", dur) + + // start at 0 + c := &rCounter{input: t.Ch} + go c.Read() + assert.Equal(0, c.Count()) + + // wait for 4 periods + time.Sleep(delay(4)) + assert.Equal(4, c.Count()) + + // keep reseting leads to no firing + for i := 0; i < 20; i++ { + time.Sleep(short) + t.Reset() + } + assert.Equal(4, c.Count()) + + // after this, it still works normal + time.Sleep(delay(2)) + assert.Equal(6, c.Count()) + + // after a stop, nothing more is sent + stopped := t.Stop() + assert.True(stopped) + time.Sleep(delay(7)) + assert.Equal(6, c.Count()) + + // close channel to stop counter + close(t.Ch) +} diff --git a/common/throttle_timer_test.go b/common/throttle_timer_test.go index bd03a7a68..00f5abdec 100644 --- a/common/throttle_timer_test.go +++ b/common/throttle_timer_test.go @@ -9,19 +9,19 @@ import ( asrt "github.com/stretchr/testify/assert" ) -type counter struct { +type thCounter struct { input chan struct{} mtx sync.Mutex count int } -func (c *counter) Increment() { +func (c *thCounter) Increment() { c.mtx.Lock() c.count++ c.mtx.Unlock() } -func (c *counter) Count() int { +func (c *thCounter) Count() int { c.mtx.Lock() val := c.count c.mtx.Unlock() @@ -30,7 +30,7 @@ func (c *counter) Count() int { // Read should run in a go-routine and // updates count by one every time a packet comes in -func (c *counter) Read() { +func (c *thCounter) Read() { for range c.input { c.Increment() } @@ -45,7 +45,7 @@ func TestThrottle(test *testing.T) { t := NewThrottleTimer("foo", delay) // start at 0 - c := &counter{input: t.Ch} + c := &thCounter{input: t.Ch} assert.Equal(0, c.Count()) go c.Read()