Skip to content

Commit

Permalink
fix: zero attempt should return error when RetryIf returns false
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhiquan committed Feb 12, 2023
1 parent 0407c19 commit f2a78f2
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 f2a78f2

Please sign in to comment.