Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the inconsistency in the number of times passed in onRetry #114

Merged
merged 1 commit into from
Apr 18, 2024
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
2 changes: 1 addition & 1 deletion retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,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
Original file line number Diff line number Diff line change
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
Loading