Skip to content

Commit 7e735d2

Browse files
authored
Merge pull request avast#114 from FarmerChillax/fix-onretry-count
Fix the inconsistency in the number of times passed in onRetry
2 parents c7fe1f1 + 4826a2b commit 7e735d2

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
@@ -155,8 +155,8 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
155155

156156
lastErr = err
157157

158-
n++
159158
config.onRetry(n, err)
159+
n++
160160
select {
161161
case <-config.timer.After(delay(config, n, err)):
162162
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)