Skip to content

Commit bef6c43

Browse files
authored
Merge pull request #12 from Rican7/bugfix/attempt-number-accuracy
Bugfix - Attempt Number Accuracy
2 parents ee26000 + 90c002c commit bef6c43

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

example_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func Example_httpGetWithStrategies() {
6969

7070
func Example_withBackoffJitter() {
7171
action := func(attempt uint) error {
72+
fmt.Println("attempt", attempt)
73+
7274
return errors.New("something happened")
7375
}
7476

@@ -83,4 +85,11 @@ func Example_withBackoffJitter() {
8385
jitter.Deviation(random, 0.5),
8486
),
8587
)
88+
89+
// Output:
90+
// attempt 1
91+
// attempt 2
92+
// attempt 3
93+
// attempt 4
94+
// attempt 5
8695
}

retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Retry(action Action, strategies ...strategy.Strategy) error {
1717
var err error
1818

1919
for attempt := uint(0); (attempt == 0 || err != nil) && shouldAttempt(attempt, strategies...); attempt++ {
20-
err = action(attempt)
20+
err = action(attempt + 1)
2121
}
2222

2323
return err

retry_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@ func TestRetry(t *testing.T) {
1717
}
1818
}
1919

20+
func TestRetryAttemptNumberIsAccurate(t *testing.T) {
21+
var strategyAttemptNumber uint
22+
var actionAttemptNumber uint
23+
24+
strategy := func(attempt uint) bool {
25+
strategyAttemptNumber = attempt
26+
27+
return true
28+
}
29+
30+
action := func(attempt uint) error {
31+
actionAttemptNumber = attempt
32+
33+
return nil
34+
}
35+
36+
err := Retry(action, strategy)
37+
38+
if err != nil {
39+
t.Error("expected a nil error")
40+
}
41+
42+
if strategyAttemptNumber != 0 {
43+
t.Errorf(
44+
"expected strategy to receive 0, received %v instead",
45+
strategyAttemptNumber,
46+
)
47+
}
48+
49+
if actionAttemptNumber != 1 {
50+
t.Errorf(
51+
"expected action to receive 1, received %v instead",
52+
actionAttemptNumber,
53+
)
54+
}
55+
}
56+
2057
func TestRetryRetriesUntilNoErrorReturned(t *testing.T) {
2158
const errorUntilAttemptNumber = 5
2259

strategy/strategy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ import (
1515
// allows for the next attempt to be made. Returning `false` halts the retrying
1616
// process and returns the last error returned by the called Action.
1717
//
18-
// The strategy will be passed an "attempt" number on each successive retry
18+
// The strategy will be passed an "attempt" number before each successive retry
1919
// iteration, starting with a `0` value before the first attempt is actually
20-
// made. This allows for a pre-action delay, etc.
20+
// made. This allows for a pre-action, such as a delay, etc.
2121
type Strategy func(attempt uint) bool
2222

2323
// Limit creates a Strategy that limits the number of attempts that Retry will
2424
// make.
2525
func Limit(attemptLimit uint) Strategy {
2626
return func(attempt uint) bool {
27-
return (attempt <= attemptLimit)
27+
return (attempt < attemptLimit)
2828
}
2929
}
3030

strategy/strategy_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ import (
1010
const timeMarginOfError = time.Millisecond
1111

1212
func TestLimit(t *testing.T) {
13+
// Strategy attempts are 0-based.
14+
// Treat this functionally as n+1.
1315
const attemptLimit = 3
1416

1517
strategy := Limit(attemptLimit)
1618

17-
if !strategy(1) {
19+
if !strategy(0) {
1820
t.Error("strategy expected to return true")
1921
}
2022

21-
if !strategy(2) {
23+
if !strategy(1) {
2224
t.Error("strategy expected to return true")
2325
}
2426

25-
if !strategy(3) {
27+
if !strategy(2) {
2628
t.Error("strategy expected to return true")
2729
}
2830

29-
if strategy(4) {
31+
if strategy(3) {
3032
t.Error("strategy expected to return false")
3133
}
3234
}

0 commit comments

Comments
 (0)