Commit 86a8d59a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

time: make TestReset more reliable

Fixes #4690

R=golang-dev, alex.brainman, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/7181052
parent da82dfac
......@@ -247,26 +247,49 @@ func TestSleepZeroDeadlock(t *testing.T) {
<-c
}
func TestReset(t *testing.T) {
t0 := NewTimer(100 * Millisecond)
Sleep(50 * Millisecond)
if t0.Reset(150*Millisecond) != true {
t.Fatalf("resetting unfired timer returned false")
func testReset(d Duration) error {
t0 := NewTimer(2 * d)
Sleep(d)
if t0.Reset(3*d) != true {
return errors.New("resetting unfired timer returned false")
}
Sleep(100 * Millisecond)
Sleep(2 * d)
select {
case <-t0.C:
t.Fatalf("timer fired early")
return errors.New("timer fired early")
default:
}
Sleep(100 * Millisecond)
Sleep(2 * d)
select {
case <-t0.C:
default:
t.Fatalf("reset timer did not fire")
return errors.New("reset timer did not fire")
}
if t0.Reset(50*Millisecond) != false {
t.Fatalf("resetting expired timer returned true")
return errors.New("resetting expired timer returned true")
}
return nil
}
func TestReset(t *testing.T) {
// We try to run this test with increasingly larger multiples
// until one works so slow, loaded hardware isn't as flaky,
// but without slowing down fast machines unnecessarily.
const unit = 25 * Millisecond
tries := []Duration{
1 * unit,
3 * unit,
7 * unit,
15 * unit,
}
var err error
for _, d := range tries {
err = testReset(d)
if err == nil {
t.Logf("passed using duration %v", d)
return
}
}
t.Error(err)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment