Skip to content

Commit f68b1b5

Browse files
authored
Merge pull request #6 from codeGROOVE-dev/merge_131
merge: Fix: Check Attempts For Error even when Attempts is zero avast#131
2 parents e08f658 + d601269 commit f68b1b5

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

retry.go

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -136,38 +136,6 @@ func DoWithData[T any](retryableFunc RetryableFuncWithData[T], opts ...Option) (
136136
return emptyT, err
137137
}
138138

139-
// Setting attempts to 0 means we'll retry until we succeed
140-
var lastErr error
141-
if config.attempts == 0 {
142-
for {
143-
t, err := retryableFunc()
144-
if err == nil {
145-
return t, nil
146-
}
147-
148-
if !IsRecoverable(err) {
149-
return emptyT, err
150-
}
151-
152-
if !config.retryIf(err) {
153-
return emptyT, err
154-
}
155-
156-
lastErr = err
157-
158-
config.onRetry(n, err)
159-
n++
160-
select {
161-
case <-config.timer.After(delay(config, n, err)):
162-
case <-config.context.Done():
163-
if config.wrapContextErrorWithLastError {
164-
return emptyT, Error{context.Cause(config.context), lastErr}
165-
}
166-
return emptyT, context.Cause(config.context)
167-
}
168-
}
169-
}
170-
171139
errorLog := Error{}
172140

173141
attemptsForError := make(map[error]uint, len(config.attemptsForError))
@@ -184,6 +152,10 @@ shouldRetry:
184152

185153
errorLog = append(errorLog, unpackUnrecoverable(err))
186154

155+
if !IsRecoverable(err) {
156+
return emptyT, err
157+
}
158+
187159
if !config.retryIf(err) {
188160
break
189161
}
@@ -198,6 +170,7 @@ shouldRetry:
198170
}
199171
}
200172

173+
// Setting attempts to 0 means we'll retry until we succeed
201174
// if this is last attempt - don't wait
202175
if n == config.attempts-1 {
203176
break shouldRetry

retry_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func TestRetryIf_ZeroAttempts(t *testing.T) {
105105
return err.Error() != "special"
106106
}),
107107
Delay(time.Nanosecond),
108+
LastErrorOnly(true),
108109
Attempts(0),
109110
)
110111
assert.Error(t, err)
@@ -216,16 +217,15 @@ func TestLastErrorOnly(t *testing.T) {
216217
func TestUnrecoverableError(t *testing.T) {
217218
attempts := 0
218219
testErr := errors.New("error")
219-
expectedErr := Error{testErr}
220220
err := Do(
221221
func() error {
222222
attempts++
223223
return Unrecoverable(testErr)
224224
},
225225
Attempts(2),
226226
)
227-
assert.Equal(t, expectedErr, err)
228-
assert.Equal(t, testErr, errors.Unwrap(err))
227+
assert.Error(t, err)
228+
assert.Equal(t, Unrecoverable(testErr), err)
229229
assert.Equal(t, 1, attempts, "unrecoverable error broke the loop")
230230
}
231231

@@ -465,6 +465,7 @@ func TestContext(t *testing.T) {
465465
cancel()
466466
}
467467
}),
468+
LastErrorOnly(true),
468469
Context(ctx),
469470
Attempts(0),
470471
)

0 commit comments

Comments
 (0)