2017-04-29 16:33:17 +00:00
|
|
|
// Package retry provides support for repeating operations in tests.
|
|
|
|
//
|
|
|
|
// A sample retry operation looks like this:
|
|
|
|
//
|
2022-10-21 19:58:06 +00:00
|
|
|
// func TestX(t *testing.T) {
|
|
|
|
// retry.Run(t, func(r *retry.R) {
|
|
|
|
// if err := foo(); err != nil {
|
|
|
|
// r.Fatal("f: ", err)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
2017-04-29 16:33:17 +00:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Failer is an interface compatible with testing.T.
|
|
|
|
type Failer interface {
|
2020-08-14 20:54:44 +00:00
|
|
|
Helper()
|
|
|
|
|
2017-04-29 16:33:17 +00:00
|
|
|
// Log is called for the final test output
|
|
|
|
Log(args ...interface{})
|
|
|
|
|
|
|
|
// FailNow is called when the retrying is abandoned.
|
|
|
|
FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// R provides context for the retryer.
|
|
|
|
type R struct {
|
|
|
|
fail bool
|
2021-04-23 21:01:05 +00:00
|
|
|
done bool
|
2017-04-29 16:33:17 +00:00
|
|
|
output []string
|
|
|
|
}
|
|
|
|
|
2022-12-15 17:52:48 +00:00
|
|
|
func (r *R) Logf(format string, args ...interface{}) {
|
|
|
|
r.log(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:04:38 +00:00
|
|
|
func (r *R) Helper() {}
|
|
|
|
|
|
|
|
var runFailed = struct{}{}
|
|
|
|
|
2017-04-29 16:33:17 +00:00
|
|
|
func (r *R) FailNow() {
|
|
|
|
r.fail = true
|
2021-04-21 21:04:38 +00:00
|
|
|
panic(runFailed)
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *R) Fatal(args ...interface{}) {
|
|
|
|
r.log(fmt.Sprint(args...))
|
|
|
|
r.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *R) Fatalf(format string, args ...interface{}) {
|
|
|
|
r.log(fmt.Sprintf(format, args...))
|
|
|
|
r.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *R) Error(args ...interface{}) {
|
|
|
|
r.log(fmt.Sprint(args...))
|
|
|
|
r.fail = true
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (r *R) Errorf(format string, args ...interface{}) {
|
|
|
|
r.log(fmt.Sprintf(format, args...))
|
|
|
|
r.fail = true
|
|
|
|
}
|
|
|
|
|
2017-05-05 07:23:28 +00:00
|
|
|
func (r *R) Check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
r.log(err.Error())
|
|
|
|
r.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 16:33:17 +00:00
|
|
|
func (r *R) log(s string) {
|
|
|
|
r.output = append(r.output, decorate(s))
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:01:05 +00:00
|
|
|
// Stop retrying, and fail the test with the specified error.
|
|
|
|
func (r *R) Stop(err error) {
|
|
|
|
r.log(err.Error())
|
|
|
|
r.done = true
|
|
|
|
}
|
|
|
|
|
2017-04-29 16:33:17 +00:00
|
|
|
func decorate(s string) string {
|
|
|
|
_, file, line, ok := runtime.Caller(3)
|
|
|
|
if ok {
|
|
|
|
n := strings.LastIndex(file, "/")
|
|
|
|
if n >= 0 {
|
|
|
|
file = file[n+1:]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file = "???"
|
|
|
|
line = 1
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d: %s", file, line, s)
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
func Run(t Failer, f func(r *R)) {
|
2021-05-10 17:20:45 +00:00
|
|
|
t.Helper()
|
2018-02-08 01:29:30 +00:00
|
|
|
run(DefaultFailer(), t, f)
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
func RunWith(r Retryer, t Failer, f func(r *R)) {
|
2021-04-21 21:04:38 +00:00
|
|
|
t.Helper()
|
2017-05-04 22:52:53 +00:00
|
|
|
run(r, t, f)
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dedup(a []string) string {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2021-04-21 21:04:38 +00:00
|
|
|
seen := map[string]struct{}{}
|
2017-04-29 16:33:17 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
for _, s := range a {
|
2021-04-21 21:04:38 +00:00
|
|
|
if _, ok := seen[s]; ok {
|
|
|
|
continue
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
2021-04-21 21:04:38 +00:00
|
|
|
seen[s] = struct{}{}
|
|
|
|
b.WriteString(s)
|
|
|
|
b.WriteRune('\n')
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
2019-07-20 13:37:19 +00:00
|
|
|
return b.String()
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
func run(r Retryer, t Failer, f func(r *R)) {
|
2021-04-21 21:04:38 +00:00
|
|
|
t.Helper()
|
2017-04-29 16:33:17 +00:00
|
|
|
rr := &R{}
|
2021-04-23 21:01:05 +00:00
|
|
|
|
|
|
|
fail := func() {
|
|
|
|
t.Helper()
|
|
|
|
out := dedup(rr.output)
|
|
|
|
if out != "" {
|
|
|
|
t.Log(out)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2021-04-23 20:47:22 +00:00
|
|
|
for r.Continue() {
|
2021-04-21 21:04:38 +00:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil && p != runFailed {
|
|
|
|
panic(p)
|
|
|
|
}
|
|
|
|
}()
|
2017-04-29 16:33:17 +00:00
|
|
|
f(rr)
|
|
|
|
}()
|
2021-04-23 21:01:05 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case rr.done:
|
|
|
|
fail()
|
|
|
|
return
|
|
|
|
case !rr.fail:
|
2021-04-21 21:04:38 +00:00
|
|
|
return
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
2021-04-21 21:04:38 +00:00
|
|
|
rr.fail = false
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
2021-04-23 21:01:05 +00:00
|
|
|
fail()
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 01:29:30 +00:00
|
|
|
// DefaultFailer provides default retry.Run() behavior for unit tests.
|
|
|
|
func DefaultFailer() *Timer {
|
|
|
|
return &Timer{Timeout: 7 * time.Second, Wait: 25 * time.Millisecond}
|
|
|
|
}
|
|
|
|
|
2017-05-09 04:57:06 +00:00
|
|
|
// TwoSeconds repeats an operation for two seconds and waits 25ms in between.
|
|
|
|
func TwoSeconds() *Timer {
|
|
|
|
return &Timer{Timeout: 2 * time.Second, Wait: 25 * time.Millisecond}
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ThreeTimes repeats an operation three times and waits 25ms in between.
|
|
|
|
func ThreeTimes() *Counter {
|
|
|
|
return &Counter{Count: 3, Wait: 25 * time.Millisecond}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retryer provides an interface for repeating operations
|
|
|
|
// until they succeed or an exit condition is met.
|
|
|
|
type Retryer interface {
|
2021-04-28 16:01:04 +00:00
|
|
|
// Continue returns true if the operation should be repeated, otherwise it
|
2021-04-23 20:47:22 +00:00
|
|
|
// returns false to indicate retrying should stop.
|
|
|
|
Continue() bool
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Counter repeats an operation a given number of
|
|
|
|
// times and waits between subsequent operations.
|
|
|
|
type Counter struct {
|
|
|
|
Count int
|
|
|
|
Wait time.Duration
|
|
|
|
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
2021-04-23 20:47:22 +00:00
|
|
|
func (r *Counter) Continue() bool {
|
2017-04-29 16:33:17 +00:00
|
|
|
if r.count == r.Count {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.count > 0 {
|
|
|
|
time.Sleep(r.Wait)
|
|
|
|
}
|
|
|
|
r.count++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer repeats an operation for a given amount
|
|
|
|
// of time and waits between subsequent operations.
|
|
|
|
type Timer struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
Wait time.Duration
|
|
|
|
|
|
|
|
// stop is the timeout deadline.
|
|
|
|
// Set on the first invocation of Next().
|
|
|
|
stop time.Time
|
|
|
|
}
|
|
|
|
|
2021-04-23 20:47:22 +00:00
|
|
|
func (r *Timer) Continue() bool {
|
2017-04-29 16:33:17 +00:00
|
|
|
if r.stop.IsZero() {
|
|
|
|
r.stop = time.Now().Add(r.Timeout)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if time.Now().After(r.stop) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
time.Sleep(r.Wait)
|
|
|
|
return true
|
|
|
|
}
|