2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-04-29 16:33:17 +00:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
2021-04-23 21:01:05 +00:00
|
|
|
"fmt"
|
2017-04-29 16:33:17 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2021-04-23 20:47:22 +00:00
|
|
|
|
2023-02-21 18:48:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-04-23 20:47:22 +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-23 20:47:22 +00:00
|
|
|
var iters int
|
2017-04-29 16:33:17 +00:00
|
|
|
start := time.Now()
|
2021-04-23 20:47:22 +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-23 20:47:22 +00:00
|
|
|
|
2023-02-21 18:48:25 +00:00
|
|
|
func TestBasics(t *testing.T) {
|
|
|
|
t.Run("Error allows retry", func(t *testing.T) {
|
|
|
|
i := 0
|
|
|
|
Run(t, func(r *R) {
|
|
|
|
i++
|
|
|
|
t.Logf("i: %d; r: %#v", i, r)
|
|
|
|
if i == 1 {
|
|
|
|
r.Errorf("Errorf, i: %d", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert.Equal(t, i, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Fatal returns from func, but does not fail test", func(t *testing.T) {
|
|
|
|
i := 0
|
|
|
|
gotHere := false
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2023-02-21 18:48:25 +00:00
|
|
|
Run(ft, func(r *R) {
|
|
|
|
i++
|
|
|
|
t.Logf("i: %d; r: %#v", i, r)
|
|
|
|
if i == 1 {
|
|
|
|
r.Fatalf("Fatalf, i: %d", i)
|
|
|
|
gotHere = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.False(t, gotHere)
|
|
|
|
assert.Equal(t, i, 2)
|
|
|
|
// surprisingly, r.FailNow() *does not* trigger ft.FailNow()!
|
|
|
|
assert.Equal(t, ft.fails, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Func being run can panic with struct{}{}", func(t *testing.T) {
|
|
|
|
gotPanic := false
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
gotPanic = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
Run(t, func(r *R) {
|
|
|
|
panic(struct{}{})
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
assert.True(t, gotPanic)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-23 20:47:22 +00:00
|
|
|
func TestRunWith(t *testing.T) {
|
|
|
|
t.Run("calls FailNow after exceeding retries", func(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2021-04-23 20:47:22 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-04-23 21:01:05 +00:00
|
|
|
|
|
|
|
t.Run("Stop ends the retrying", func(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2021-04-23 21:01:05 +00:00
|
|
|
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")
|
|
|
|
})
|
|
|
|
|
2023-02-21 18:48:25 +00:00
|
|
|
// TODO: these should all be assert
|
2021-04-23 21:01:05 +00:00
|
|
|
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")
|
|
|
|
})
|
2021-04-23 20:47:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
func TestCleanup_Passthrough(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
func TestCleanup(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
t.Run("basic", func(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2023-06-16 20:29:50 +00:00
|
|
|
cleanupsExecuted := 0
|
2023-12-06 17:11:32 +00:00
|
|
|
|
|
|
|
Run(
|
|
|
|
ft,
|
|
|
|
func(r *R) {
|
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
})
|
|
|
|
},
|
|
|
|
WithImmediateCleanup(),
|
|
|
|
WithRetryer(&Counter{Count: 2, Wait: time.Millisecond}),
|
|
|
|
)
|
2023-06-16 20:29:50 +00:00
|
|
|
|
|
|
|
require.Equal(t, 0, ft.fails)
|
|
|
|
require.Equal(t, 1, cleanupsExecuted)
|
|
|
|
})
|
|
|
|
t.Run("cleanup-panic-recovery", func(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2023-06-16 20:29:50 +00:00
|
|
|
cleanupsExecuted := 0
|
2023-12-06 17:11:32 +00:00
|
|
|
Run(
|
|
|
|
ft,
|
|
|
|
func(r *R) {
|
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
})
|
2023-06-16 20:29:50 +00:00
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
panic(fmt.Errorf("fake test error"))
|
|
|
|
})
|
2023-06-16 20:29:50 +00:00
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
})
|
2023-06-16 20:29:50 +00:00
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
// test is successful but should fail due to the cleanup panicing
|
|
|
|
},
|
|
|
|
WithRetryer(&Counter{Count: 2, Wait: time.Millisecond}),
|
|
|
|
WithImmediateCleanup(),
|
|
|
|
)
|
2023-06-16 20:29:50 +00:00
|
|
|
|
|
|
|
require.Equal(t, 3, cleanupsExecuted)
|
|
|
|
require.Equal(t, 1, ft.fails)
|
|
|
|
require.Contains(t, ft.out[0], "fake test error")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cleanup-per-retry", func(t *testing.T) {
|
2023-12-06 17:11:32 +00:00
|
|
|
ft := &fakeT{T: t}
|
2023-06-16 20:29:50 +00:00
|
|
|
iter := 0
|
|
|
|
cleanupsExecuted := 0
|
2023-12-06 17:11:32 +00:00
|
|
|
Run(
|
|
|
|
ft,
|
|
|
|
func(r *R) {
|
|
|
|
if cleanupsExecuted != iter {
|
|
|
|
r.Stop(fmt.Errorf("cleanups not executed between retries"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
iter += 1
|
2023-06-16 20:29:50 +00:00
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
})
|
2023-06-16 20:29:50 +00:00
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
r.FailNow()
|
|
|
|
},
|
|
|
|
WithRetryer(&Counter{Count: 3, Wait: time.Millisecond}),
|
|
|
|
WithImmediateCleanup(),
|
|
|
|
)
|
2023-06-16 20:29:50 +00:00
|
|
|
|
|
|
|
require.Equal(t, 3, cleanupsExecuted)
|
|
|
|
// ensure that r.Stop hadn't been called. If it was then we would
|
|
|
|
// have log output
|
|
|
|
require.Len(t, ft.out, 0)
|
|
|
|
})
|
2023-12-06 17:11:32 +00:00
|
|
|
|
|
|
|
t.Run("passthrough-to-t", func(t *testing.T) {
|
|
|
|
cleanupsExecuted := 0
|
|
|
|
|
|
|
|
require.True(t, t.Run("internal", func(t *testing.T) {
|
|
|
|
iter := 0
|
|
|
|
Run(
|
|
|
|
t,
|
|
|
|
func(r *R) {
|
|
|
|
iter++
|
|
|
|
|
|
|
|
r.Cleanup(func() {
|
|
|
|
cleanupsExecuted += 1
|
|
|
|
})
|
|
|
|
|
|
|
|
// fail all but the last one to ensure the right number of cleanups
|
|
|
|
// are eventually executed
|
|
|
|
if iter < 3 {
|
|
|
|
r.FailNow()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WithRetryer(&Counter{Count: 3, Wait: time.Millisecond}),
|
|
|
|
)
|
|
|
|
|
|
|
|
// at this point nothing should be cleaned up
|
|
|
|
require.Equal(t, 0, cleanupsExecuted)
|
|
|
|
}))
|
|
|
|
|
|
|
|
// now since the subtest finished the test cleanup funcs
|
|
|
|
// should have been executed.
|
|
|
|
require.Equal(t, 3, cleanupsExecuted)
|
|
|
|
})
|
2023-06-16 20:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 20:47:22 +00:00
|
|
|
type fakeT struct {
|
|
|
|
fails int
|
2021-04-23 21:01:05 +00:00
|
|
|
out []string
|
2023-12-06 17:11:32 +00:00
|
|
|
*testing.T
|
2021-04-23 20:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeT) Helper() {}
|
|
|
|
|
|
|
|
func (f *fakeT) Log(args ...interface{}) {
|
2021-04-23 21:01:05 +00:00
|
|
|
f.out = append(f.out, fmt.Sprint(args...))
|
2021-04-23 20:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeT) FailNow() {
|
|
|
|
f.fails++
|
|
|
|
}
|
|
|
|
|
2023-12-06 17:11:32 +00:00
|
|
|
var _ TestingTB = &fakeT{}
|