Browse Source

Merge pull request #90 from tendermint/feature/throttle-timer

test timers
pull/1842/head
Anton Kaliaev 7 years ago
committed by GitHub
parent
commit
27ab1f6bd9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 156 additions and 0 deletions
  1. +78
    -0
      common/repeat_timer_test.go
  2. +78
    -0
      common/throttle_timer_test.go

+ 78
- 0
common/repeat_timer_test.go View File

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

+ 78
- 0
common/throttle_timer_test.go View File

@ -0,0 +1,78 @@
package common
import (
"sync"
"testing"
"time"
// make govet noshadow happy...
asrt "github.com/stretchr/testify/assert"
)
type thCounter struct {
input chan struct{}
mtx sync.Mutex
count int
}
func (c *thCounter) Increment() {
c.mtx.Lock()
c.count++
c.mtx.Unlock()
}
func (c *thCounter) 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 *thCounter) Read() {
for range c.input {
c.Increment()
}
}
func TestThrottle(test *testing.T) {
assert := asrt.New(test)
ms := 50
delay := time.Duration(ms) * time.Millisecond
longwait := time.Duration(2) * delay
t := NewThrottleTimer("foo", delay)
// start at 0
c := &thCounter{input: t.Ch}
assert.Equal(0, c.Count())
go c.Read()
// waiting does nothing
time.Sleep(longwait)
assert.Equal(0, c.Count())
// send one event adds one
t.Set()
time.Sleep(longwait)
assert.Equal(1, c.Count())
// send a burst adds one
for i := 0; i < 5; i++ {
t.Set()
}
time.Sleep(longwait)
assert.Equal(2, c.Count())
// send 12, over 2 delay sections, adds 3
short := time.Duration(ms/5) * time.Millisecond
for i := 0; i < 13; i++ {
t.Set()
time.Sleep(short)
}
time.Sleep(longwait)
assert.Equal(5, c.Count())
close(t.Ch)
}

Loading…
Cancel
Save