Skip to content

Commit 4826a2b

Browse files
committed
fix: Fix the inconsistency in the number of times passed in onRetry
1 parent 64083c1 commit 4826a2b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
154154

155155
lastErr = err
156156

157-
n++
158157
config.onRetry(n, err)
158+
n++
159159
select {
160160
case <-config.timer.After(delay(config, n, err)):
161161
case <-config.context.Done():

retry_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestDoWithDataAllFailed(t *testing.T) {
3333
#9: test
3434
#10: test`
3535
assert.Len(t, err, 10)
36+
fmt.Println(err.Error())
3637
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
3738
assert.Equal(t, uint(45), retrySum, "right count of retry")
3839
}
@@ -88,16 +89,17 @@ func TestRetryIf(t *testing.T) {
8889
}
8990

9091
func TestRetryIf_ZeroAttempts(t *testing.T) {
91-
var retryCount uint
92+
var retryCount, onRetryCount uint
9293
err := Do(
9394
func() error {
9495
if retryCount >= 2 {
9596
return errors.New("special")
9697
} else {
98+
retryCount++
9799
return errors.New("test")
98100
}
99101
},
100-
OnRetry(func(n uint, err error) { retryCount++ }),
102+
OnRetry(func(n uint, err error) { onRetryCount = n }),
101103
RetryIf(func(err error) bool {
102104
return err.Error() != "special"
103105
}),
@@ -107,7 +109,7 @@ func TestRetryIf_ZeroAttempts(t *testing.T) {
107109
assert.Error(t, err)
108110

109111
assert.Equal(t, "special", err.Error(), "retry error format")
110-
assert.Equal(t, uint(2), retryCount, "right count of retry")
112+
assert.Equal(t, retryCount, onRetryCount+1, "right count of retry")
111113
}
112114

113115
func TestZeroAttemptsWithError(t *testing.T) {

0 commit comments

Comments
 (0)