forked from cenkalti/backoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
224 lines (191 loc) · 4.31 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package backoff
import (
"context"
"errors"
"fmt"
"io"
"log"
"testing"
"time"
)
type testTimer struct {
timer *time.Timer
}
func (t *testTimer) Start(duration time.Duration) {
t.timer = time.NewTimer(0)
}
func (t *testTimer) Stop() {
if t.timer != nil {
t.timer.Stop()
}
}
func (t *testTimer) C() <-chan time.Time {
return t.timer.C
}
func TestRetry(t *testing.T) {
const successOn = 3
var i = 0
// This function is successful on "successOn" calls.
f := func() error {
i++
log.Printf("function is called %d. time\n", i)
if i == successOn {
log.Println("OK")
return nil
}
log.Println("error")
return errors.New("error")
}
err := RetryNotifyWithTimer(f, NewExponentialBackOff(), nil, &testTimer{})
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if i != successOn {
t.Errorf("invalid number of retries: %d", i)
}
}
func TestRetryWithData(t *testing.T) {
const successOn = 3
var i = 0
// This function is successful on "successOn" calls.
f := func() (int, error) {
i++
log.Printf("function is called %d. time\n", i)
if i == successOn {
log.Println("OK")
return 42, nil
}
log.Println("error")
return 1, errors.New("error")
}
res, err := RetryNotifyWithTimerAndData(f, NewExponentialBackOff(), nil, &testTimer{})
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if i != successOn {
t.Errorf("invalid number of retries: %d", i)
}
if res != 42 {
t.Errorf("invalid data in response: %d, expected 42", res)
}
}
func TestRetryContext(t *testing.T) {
var cancelOn = 3
var i = 0
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// This function cancels context on "cancelOn" calls.
f := func() error {
i++
log.Printf("function is called %d. time\n", i)
// cancelling the context in the operation function is not a typical
// use-case, however it allows to get predictable test results.
if i == cancelOn {
cancel()
}
log.Println("error")
return fmt.Errorf("error (%d)", i)
}
err := RetryNotifyWithTimer(f, WithContext(NewConstantBackOff(time.Millisecond), ctx), nil, &testTimer{})
if err == nil {
t.Errorf("error is unexpectedly nil")
}
if !errors.Is(err, context.Canceled) {
t.Errorf("unexpected error: %s", err.Error())
}
if i != cancelOn {
t.Errorf("invalid number of retries: %d", i)
}
}
func TestRetryPermanent(t *testing.T) {
ensureRetries := func(test string, shouldRetry bool, f func() (int, error), expectRes int) {
numRetries := -1
maxRetries := 1
res, _ := RetryNotifyWithTimerAndData(
func() (int, error) {
numRetries++
if numRetries >= maxRetries {
return -1, Permanent(errors.New("forced"))
}
return f()
},
NewExponentialBackOff(),
nil,
&testTimer{},
)
if shouldRetry && numRetries == 0 {
t.Errorf("Test: '%s', backoff should have retried", test)
}
if !shouldRetry && numRetries > 0 {
t.Errorf("Test: '%s', backoff should not have retried", test)
}
if res != expectRes {
t.Errorf("Test: '%s', got res %d but expected %d", test, res, expectRes)
}
}
for _, testCase := range []struct {
name string
f func() (int, error)
shouldRetry bool
res int
}{
{
"nil test",
func() (int, error) {
return 1, nil
},
false,
1,
},
{
"io.EOF",
func() (int, error) {
return 2, io.EOF
},
true,
-1,
},
{
"Permanent(io.EOF)",
func() (int, error) {
return 3, Permanent(io.EOF)
},
false,
3,
},
{
"Wrapped: Permanent(io.EOF)",
func() (int, error) {
return 4, fmt.Errorf("Wrapped error: %w", Permanent(io.EOF))
},
false,
4,
},
} {
ensureRetries(testCase.name, testCase.shouldRetry, testCase.f, testCase.res)
}
}
func TestPermanent(t *testing.T) {
want := errors.New("foo")
other := errors.New("bar")
var err error = Permanent(want)
got := errors.Unwrap(err)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
if is := errors.Is(err, want); !is {
t.Errorf("err: %v is not %v", err, want)
}
if is := errors.Is(err, other); is {
t.Errorf("err: %v is %v", err, other)
}
wrapped := fmt.Errorf("wrapped: %w", err)
var permanent *PermanentError
if !errors.As(wrapped, &permanent) {
t.Errorf("errors.As(%v, %v)", wrapped, permanent)
}
err = Permanent(nil)
if err != nil {
t.Errorf("got %v, want nil", err)
}
}