mirror of https://github.com/golang/go.git
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: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
c726361fff
commit
3efa09f386
|
|
@ -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)
|
||||
t0 := Now()
|
||||
for i := 0; i < Count; i++ {
|
||||
<-ticker.C
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
ticker.Stop()
|
||||
t1 := Now()
|
||||
dt := t1.Sub(t0)
|
||||
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)
|
||||
}
|
||||
// Now test that the ticker stopped
|
||||
Sleep(2 * Delta)
|
||||
select {
|
||||
case <-ticker.C:
|
||||
t.Fatal("Ticker did not shut down")
|
||||
default:
|
||||
// ok
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
ticker := NewTicker(delta)
|
||||
t0 := Now()
|
||||
for i := 0; i < count; i++ {
|
||||
<-ticker.C
|
||||
}
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue