Skip to content

Commit

Permalink
Merge pull request #86 from lizhiquan/fix-66
Browse files Browse the repository at this point in the history
fix: zero attempt should return error when `RetryIf` returns false
  • Loading branch information
JaSei authored Apr 21, 2023
2 parents d82cd49 + f2a78f2 commit a9a7017
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
// Setting attempts to 0 means we'll retry until we succeed
if config.attempts == 0 {
for err := retryableFunc(); err != nil; err = retryableFunc() {
n++

if !IsRecoverable(err) {
return err
}

if !config.retryIf(err) {
return err
}

n++
config.onRetry(n, err)
select {
case <-config.timer.After(delay(config, n, err)):
Expand Down
22 changes: 22 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ func TestRetryIf(t *testing.T) {
assert.Len(t, err, 3)
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(2), retryCount, "right count of retry")
}

func TestRetryIf_ZeroAttempts(t *testing.T) {
var retryCount uint
err := Do(
func() error {
if retryCount >= 2 {
return errors.New("special")
} else {
return errors.New("test")
}
},
OnRetry(func(n uint, err error) { retryCount++ }),
RetryIf(func(err error) bool {
return err.Error() != "special"
}),
Delay(time.Nanosecond),
Attempts(0),
)
assert.Error(t, err)

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

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

0 comments on commit a9a7017

Please sign in to comment.