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 @@
package time_test
import (
"fmt"
"testing"
. "time"
)
func TestTicker(t *testing.T) {
const Count = 10
Delta := 100 * Millisecond
ticker := NewTicker(Delta)
// We want to test that a ticker takes as much time as expected.
// Since we don't want the test to run for too long, we don't
// want to use lengthy times. This makes the test inherently flaky.
// So only report an error if it fails five times in a row.
const count = 10
delta := 20 * Millisecond
var errs []string
logErrs := func() {
for _, e := range errs {
t.Log(e)
}
}
for i := 0; i < 5; i++ {
ticker := NewTicker(delta)
t0 := Now()
for i := 0; i < Count; i++ {
for i := 0; i < count; i++ {
<-ticker.C
}
ticker.Stop()
t1 := Now()
dt := t1.Sub(t0)
target := Delta * Count
target := delta * count
slop := target * 2 / 10
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)
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)
// Now test that the ticker stopped.
Sleep(2 * delta)
select {
case <-ticker.C:
t.Fatal("Ticker did not shut down")
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
......
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