reset retries after stable playback

This commit is contained in:
Dmitry Sergeev
2026-08-27 09:44:14 +03:00
parent 418d0fc102
commit 8cb2d0b88f
3 changed files with 102 additions and 26 deletions
+81 -23
View File
@@ -21,9 +21,9 @@ func TestRunWithRetryFirstAttemptSucceeds(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(3),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
return nil
return false, nil
},
func(error) bool {
t.Fatal("shouldRetry called after successful attempt")
@@ -52,12 +52,12 @@ func TestRunWithRetryFailuresThenSuccess(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(3),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
if attempts < 3 {
return attemptErr
return false, attemptErr
}
return nil
return false, nil
},
func(error) bool { return true },
func(_ context.Context, delay time.Duration) error {
@@ -87,9 +87,9 @@ func TestRunWithRetryFiniteAttemptsExhausted(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(3),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
return attemptErr
return false, attemptErr
},
func(error) bool { return true },
func(context.Context, time.Duration) error {
@@ -117,12 +117,12 @@ func TestRunWithRetryUnlimitedEventuallySucceeds(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(0),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
if attempts < 20 {
return attemptErr
return false, attemptErr
}
return nil
return false, nil
},
func(error) bool { return true },
func(context.Context, time.Duration) error { return nil },
@@ -144,9 +144,9 @@ func TestRunWithRetryStopsWhenErrorIsNotRetryable(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(0),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
return attemptErr
return false, attemptErr
},
func(error) bool { return false },
func(context.Context, time.Duration) error {
@@ -171,9 +171,9 @@ func TestRunWithRetryReturnsCancellationFromAttempt(t *testing.T) {
err := runWithRetry(
ctx,
testRetryPolicy(0),
func(context.Context) error {
func(context.Context) (bool, error) {
cancel()
return attemptErr
return false, attemptErr
},
func(error) bool { return true },
func(context.Context, time.Duration) error {
@@ -195,7 +195,7 @@ func TestRunWithRetryReturnsCancellationDuringBackoff(t *testing.T) {
err := runWithRetry(
ctx,
testRetryPolicy(0),
func(context.Context) error { return attemptErr },
func(context.Context) (bool, error) { return false, attemptErr },
func(error) bool { return true },
func(ctx context.Context, _ time.Duration) error {
cancel()
@@ -216,7 +216,7 @@ func TestRunWithRetryReturnsWaitError(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(0),
func(context.Context) error { return attemptErr },
func(context.Context) (bool, error) { return false, attemptErr },
func(error) bool { return true },
func(context.Context, time.Duration) error { return waitErr },
nil,
@@ -245,12 +245,12 @@ func TestRetryObserverReportsFailuresBeforeSuccess(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(3),
func(context.Context) error {
func(context.Context) (bool, error) {
attempts++
if attempts < 3 {
return attemptErr
return false, attemptErr
}
return nil
return false, nil
},
func(error) bool { return true },
func(context.Context, time.Duration) error { return nil },
@@ -291,7 +291,7 @@ func TestRetryObserverReportsExhaustion(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(2),
func(context.Context) error { return attemptErr },
func(context.Context) (bool, error) { return false, attemptErr },
func(error) bool { return true },
func(context.Context, time.Duration) error { return nil },
func(event retryEvent) {
@@ -328,7 +328,7 @@ func TestRetryObserverNotCalledOnImmediateSuccess(t *testing.T) {
err := runWithRetry(
context.Background(),
testRetryPolicy(3),
func(context.Context) error { return nil },
func(context.Context) (bool, error) { return false, nil },
func(error) bool { return true },
func(context.Context, time.Duration) error { return nil },
func(retryEvent) { observerCalls++ },
@@ -349,9 +349,9 @@ func TestRetryObserverNotCalledWhenAttemptCancelsContext(t *testing.T) {
err := runWithRetry(
ctx,
testRetryPolicy(0),
func(context.Context) error {
func(context.Context) (bool, error) {
cancel()
return errors.New("attempt interrupted")
return false, errors.New("attempt interrupted")
},
func(error) bool { return true },
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)
}
}
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],
)
}
}
}