Skip to content

Commit 8e4a5f3

Browse files
authored
Merge branch 'master' into no-preallocation
2 parents d1bbd8c + a9a7017 commit 8e4a5f3

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/avast/retry-go/v4
22

33
go 1.13
44

5-
require github.com/stretchr/testify v1.8.1
5+
require github.com/stretchr/testify v1.8.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
88
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
99
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1010
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1313
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1414
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1515
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

retry.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
9797
// Setting attempts to 0 means we'll retry until we succeed
9898
if config.attempts == 0 {
9999
for err := retryableFunc(); err != nil; err = retryableFunc() {
100-
n++
101-
102100
if !IsRecoverable(err) {
103101
return err
104102
}
105103

104+
if !config.retryIf(err) {
105+
return err
106+
}
107+
108+
n++
106109
config.onRetry(n, err)
107110
select {
108111
case <-config.timer.After(delay(config, n, err)):
@@ -262,8 +265,13 @@ func Unrecoverable(err error) error {
262265

263266
// IsRecoverable checks if error is an instance of `unrecoverableError`
264267
func IsRecoverable(err error) bool {
268+
return !errors.Is(err, unrecoverableError{})
269+
}
270+
271+
// Adds support for errors.Is usage on unrecoverableError
272+
func (unrecoverableError) Is(err error) bool {
265273
_, isUnrecoverable := err.(unrecoverableError)
266-
return !isUnrecoverable
274+
return isUnrecoverable
267275
}
268276

269277
func unpackUnrecoverable(err error) error {

retry_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,29 @@ func TestRetryIf(t *testing.T) {
7272
assert.Len(t, err, 3)
7373
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
7474
assert.Equal(t, uint(2), retryCount, "right count of retry")
75+
}
76+
77+
func TestRetryIf_ZeroAttempts(t *testing.T) {
78+
var retryCount uint
79+
err := Do(
80+
func() error {
81+
if retryCount >= 2 {
82+
return errors.New("special")
83+
} else {
84+
return errors.New("test")
85+
}
86+
},
87+
OnRetry(func(n uint, err error) { retryCount++ }),
88+
RetryIf(func(err error) bool {
89+
return err.Error() != "special"
90+
}),
91+
Delay(time.Nanosecond),
92+
Attempts(0),
93+
)
94+
assert.Error(t, err)
7595

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

78100
func TestZeroAttemptsWithError(t *testing.T) {
@@ -504,6 +526,7 @@ func TestUnwrap(t *testing.T) {
504526
assert.Equal(t, testError, errors.Unwrap(err))
505527
}
506528

529+
507530
func BenchmarkDo(b *testing.B) {
508531
testError := errors.New("test error")
509532

@@ -529,3 +552,14 @@ func BenchmarkDoNoErrors(b *testing.B) {
529552
)
530553
}
531554
}
555+
556+
func TestIsRecoverable(t *testing.T) {
557+
err := errors.New("err")
558+
assert.True(t, IsRecoverable(err))
559+
560+
err = Unrecoverable(err)
561+
assert.False(t, IsRecoverable(err))
562+
563+
err = fmt.Errorf("wrapping: %w", err)
564+
assert.False(t, IsRecoverable(err))
565+
}

0 commit comments

Comments
 (0)