2017-04-29 16:33:17 +00:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
2021-04-28 21:31:42 +00:00
|
|
|
"fmt"
|
2017-04-29 16:33:17 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2021-04-28 21:31:42 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-29 16:33:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// delta defines the time band a test run should complete in.
|
2017-06-30 23:02:51 +00:00
|
|
|
var delta = 25 * time.Millisecond
|
2017-04-29 16:33:17 +00:00
|
|
|
|
|
|
|
func TestRetryer(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
r Retryer
|
|
|
|
}{
|
2017-06-30 23:02:51 +00:00
|
|
|
{"counter", &Counter{Count: 3, Wait: 100 * time.Millisecond}},
|
|
|
|
{"timer", &Timer{Timeout: 200 * time.Millisecond, Wait: 100 * time.Millisecond}},
|
2017-04-29 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
2021-04-28 21:31:42 +00:00
|
|
|
var iters int
|
2017-04-29 16:33:17 +00:00
|
|
|
start := time.Now()
|
2021-04-28 21:31:42 +00:00
|
|
|
for tt.r.Continue() {
|
2017-04-29 16:33:17 +00:00
|
|
|
iters++
|
|
|
|
}
|
|
|
|
dur := time.Since(start)
|
|
|
|
if got, want := iters, 3; got != want {
|
|
|
|
t.Fatalf("got %d retries 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)
|
2017-06-30 23:02:51 +00:00
|
|
|
if got, want := dur, 200*time.Millisecond; got < (want-delta) || got > (want+delta) {
|
2017-04-29 16:33:17 +00:00
|
|
|
t.Fatalf("loop took %v want %v (+/- %v)", got, want, delta)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 21:31:42 +00:00
|
|
|
|
|
|
|
func TestRunWith(t *testing.T) {
|
|
|
|
t.Run("calls FailNow after exceeding retries", func(t *testing.T) {
|
|
|
|
ft := &fakeT{}
|
|
|
|
iter := 0
|
|
|
|
RunWith(&Counter{Count: 3, Wait: time.Millisecond}, ft, func(r *R) {
|
|
|
|
iter++
|
|
|
|
r.FailNow()
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, 3, iter)
|
|
|
|
require.Equal(t, 1, ft.fails)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Stop ends the retrying", func(t *testing.T) {
|
|
|
|
ft := &fakeT{}
|
|
|
|
iter := 0
|
|
|
|
RunWith(&Counter{Count: 5, Wait: time.Millisecond}, ft, func(r *R) {
|
|
|
|
iter++
|
|
|
|
if iter == 2 {
|
|
|
|
r.Stop(fmt.Errorf("do not proceed"))
|
|
|
|
}
|
|
|
|
r.Fatalf("not yet")
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, 2, iter)
|
|
|
|
require.Equal(t, 1, ft.fails)
|
|
|
|
require.Len(t, ft.out, 1)
|
|
|
|
require.Contains(t, ft.out[0], "not yet\n")
|
|
|
|
require.Contains(t, ft.out[0], "do not proceed\n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeT struct {
|
|
|
|
fails int
|
|
|
|
out []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeT) Helper() {}
|
|
|
|
|
|
|
|
func (f *fakeT) Log(args ...interface{}) {
|
|
|
|
f.out = append(f.out, fmt.Sprint(args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeT) FailNow() {
|
|
|
|
f.fails++
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Failer = &fakeT{}
|