Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
break
}

config.onRetry(n, err)

for errToCheck, attempts := range attemptsForError {
if errors.Is(err, errToCheck) {
attempts--
Expand All @@ -202,6 +200,9 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
if n == config.attempts-1 {
break
}

config.onRetry(n, err)

n++
select {
case <-config.timer.After(delay(config, n, err)):
Expand Down
24 changes: 23 additions & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestDoWithDataAllFailed(t *testing.T) {
assert.Len(t, err, 10)
fmt.Println(err.Error())
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(45), retrySum, "right count of retry")
assert.Equal(t, uint(36), retrySum, "right count of retry")
}

func TestDoFirstOk(t *testing.T) {
Expand Down Expand Up @@ -632,6 +632,28 @@ func BenchmarkDoWithDataNoErrors(b *testing.B) {
}
}

func TestOnRetryNotCalledOnLastAttempt(t *testing.T) {
callCount := 0
onRetryCalls := make([]uint, 0)

err := Do(
func() error {
callCount++
return errors.New("test error")
},
Attempts(3),
OnRetry(func(n uint, err error) {
onRetryCalls = append(onRetryCalls, n)
}),
Delay(time.Nanosecond),
)

assert.Error(t, err)
assert.Equal(t, 3, callCount, "function should be called 3 times")
assert.Equal(t, []uint{0, 1}, onRetryCalls, "onRetry should only be called for first 2 attempts, not the final one")
assert.Len(t, onRetryCalls, 2, "onRetry should be called exactly 2 times (not on last attempt)")
}

func TestIsRecoverable(t *testing.T) {
err := errors.New("err")
assert.True(t, IsRecoverable(err))
Expand Down