mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
eb6465551b
The current retry framework in testutil/testprc.WaitForResult uses a func() (bool, error) callback until it succeeds or times out. It captures the last error and returns it. if err := testutil.WaitForResult(t, func() (bool, error) { if err := foo(); err != nil { return false, err } ... return true, nil }); err != nil { t.Fatal(err) } This makes the test functions more complex than they need to be since both the boolean and the error indicate a success or a failure. The retry.Run framework uses a an approach similar to t.Run() from the testing framework. retry.Run(t, func(r *retry.R) { if err := foo(); err != nil { r.Fatal(err) } }) The behavior of the Run function is configurable so that different timeouts can be used for different tests.
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package retry
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// delta defines the time band a test run should complete in.
|
|
var delta = 5 * time.Millisecond
|
|
|
|
func TestRetryer(t *testing.T) {
|
|
tests := []struct {
|
|
desc string
|
|
r Retryer
|
|
}{
|
|
{"counter", &Counter{Count: 3, Wait: 10 * time.Millisecond}},
|
|
{"timer", &Timer{Timeout: 20 * time.Millisecond, Wait: 10 * time.Millisecond}},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
var iters, fails int
|
|
fail := func() { fails++ }
|
|
start := time.Now()
|
|
for tt.r.NextOr(fail) {
|
|
iters++
|
|
}
|
|
dur := time.Since(start)
|
|
if got, want := iters, 3; got != want {
|
|
t.Fatalf("got %d retries want %d", got, want)
|
|
}
|
|
if got, want := fails, 1; got != want {
|
|
t.Fatalf("got %d FailNow calls want %d", got, want)
|
|
}
|
|
// since the first iteration happens immediately
|
|
// the retryer waits only twice for three iterations.
|
|
// order of events: (true, (wait) true, (wait) true, false)
|
|
if got, want := dur, 20*time.Millisecond; got < (want-delta) || got > (want+delta) {
|
|
t.Fatalf("loop took %v want %v (+/- %v)", got, want, delta)
|
|
}
|
|
})
|
|
}
|
|
}
|