influxdb/timer_test.go

87 lines
1.6 KiB
Go
Raw Normal View History

2013-04-28 21:23:21 +00:00
package raft
import (
2013-05-05 19:36:23 +00:00
"testing"
2013-04-28 21:23:21 +00:00
"time"
)
//------------------------------------------------------------------------------
//
// Tests
//
//------------------------------------------------------------------------------
// Ensure that we can start an election timer and it will go off in the specified duration.
func TestTimer(t *testing.T) {
2013-07-06 04:49:47 +00:00
timer := newTimer(5*time.Millisecond, 10*time.Millisecond)
// test timer start
for i := 0; i < 10; i++ {
start := time.Now()
2013-07-06 04:49:47 +00:00
timer.start()
duration := time.Now().Sub(start)
if duration > 12*time.Millisecond || duration < 5*time.Millisecond {
t.Fatal("Duration Error! ", duration)
2013-04-28 21:23:21 +00:00
}
}
// test timer stop
for i := 0; i < 100; i++ {
start := time.Now()
go stop(timer)
2013-07-06 04:49:47 +00:00
timer.start()
duration := time.Now().Sub(start)
2013-06-27 00:21:00 +00:00
if duration > 3*time.Millisecond {
t.Fatal("Duration Error! ", duration)
}
// ready the timer after stop it
2013-07-06 04:49:47 +00:00
timer.ready()
2013-04-28 21:23:21 +00:00
}
// test timer fire
for i := 0; i < 100; i++ {
start := time.Now()
go fire(timer)
2013-07-06 04:49:47 +00:00
timer.start()
duration := time.Now().Sub(start)
2013-06-27 00:21:00 +00:00
if duration > 3*time.Millisecond {
t.Fatal("Fire Duration Error! ", duration)
}
2013-04-28 21:23:21 +00:00
}
resp := make(chan bool)
2013-06-27 02:35:32 +00:00
// play with start and stop
2013-06-27 00:12:44 +00:00
// make sure we can stop timer
// in all the possible seq of start and stop
for i := 0; i < 100; i++ {
go stop(timer)
go start(timer, resp)
ret := <-resp
if ret != false {
t.Fatal("cannot stop timer!")
}
2013-07-06 04:49:47 +00:00
timer.ready()
2013-04-28 21:23:21 +00:00
}
}
2013-07-06 04:49:47 +00:00
func stop(t *timer) {
time.Sleep(time.Millisecond)
2013-07-06 04:49:47 +00:00
t.stop()
}
2013-07-06 04:49:47 +00:00
func start(t *timer, resp chan bool) {
time.Sleep(time.Millisecond)
2013-07-06 04:49:47 +00:00
resp <- t.start()
}
2013-07-06 04:49:47 +00:00
func fire(t *timer) {
time.Sleep(time.Millisecond)
2013-07-06 04:49:47 +00:00
t.fire()
2013-06-27 00:12:44 +00:00
}