Skip to content

Commit 1eb3dfc

Browse files
authored
Merge pull request #1 from codeGROOVE-dev/merge_103
Merge avast#103 with test
2 parents cbf4dbf + 485d7e2 commit 1eb3dfc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

retry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
188188
break
189189
}
190190

191-
config.onRetry(n, err)
192-
193191
for errToCheck, attempts := range attemptsForError {
194192
if errors.Is(err, errToCheck) {
195193
attempts--
@@ -202,6 +200,9 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
202200
if n == config.attempts-1 {
203201
break
204202
}
203+
204+
config.onRetry(n, err)
205+
205206
n++
206207
select {
207208
case <-config.timer.After(delay(config, n, err)):

retry_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestDoWithDataAllFailed(t *testing.T) {
3535
assert.Len(t, err, 10)
3636
fmt.Println(err.Error())
3737
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
38-
assert.Equal(t, uint(45), retrySum, "right count of retry")
38+
assert.Equal(t, uint(36), retrySum, "right count of retry")
3939
}
4040

4141
func TestDoFirstOk(t *testing.T) {
@@ -632,6 +632,28 @@ func BenchmarkDoWithDataNoErrors(b *testing.B) {
632632
}
633633
}
634634

635+
func TestOnRetryNotCalledOnLastAttempt(t *testing.T) {
636+
callCount := 0
637+
onRetryCalls := make([]uint, 0)
638+
639+
err := Do(
640+
func() error {
641+
callCount++
642+
return errors.New("test error")
643+
},
644+
Attempts(3),
645+
OnRetry(func(n uint, err error) {
646+
onRetryCalls = append(onRetryCalls, n)
647+
}),
648+
Delay(time.Nanosecond),
649+
)
650+
651+
assert.Error(t, err)
652+
assert.Equal(t, 3, callCount, "function should be called 3 times")
653+
assert.Equal(t, []uint{0, 1}, onRetryCalls, "onRetry should only be called for first 2 attempts, not the final one")
654+
assert.Len(t, onRetryCalls, 2, "onRetry should be called exactly 2 times (not on last attempt)")
655+
}
656+
635657
func TestIsRecoverable(t *testing.T) {
636658
err := errors.New("err")
637659
assert.True(t, IsRecoverable(err))

0 commit comments

Comments
 (0)