Skip to content

Commit

Permalink
Fixing the limit test and improving an example
Browse files Browse the repository at this point in the history
Just now realizing that the limit strategy has had an off-by-one error
for 5+ years now...

Not sure what I was thinking here:
678601c
  • Loading branch information
Rican7 committed Aug 1, 2021
1 parent 552c087 commit 55b1410
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func Example_httpGetWithStrategies() {

func Example_withBackoffJitter() {
action := func(attempt uint) error {
fmt.Println("attempt", attempt)

return errors.New("something happened")
}

Expand All @@ -83,4 +85,11 @@ func Example_withBackoffJitter() {
jitter.Deviation(random, 0.5),
),
)

// Output:
// attempt 1
// attempt 2
// attempt 3
// attempt 4
// attempt 5
}
10 changes: 6 additions & 4 deletions strategy/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import (
const timeMarginOfError = time.Millisecond

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

strategy := Limit(attemptLimit)

if !strategy(1) {
if !strategy(0) {
t.Error("strategy expected to return true")
}

if !strategy(2) {
if !strategy(1) {
t.Error("strategy expected to return true")
}

if !strategy(3) {
if !strategy(2) {
t.Error("strategy expected to return true")
}

if strategy(4) {
if strategy(3) {
t.Error("strategy expected to return false")
}
}
Expand Down

0 comments on commit 55b1410

Please sign in to comment.