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.
|
2013-06-26 23:48:47 +00:00
|
|
|
func TestTimer(t *testing.T) {
|
|
|
|
|
2013-05-05 20:26:04 +00:00
|
|
|
timer := NewTimer(5*time.Millisecond, 10*time.Millisecond)
|
2013-06-26 23:48:47 +00:00
|
|
|
|
|
|
|
// test timer start
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2013-06-26 23:48:47 +00:00
|
|
|
|
2013-04-28 21:23:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 23:48:47 +00:00
|
|
|
// test timer stop
|
|
|
|
for i := 0; i < 100; i++ {
|
2013-04-28 21:23:21 +00:00
|
|
|
|
2013-06-26 23:48:47 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
go stop(timer)
|
|
|
|
|
|
|
|
timer.Start()
|
|
|
|
|
|
|
|
duration := time.Now().Sub(start)
|
2013-06-27 00:21:00 +00:00
|
|
|
if duration > 3*time.Millisecond {
|
2013-06-26 23:48:47 +00:00
|
|
|
t.Fatal("Duration Error! ", duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ready the timer after stop it
|
|
|
|
timer.Ready()
|
2013-04-28 21:23:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-26 23:48:47 +00:00
|
|
|
// test timer fire
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
go fire(timer)
|
|
|
|
|
|
|
|
timer.Start()
|
|
|
|
|
|
|
|
duration := time.Now().Sub(start)
|
2013-06-27 00:21:00 +00:00
|
|
|
if duration > 3*time.Millisecond {
|
2013-06-26 23:48:47 +00:00
|
|
|
t.Fatal("Fire Duration Error! ", duration)
|
|
|
|
}
|
|
|
|
|
2013-04-28 21:23:21 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 23:48:47 +00:00
|
|
|
resp := make(chan bool)
|
|
|
|
// play with start and stop
|
2013-06-27 00:12:44 +00:00
|
|
|
// make sure we can stop timer
|
2013-06-26 23:48:47 +00:00
|
|
|
// 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!")
|
|
|
|
}
|
|
|
|
timer.Ready()
|
2013-04-28 21:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-06-26 23:48:47 +00:00
|
|
|
|
|
|
|
func stop(t *Timer) {
|
|
|
|
// sync
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
t.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(t *Timer, resp chan bool) {
|
|
|
|
// sync
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
resp <- t.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fire(t *Timer) {
|
|
|
|
// sync
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
t.Fire()
|
2013-06-27 00:12:44 +00:00
|
|
|
}
|