Refactor throttle timer

This commit is contained in:
Ethan Frey
2017-12-06 11:17:50 +01:00
parent 3d9113c16e
commit dcb4395604
2 changed files with 86 additions and 37 deletions

View File

@ -41,6 +41,7 @@ func TestThrottle(test *testing.T) {
ms := 50
delay := time.Duration(ms) * time.Millisecond
shortwait := time.Duration(ms/2) * time.Millisecond
longwait := time.Duration(2) * delay
t := NewThrottleTimer("foo", delay)
@ -65,6 +66,21 @@ func TestThrottle(test *testing.T) {
time.Sleep(longwait)
assert.Equal(2, c.Count())
// keep cancelling before it is ready
for i := 0; i < 10; i++ {
t.Set()
time.Sleep(shortwait)
t.Unset()
}
time.Sleep(longwait)
assert.Equal(2, c.Count())
// a few unsets do nothing...
for i := 0; i < 5; i++ {
t.Unset()
}
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++ {
@ -74,5 +90,6 @@ func TestThrottle(test *testing.T) {
time.Sleep(longwait)
assert.Equal(5, c.Count())
close(t.Ch)
stopped := t.Stop()
assert.True(stopped)
}