Commit 3efa09f3 authored by Ian Lance Taylor's avatar Ian Lance Taylor

time: deflake TestTicker

Take the opportunity of deflaking to make it take less time to run.

Updates #35537

Change-Id: I91ca8094fbe18fbfcd34dfda98da1592c9c82943
Reviewed-on: https://go-review.googlesource.com/c/go/+/207403
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent c726361f
...@@ -5,34 +5,63 @@ ...@@ -5,34 +5,63 @@
package time_test package time_test
import ( import (
"fmt"
"testing" "testing"
. "time" . "time"
) )
func TestTicker(t *testing.T) { func TestTicker(t *testing.T) {
const Count = 10 // We want to test that a ticker takes as much time as expected.
Delta := 100 * Millisecond // Since we don't want the test to run for too long, we don't
ticker := NewTicker(Delta) // want to use lengthy times. This makes the test inherently flaky.
t0 := Now() // So only report an error if it fails five times in a row.
for i := 0; i < Count; i++ {
<-ticker.C const count = 10
} delta := 20 * Millisecond
ticker.Stop()
t1 := Now() var errs []string
dt := t1.Sub(t0) logErrs := func() {
target := Delta * Count for _, e := range errs {
slop := target * 2 / 10 t.Log(e)
if dt < target-slop || (!testing.Short() && dt > target+slop) { }
t.Fatalf("%d %s ticks took %s, expected [%s,%s]", Count, Delta, dt, target-slop, target+slop)
} }
// Now test that the ticker stopped
Sleep(2 * Delta) for i := 0; i < 5; i++ {
select { ticker := NewTicker(delta)
case <-ticker.C: t0 := Now()
t.Fatal("Ticker did not shut down") for i := 0; i < count; i++ {
default: <-ticker.C
// ok }
ticker.Stop()
t1 := Now()
dt := t1.Sub(t0)
target := delta * count
slop := target * 2 / 10
if dt < target-slop || dt > target+slop {
errs = append(errs, fmt.Sprintf("%d %s ticks took %s, expected [%s,%s]", count, delta, dt, target-slop, target+slop))
continue
}
// Now test that the ticker stopped.
Sleep(2 * delta)
select {
case <-ticker.C:
errs = append(errs, "Ticker did not shut down")
continue
default:
// ok
}
// Test passed, so all done.
if len(errs) > 0 {
t.Logf("saw %d errors, ignoring to avoid flakiness", len(errs))
logErrs()
}
return
} }
t.Errorf("saw %d errors", len(errs))
logErrs()
} }
// Issue 21874 // Issue 21874
......
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