|
|
@@ -21,9 +21,9 @@ func TestRunWithRetryFirstAttemptSucceeds(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
return nil
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool {
|
|
|
|
func(error) bool {
|
|
|
|
t.Fatal("shouldRetry called after successful attempt")
|
|
|
|
t.Fatal("shouldRetry called after successful attempt")
|
|
|
@@ -52,12 +52,12 @@ func TestRunWithRetryFailuresThenSuccess(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
if attempts < 3 {
|
|
|
|
if attempts < 3 {
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(_ context.Context, delay time.Duration) error {
|
|
|
|
func(_ context.Context, delay time.Duration) error {
|
|
|
@@ -87,9 +87,9 @@ func TestRunWithRetryFiniteAttemptsExhausted(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
@@ -117,12 +117,12 @@ func TestRunWithRetryUnlimitedEventuallySucceeds(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
if attempts < 20 {
|
|
|
|
if attempts < 20 {
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
@@ -144,9 +144,9 @@ func TestRunWithRetryStopsWhenErrorIsNotRetryable(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return false },
|
|
|
|
func(error) bool { return false },
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
@@ -171,9 +171,9 @@ func TestRunWithRetryReturnsCancellationFromAttempt(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
ctx,
|
|
|
|
ctx,
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
cancel()
|
|
|
|
cancel()
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
|
func(context.Context, time.Duration) error {
|
|
|
@@ -195,7 +195,7 @@ func TestRunWithRetryReturnsCancellationDuringBackoff(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
ctx,
|
|
|
|
ctx,
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error { return attemptErr },
|
|
|
|
func(context.Context) (bool, error) { return false, attemptErr },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(ctx context.Context, _ time.Duration) error {
|
|
|
|
func(ctx context.Context, _ time.Duration) error {
|
|
|
|
cancel()
|
|
|
|
cancel()
|
|
|
@@ -216,7 +216,7 @@ func TestRunWithRetryReturnsWaitError(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error { return attemptErr },
|
|
|
|
func(context.Context) (bool, error) { return false, attemptErr },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return waitErr },
|
|
|
|
func(context.Context, time.Duration) error { return waitErr },
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
@@ -245,12 +245,12 @@ func TestRetryObserverReportsFailuresBeforeSuccess(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
attempts++
|
|
|
|
attempts++
|
|
|
|
if attempts < 3 {
|
|
|
|
if attempts < 3 {
|
|
|
|
return attemptErr
|
|
|
|
return false, attemptErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
@@ -291,7 +291,7 @@ func TestRetryObserverReportsExhaustion(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(2),
|
|
|
|
testRetryPolicy(2),
|
|
|
|
func(context.Context) error { return attemptErr },
|
|
|
|
func(context.Context) (bool, error) { return false, attemptErr },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(event retryEvent) {
|
|
|
|
func(event retryEvent) {
|
|
|
@@ -328,7 +328,7 @@ func TestRetryObserverNotCalledOnImmediateSuccess(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
context.Background(),
|
|
|
|
context.Background(),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
testRetryPolicy(3),
|
|
|
|
func(context.Context) error { return nil },
|
|
|
|
func(context.Context) (bool, error) { return false, nil },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(retryEvent) { observerCalls++ },
|
|
|
|
func(retryEvent) { observerCalls++ },
|
|
|
@@ -349,9 +349,9 @@ func TestRetryObserverNotCalledWhenAttemptCancelsContext(t *testing.T) {
|
|
|
|
err := runWithRetry(
|
|
|
|
err := runWithRetry(
|
|
|
|
ctx,
|
|
|
|
ctx,
|
|
|
|
testRetryPolicy(0),
|
|
|
|
testRetryPolicy(0),
|
|
|
|
func(context.Context) error {
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
cancel()
|
|
|
|
cancel()
|
|
|
|
return errors.New("attempt interrupted")
|
|
|
|
return false, errors.New("attempt interrupted")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(error) bool { return true },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
@@ -365,3 +365,61 @@ func TestRetryObserverNotCalledWhenAttemptCancelsContext(t *testing.T) {
|
|
|
|
t.Fatalf("observer call count = %d, want 0", observerCalls)
|
|
|
|
t.Fatalf("observer call count = %d, want 0", observerCalls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestRunWithRetryResetsFailuresAfterStableAttempt(t *testing.T) {
|
|
|
|
|
|
|
|
attemptErr := errors.New("attempt failed")
|
|
|
|
|
|
|
|
attempts := 0
|
|
|
|
|
|
|
|
var events []retryEvent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := runWithRetry(
|
|
|
|
|
|
|
|
context.Background(),
|
|
|
|
|
|
|
|
testRetryPolicy(2),
|
|
|
|
|
|
|
|
func(context.Context) (bool, error) {
|
|
|
|
|
|
|
|
attempts++
|
|
|
|
|
|
|
|
switch attempts {
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
return false, attemptErr
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
return true, attemptErr
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return false, attemptErr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
func(error) bool { return true },
|
|
|
|
|
|
|
|
func(context.Context, time.Duration) error { return nil },
|
|
|
|
|
|
|
|
func(event retryEvent) {
|
|
|
|
|
|
|
|
events = append(events, event)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !errors.Is(err, attemptErr) {
|
|
|
|
|
|
|
|
t.Fatalf("runWithRetry() error = %v, want %v", err, attemptErr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if attempts != 3 {
|
|
|
|
|
|
|
|
t.Fatalf("attempt count = %d, want 3", attempts)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(events) != 3 {
|
|
|
|
|
|
|
|
t.Fatalf("event count = %d, want 3", len(events))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wantFailedAttempts := []int{1, 1, 2}
|
|
|
|
|
|
|
|
wantWillRetry := []bool{true, true, false}
|
|
|
|
|
|
|
|
for i, event := range events {
|
|
|
|
|
|
|
|
if event.FailedAttempts != wantFailedAttempts[i] {
|
|
|
|
|
|
|
|
t.Errorf(
|
|
|
|
|
|
|
|
"event %d failed attempts = %d, want %d",
|
|
|
|
|
|
|
|
i,
|
|
|
|
|
|
|
|
event.FailedAttempts,
|
|
|
|
|
|
|
|
wantFailedAttempts[i],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.WillRetry != wantWillRetry[i] {
|
|
|
|
|
|
|
|
t.Errorf(
|
|
|
|
|
|
|
|
"event %d WillRetry = %t, want %t",
|
|
|
|
|
|
|
|
i,
|
|
|
|
|
|
|
|
event.WillRetry,
|
|
|
|
|
|
|
|
wantWillRetry[i],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|