]> Cypherpunks repositories - gostls13.git/commitdiff
time: deflake TestTicker
authorIan Lance Taylor <iant@golang.org>
Mon, 18 Nov 2019 22:20:34 +0000 (14:20 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 19 Nov 2019 02:36:43 +0000 (02:36 +0000)
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>
src/time/tick_test.go

index dd17aab1b15364d53f91d25693938669156bf605..92fb2f91fb7761c293718631cb337c934ac0ad7b 100644 (file)
@@ -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
-       }
-       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)
+       // 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)
+               }
        }
-       // 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