Skip to content

Commit

Permalink
Merge pull request #114 from FarmerChillax/fix-onretry-count
Browse files Browse the repository at this point in the history
Fix the inconsistency in the number of times passed in onRetry
  • Loading branch information
JaSei committed Apr 18, 2024
2 parents c7fe1f1 + 4826a2b commit 7e735d2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion retry.go
Expand Up @@ -155,8 +155,8 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (

lastErr = err

n++
config.onRetry(n, err)
n++
select {
case <-config.timer.After(delay(config, n, err)):
case <-config.context.Done():
Expand Down
8 changes: 5 additions & 3 deletions retry_test.go
Expand Up @@ -33,6 +33,7 @@ func TestDoWithDataAllFailed(t *testing.T) {
#9: test
#10: test`
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")
}
Expand Down Expand Up @@ -88,16 +89,17 @@ func TestRetryIf(t *testing.T) {
}

func TestRetryIf_ZeroAttempts(t *testing.T) {
var retryCount uint
var retryCount, onRetryCount uint
err := Do(
func() error {
if retryCount >= 2 {
return errors.New("special")
} else {
retryCount++
return errors.New("test")
}
},
OnRetry(func(n uint, err error) { retryCount++ }),
OnRetry(func(n uint, err error) { onRetryCount = n }),
RetryIf(func(err error) bool {
return err.Error() != "special"
}),
Expand All @@ -107,7 +109,7 @@ func TestRetryIf_ZeroAttempts(t *testing.T) {
assert.Error(t, err)

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

func TestZeroAttemptsWithError(t *testing.T) {
Expand Down

0 comments on commit 7e735d2

Please sign in to comment.