-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
176 lines (132 loc) · 5.11 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
package retrier_test
import (
"context"
"errors"
"testing"
"time"
retrier "github.com/hueristiq/hq-go-retrier"
"github.com/hueristiq/hq-go-retrier/backoff"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
var errTestOperation = errors.New("operation failed")
// Mock operation that will fail a given number of times before succeeding.
type mockOperation struct {
mock.Mock
failureCount int
callCount int
}
func (m *mockOperation) Operation() error {
m.callCount++
if m.callCount <= m.failureCount {
return errTestOperation
}
return nil
}
func TestRetry_SuccessAfterFailures(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2} // Fail twice, then succeed
ctx := context.Background()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.Exponential()))
require.NoError(t, err, "Expected operation to succeed after retries")
assert.Equal(t, 3, mockOp.callCount, "Expected the operation to be called 3 times")
}
func TestRetry_MaxRetriesExceeded(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 10} // Will fail more times than the allowed retries
ctx := context.Background()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(3),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.Exponential()))
require.Error(t, err, "Expected operation to fail after retries")
assert.Equal(t, 3, mockOp.callCount, "Expected the operation to be called 3 times")
}
func TestRetryWithContext_Timeout(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 10}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(30*time.Millisecond),
retrier.WithMaxDelay(100*time.Millisecond),
retrier.WithBackoff(backoff.Exponential()))
require.Error(t, err, "Expected operation to fail due to context timeout")
require.ErrorIs(t, err, context.DeadlineExceeded, "Expected timeout error")
assert.LessOrEqual(t, mockOp.callCount, 2, "Expected the operation to be called less than the max retries due to timeout")
}
func TestRetryWithData_Success(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2}
ctx := context.Background()
operationWithData := func() (int, error) {
if mockOp.callCount < 2 {
mockOp.callCount++
return 0, errTestOperation
}
return 42, nil
}
result, err := retrier.RetryWithData(ctx, operationWithData,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.Exponential()))
require.NoError(t, err, "Expected operation to succeed after retries")
assert.Equal(t, 42, result, "Expected operation result to be 42")
}
func TestRetryWithDecorrelatedJitter(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2}
ctx := context.Background()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.ExponentialWithDecorrelatedJitter()))
require.NoError(t, err, "Expected operation to succeed after retries with decorrelated jitter")
assert.Equal(t, 3, mockOp.callCount, "Expected the operation to be called 3 times")
}
func TestRetry_FullJitter(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2}
ctx := context.Background()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.ExponentialWithFullJitter()))
require.NoError(t, err, "Expected operation to succeed after retries with full jitter")
assert.Equal(t, 3, mockOp.callCount, "Expected the operation to be called 3 times")
}
func TestRetry_EqualJitter(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2}
ctx := context.Background()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.ExponentialWithEqualJitter()))
require.NoError(t, err, "Expected operation to succeed after retries with equal jitter")
assert.Equal(t, 3, mockOp.callCount, "Expected the operation to be called 3 times")
}
func TestRetry_ContextCanceled(t *testing.T) {
t.Parallel()
mockOp := &mockOperation{failureCount: 2}
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := retrier.Retry(ctx, mockOp.Operation,
retrier.WithMaxRetries(5),
retrier.WithMinDelay(10*time.Millisecond),
retrier.WithMaxDelay(50*time.Millisecond),
retrier.WithBackoff(backoff.Exponential()))
require.Error(t, err, "Expected operation to fail due to canceled context")
require.ErrorIs(t, err, context.Canceled, "Expected timeout error")
}