2017-12-05 06:01:07 -08:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-14 21:20:53 -08:00
|
|
|
"sync"
|
2017-12-05 06:01:07 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-12-23 04:18:50 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-05 06:01:07 -08:00
|
|
|
)
|
|
|
|
|
2017-12-25 08:10:48 -08:00
|
|
|
func TestDefaultTicker(t *testing.T) {
|
|
|
|
ticker := defaultTickerMaker(time.Millisecond * 10)
|
|
|
|
<-ticker.Chan()
|
|
|
|
ticker.Stop()
|
|
|
|
}
|
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
func TestRepeatTimer(t *testing.T) {
|
2017-12-05 06:01:07 -08:00
|
|
|
|
2017-12-21 08:15:17 -08:00
|
|
|
ch := make(chan time.Time, 100)
|
2018-01-14 21:20:53 -08:00
|
|
|
mtx := new(sync.Mutex)
|
2017-12-23 04:18:50 -08:00
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
// tick() fires from start to end
|
|
|
|
// (exclusive) in milliseconds with incr.
|
|
|
|
// It locks on mtx, so subsequent calls
|
|
|
|
// run in series.
|
|
|
|
tick := func(startMs, endMs, incrMs time.Duration) {
|
|
|
|
mtx.Lock()
|
|
|
|
go func() {
|
|
|
|
for tMs := startMs; tMs < endMs; tMs += incrMs {
|
|
|
|
lt := time.Time{}
|
|
|
|
lt = lt.Add(tMs * time.Millisecond)
|
|
|
|
ch <- lt
|
|
|
|
}
|
|
|
|
mtx.Unlock()
|
|
|
|
}()
|
2017-12-05 06:01:07 -08:00
|
|
|
}
|
2017-12-23 04:18:50 -08:00
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
// tock consumes Ticker.Chan() events and checks them against the ms in "timesMs".
|
|
|
|
tock := func(t *testing.T, rt *RepeatTimer, timesMs []int64) {
|
|
|
|
|
|
|
|
// Check against timesMs.
|
|
|
|
for _, timeMs := range timesMs {
|
|
|
|
tyme := <-rt.Chan()
|
|
|
|
sinceMs := tyme.Sub(time.Time{}) / time.Millisecond
|
|
|
|
assert.Equal(t, timeMs, int64(sinceMs))
|
2017-12-21 08:15:17 -08:00
|
|
|
}
|
2018-01-14 21:20:53 -08:00
|
|
|
|
|
|
|
// TODO detect number of running
|
|
|
|
// goroutines to ensure that
|
|
|
|
// no other times will fire.
|
|
|
|
// See https://github.com/tendermint/tmlibs/issues/120.
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
2017-12-21 08:15:17 -08:00
|
|
|
done := true
|
|
|
|
select {
|
2017-12-23 04:18:50 -08:00
|
|
|
case <-rt.Chan():
|
2017-12-21 08:15:17 -08:00
|
|
|
done = false
|
|
|
|
default:
|
|
|
|
}
|
2017-12-23 04:18:50 -08:00
|
|
|
assert.True(t, done)
|
2017-12-21 08:15:17 -08:00
|
|
|
}
|
|
|
|
|
2017-12-23 04:18:50 -08:00
|
|
|
tm := NewLogicalTickerMaker(ch)
|
2018-01-14 21:20:53 -08:00
|
|
|
rt := NewRepeatTimerWithTickerMaker("bar", time.Second, tm)
|
2017-12-05 06:01:07 -08:00
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
/* NOTE: Useful for debugging deadlocks...
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
trace := make([]byte, 102400)
|
|
|
|
count := runtime.Stack(trace, true)
|
|
|
|
fmt.Printf("Stack of %d bytes: %s\n", count, trace)
|
|
|
|
}()
|
|
|
|
*/
|
2017-12-05 06:01:07 -08:00
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
tick(0, 1000, 10)
|
|
|
|
tock(t, rt, []int64{})
|
|
|
|
tick(1000, 2000, 10)
|
|
|
|
tock(t, rt, []int64{1000})
|
|
|
|
tick(2005, 5000, 10)
|
|
|
|
tock(t, rt, []int64{2005, 3005, 4005})
|
|
|
|
tick(5001, 5999, 1)
|
|
|
|
// Read 5005 instead of 5001 because
|
|
|
|
// it's 1 second greater than 4005.
|
|
|
|
tock(t, rt, []int64{5005})
|
|
|
|
tick(6000, 7005, 1)
|
|
|
|
tock(t, rt, []int64{6005})
|
|
|
|
tick(7033, 8032, 1)
|
|
|
|
tock(t, rt, []int64{7033})
|
2017-12-23 04:18:50 -08:00
|
|
|
|
2018-01-14 21:20:53 -08:00
|
|
|
// After a reset, nothing happens
|
|
|
|
// until two ticks are received.
|
|
|
|
rt.Reset()
|
|
|
|
tock(t, rt, []int64{})
|
|
|
|
tick(8040, 8041, 1)
|
|
|
|
tock(t, rt, []int64{})
|
|
|
|
tick(9555, 9556, 1)
|
|
|
|
tock(t, rt, []int64{9555})
|
2017-12-05 06:01:07 -08:00
|
|
|
|
2017-12-23 04:18:50 -08:00
|
|
|
// After a stop, nothing more is sent.
|
|
|
|
rt.Stop()
|
2018-01-14 21:20:53 -08:00
|
|
|
tock(t, rt, []int64{})
|
2017-12-05 06:01:07 -08:00
|
|
|
|
2017-12-23 04:18:50 -08:00
|
|
|
// Another stop panics.
|
|
|
|
assert.Panics(t, func() { rt.Stop() })
|
2017-12-05 06:01:07 -08:00
|
|
|
}
|