-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepeater_test.go
277 lines (236 loc) · 7.06 KB
/
repeater_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package repeater
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepeater(t *testing.T) {
t.Run("zero or negative attempts converted to 1", func(t *testing.T) {
r := NewFixed(0, time.Millisecond)
assert.Equal(t, 1, r.attempts)
r = NewFixed(-1, time.Millisecond)
assert.Equal(t, 1, r.attempts)
})
t.Run("nil strategy defaults to fixed 1s", func(t *testing.T) {
r := NewWithStrategy(1, nil)
s, ok := r.strategy.(FixedDelay)
require.True(t, ok)
assert.Equal(t, time.Second, s.Delay)
})
}
func TestDo(t *testing.T) {
t.Run("success first try", func(t *testing.T) {
calls := 0
r := NewFixed(3, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
return nil
})
require.NoError(t, err)
assert.Equal(t, 1, calls)
})
t.Run("success after retries", func(t *testing.T) {
calls := 0
r := NewFixed(3, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
if calls < 3 {
return errors.New("not yet")
}
return nil
})
require.NoError(t, err)
assert.Equal(t, 3, calls)
})
t.Run("failure after all attempts", func(t *testing.T) {
calls := 0
r := NewFixed(3, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
return errors.New("always fails")
})
require.Error(t, err)
assert.Equal(t, "always fails", err.Error())
assert.Equal(t, 3, calls)
})
t.Run("stops on critical error", func(t *testing.T) {
calls := 0
criticalErr := errors.New("critical")
r := NewFixed(5, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
return criticalErr
}, criticalErr)
require.ErrorIs(t, err, criticalErr)
assert.Equal(t, 1, calls)
})
t.Run("stops on wrapped critical error", func(t *testing.T) {
calls := 0
criticalErr := errors.New("critical")
r := NewFixed(5, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
return errors.Join(errors.New("wrapped"), criticalErr)
}, criticalErr)
require.ErrorIs(t, err, criticalErr)
assert.Equal(t, 1, calls)
})
}
func TestDoContext(t *testing.T) {
t.Run("respects cancellation", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
calls := 0
r := NewFixed(5, 100*time.Millisecond)
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
err := r.Do(ctx, func() error {
calls++
return errors.New("failed")
})
require.ErrorIs(t, err, context.Canceled)
assert.Equal(t, 1, calls)
})
t.Run("timeout before first attempt", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
time.Sleep(5 * time.Millisecond) // ensure timeout
calls := 0
r := NewFixed(5, time.Millisecond)
err := r.Do(ctx, func() error {
calls++
return errors.New("failed")
})
require.ErrorIs(t, err, context.DeadlineExceeded)
assert.Equal(t, 0, calls)
})
}
func TestDoWithErrAny(t *testing.T) {
t.Run("stops on any error", func(t *testing.T) {
calls := 0
r := NewFixed(5, time.Millisecond)
err := r.Do(context.Background(), func() error {
calls++
return errors.New("some error")
}, ErrAny)
require.Error(t, err)
assert.Equal(t, 1, calls, "should stop on first error with ErrAny")
})
t.Run("combines with other critical errors", func(t *testing.T) {
counts := make(map[string]int)
r := NewFixed(5, time.Millisecond)
criticalErr := errors.New("critical")
err := r.Do(context.Background(), func() error {
// return different errors
counts["total"]++
if counts["total"] == 2 {
return criticalErr
}
return errors.New("non-critical")
}, criticalErr, ErrAny)
require.Error(t, err)
assert.Equal(t, 1, counts["total"], "should stop on first error when ErrAny is used")
})
}
func TestNewBackoff(t *testing.T) {
r := NewBackoff(5, time.Second)
assert.Equal(t, 5, r.attempts)
st, ok := r.strategy.(*backoff)
require.True(t, ok)
// check defaults
assert.Equal(t, time.Second, st.initial)
assert.Equal(t, 30*time.Second, st.maxDelay)
assert.Equal(t, BackoffExponential, st.btype)
assert.InDelta(t, 0.1, st.jitter, 0.0001, "default jitter")
// check with options
r = NewBackoff(5, time.Second,
WithMaxDelay(5*time.Second),
WithBackoffType(BackoffLinear),
WithJitter(0.2),
)
st, ok = r.strategy.(*backoff)
require.True(t, ok)
assert.Equal(t, time.Second, st.initial)
assert.Equal(t, 5*time.Second, st.maxDelay)
assert.Equal(t, BackoffLinear, st.btype)
assert.InDelta(t, 0.2, st.jitter, 0.0001, "custom jitter")
}
func TestBackoffReal(t *testing.T) {
startTime := time.Now()
var attempts []time.Time
expectedAttempts := 4
r := NewBackoff(expectedAttempts, 10*time.Millisecond, WithJitter(0))
// record all attempt times
err := r.Do(context.Background(), func() error {
attempts = append(attempts, time.Now())
return errors.New("test error")
})
require.Error(t, err)
assert.Len(t, attempts, expectedAttempts, "should make exactly %d attempts", expectedAttempts)
// first attempt should be immediate
assert.Less(t, attempts[0].Sub(startTime), 5*time.Millisecond)
// check intervals between attempts
var intervals []time.Duration
for i := 1; i < len(attempts); i++ {
intervals = append(intervals, attempts[i].Sub(attempts[i-1]))
t.Logf("attempt %d interval: %v", i, intervals[i-1])
}
// check total time for all attempts
// with exponential backoff and 10ms initial delay we expect:
// - attempt 1 - immediate (0ms)
// - attempt 2 - after 10ms delay (total ~10ms)
// - attempt 3 - after 20ms delay (total ~30ms)
// - attempt 4 - after 40ms delay (total ~70ms)
totalTime := attempts[len(attempts)-1].Sub(startTime)
assert.Greater(t, totalTime, 65*time.Millisecond)
assert.Less(t, totalTime, 75*time.Millisecond)
}
func ExampleRepeater_Do() {
// create repeater with exponential backoff
r := NewBackoff(5, time.Second)
err := r.Do(context.Background(), func() error {
// simulating successful operation
return nil
})
fmt.Printf("completed with error: %v", err)
// Output: completed with error: <nil>
}
func ExampleNewFixed() {
// create repeater with fixed 100ms delay between attempts
r := NewFixed(3, 100*time.Millisecond)
// retry on "temp error" but give up immediately on "critical error"
criticalErr := errors.New("critical error")
// run Do and check the returned error
err := r.Do(context.Background(), func() error {
// simulating critical error
return criticalErr
}, criticalErr)
if err != nil {
fmt.Printf("got expected error: %v", err)
}
// Output: got expected error: critical error
}
func ExampleNewBackoff() {
// create backoff repeater with custom settings
r := NewBackoff(3, time.Millisecond,
WithMaxDelay(10*time.Millisecond),
WithBackoffType(BackoffLinear),
WithJitter(0),
)
var attempts int
err := r.Do(context.Background(), func() error {
attempts++
if attempts < 3 {
return errors.New("temporary error")
}
return nil
})
fmt.Printf("completed with error: %v after %d attempts", err, attempts)
// Output: completed with error: <nil> after 3 attempts
}