Debian Codegraph @

retry_test.go

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