Skip to content

Commit eead117

Browse files
authored
Deduplicate retry tests a bit (#1173)
These 4 are doing almost exactly the same thing, might as well make it more clear.
1 parent 4817c77 commit eead117

File tree

1 file changed

+46
-85
lines changed

1 file changed

+46
-85
lines changed

internal/common/backoff/retry_test.go

Lines changed: 46 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,56 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
)
3131

32-
func TestRetrySuccess(t *testing.T) {
32+
func TestRetry(t *testing.T) {
3333
t.Parallel()
34-
i := 0
35-
op := func() error {
36-
i++
37-
38-
if i == 5 {
39-
return nil
40-
}
4134

42-
return &someError{}
35+
succeedOnAttemptNum := 5
36+
tests := []struct {
37+
name string
38+
maxAttempts int
39+
isRetryable func(error) bool
40+
41+
shouldError bool
42+
expectedCalls int
43+
}{
44+
{"success", 2 * succeedOnAttemptNum, nil, false, succeedOnAttemptNum},
45+
{"too many tries", 3, nil, true, 4}, // max 3 retries == 4 calls. must be < succeedOnAttemptNum to work.
46+
{"success with always custom retry", 2 * succeedOnAttemptNum, func(err error) bool {
47+
return true // retry on all errors, same as no custom retry
48+
}, false, succeedOnAttemptNum},
49+
{"success with never custom retry", 2 * succeedOnAttemptNum, func(err error) bool {
50+
return false // never retry
51+
}, true, 1},
4352
}
4453

45-
policy := NewExponentialRetryPolicy(1 * time.Millisecond)
46-
policy.SetMaximumInterval(5 * time.Millisecond)
47-
policy.SetMaximumAttempts(10)
48-
49-
err := Retry(context.Background(), op, policy, nil)
50-
assert.NoError(t, err)
51-
assert.Equal(t, 5, i)
54+
for _, test := range tests {
55+
test := test
56+
t.Run(test.name, func(t *testing.T) {
57+
t.Parallel()
58+
i := 0
59+
op := func() error {
60+
i++
61+
62+
if i == succeedOnAttemptNum { // prevent infinite loops, and lets max-attempts > 5 eventually succeed
63+
return nil
64+
}
65+
66+
return &someError{}
67+
}
68+
69+
policy := NewExponentialRetryPolicy(1 * time.Millisecond)
70+
policy.SetMaximumInterval(5 * time.Millisecond)
71+
policy.SetMaximumAttempts(test.maxAttempts)
72+
73+
err := Retry(context.Background(), op, policy, test.isRetryable)
74+
if test.shouldError {
75+
assert.Error(t, err)
76+
} else {
77+
assert.NoError(t, err, "Retry count: %v", i)
78+
}
79+
assert.Equal(t, test.expectedCalls, i, "wrong number of calls")
80+
})
81+
}
5282
}
5383

5484
func TestNoRetryAfterContextDone(t *testing.T) {
@@ -76,75 +106,6 @@ func TestNoRetryAfterContextDone(t *testing.T) {
76106
assert.True(t, retryCounter >= 2, "retryCounter should be at least 2 but was %d", retryCounter) // verify that we did retry
77107
}
78108

79-
func TestRetryFailed(t *testing.T) {
80-
t.Parallel()
81-
i := 0
82-
op := func() error {
83-
i++
84-
85-
if i == 7 {
86-
return nil
87-
}
88-
89-
return &someError{}
90-
}
91-
92-
policy := NewExponentialRetryPolicy(1 * time.Millisecond)
93-
policy.SetMaximumInterval(5 * time.Millisecond)
94-
policy.SetMaximumAttempts(5)
95-
96-
err := Retry(context.Background(), op, policy, nil)
97-
assert.Error(t, err)
98-
}
99-
100-
func TestIsRetryableSuccess(t *testing.T) {
101-
t.Parallel()
102-
i := 0
103-
op := func() error {
104-
i++
105-
106-
if i == 5 {
107-
return nil
108-
}
109-
110-
return &someError{}
111-
}
112-
113-
policy := NewExponentialRetryPolicy(1 * time.Millisecond)
114-
policy.SetMaximumInterval(5 * time.Millisecond)
115-
policy.SetMaximumAttempts(10)
116-
117-
err := Retry(context.Background(), op, policy, func(err error) bool {
118-
return true // retry on any error
119-
})
120-
assert.NoError(t, err, "Retry count: %v", i)
121-
assert.Equal(t, 5, i)
122-
}
123-
124-
func TestIsRetryableFailure(t *testing.T) {
125-
t.Parallel()
126-
i := 0
127-
op := func() error {
128-
i++
129-
130-
if i == 5 {
131-
return nil
132-
}
133-
134-
return &someError{}
135-
}
136-
137-
policy := NewExponentialRetryPolicy(1 * time.Millisecond)
138-
policy.SetMaximumInterval(5 * time.Millisecond)
139-
policy.SetMaximumAttempts(10)
140-
141-
err := Retry(context.Background(), op, policy, func(err error) bool {
142-
return false // never retry
143-
})
144-
assert.Error(t, err)
145-
assert.Equal(t, 1, i)
146-
}
147-
148109
func TestConcurrentRetrier(t *testing.T) {
149110
t.Parallel()
150111
a := assert.New(t)

0 commit comments

Comments
 (0)