-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretry_test.go
More file actions
188 lines (152 loc) · 4.39 KB
/
Copy pathretry_test.go
File metadata and controls
188 lines (152 loc) · 4.39 KB
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
package goretry
import (
"context"
"errors"
"testing"
"time"
)
type testError struct {
message string
transient bool
timeout bool
temporary bool
}
func (e *testError) Error() string { return e.message }
func (e *testError) Timeout() bool { return e.timeout }
func (e *testError) Temporary() bool { return e.temporary }
func TestRetrier_Do_Success(t *testing.T) {
retrier := NewRetrier(NewFixedDelayPolicy(10 * time.Millisecond))
callCount := 0
err := retrier.Do(func() error {
callCount++
if callCount < 3 {
return &testError{message: "transient error", transient: true, timeout: true}
}
return nil
})
if err != nil {
t.Errorf("Expected success, got error: %v", err)
}
if callCount != 3 {
t.Errorf("Expected 3 calls, got %d", callCount)
}
}
func TestRetrier_Do_NonTransientError(t *testing.T) {
retrier := NewRetrier(NewFixedDelayPolicy(10 * time.Millisecond))
callCount := 0
nonTransientErr := errors.New("non-transient error")
err := retrier.Do(func() error {
callCount++
return nonTransientErr
})
if err != nonTransientErr {
t.Errorf("Expected non-transient error to be returned immediately, got: %v", err)
}
if callCount != 1 {
t.Errorf("Expected 1 call for non-transient error, got %d", callCount)
}
}
func TestRetrier_Do_OutOfRetries(t *testing.T) {
retrier := NewRetrier(NewFixedDelayPolicy(1*time.Millisecond), WithMaxAttempts(2))
callCount := 0
transientErr := &testError{message: "transient error", timeout: true}
err := retrier.Do(func() error {
callCount++
return transientErr
})
var outOfRetriesErr *OutOfRetriesError
if !errors.As(err, &outOfRetriesErr) {
t.Errorf("Expected OutOfRetriesError, got: %T", err)
}
if outOfRetriesErr.Attempts != 2 {
t.Errorf("Expected 2 attempts, got %d", outOfRetriesErr.Attempts)
}
if callCount != 2 {
t.Errorf("Expected 2 calls, got %d", callCount)
}
}
func TestRetrier_DoWithContext_Cancellation(t *testing.T) {
retrier := NewRetrier(NewFixedDelayPolicy(100 * time.Millisecond))
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
err := retrier.DoWithContext(ctx, func(ctx context.Context) error {
return &testError{message: "transient error", timeout: true}
})
if !errors.Is(err, context.Canceled) {
t.Errorf("Expected context.Canceled, got: %v", err)
}
}
func TestDefaultTransientErrorFunc(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{"nil error", nil, false},
{"timeout error", &testError{timeout: true}, true},
{"temporary error", &testError{temporary: true}, true},
{"context canceled", context.Canceled, false},
{"context deadline exceeded", context.DeadlineExceeded, false},
{"regular error", errors.New("regular error"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DefaultTransientErrorFunc(tt.err)
if result != tt.expected {
t.Errorf("Expected %v, got %v for error: %v", tt.expected, result, tt.err)
}
})
}
}
func TestCustomTransientErrorFunc(t *testing.T) {
customTransient := func(err error) bool {
return err.Error() == "custom transient"
}
retrier := NewRetrier(
NewFixedDelayPolicy(1*time.Millisecond),
WithTransientErrorFunc(customTransient),
WithMaxAttempts(2),
)
callCount := 0
err := retrier.Do(func() error {
callCount++
return errors.New("custom transient")
})
var outOfRetriesErr *OutOfRetriesError
if !errors.As(err, &outOfRetriesErr) {
t.Errorf("Expected OutOfRetriesError, got: %T", err)
}
if callCount != 2 {
t.Errorf("Expected 2 calls, got %d", callCount)
}
}
func TestOnRetryCallback(t *testing.T) {
var retryAttempts []int
var retryErrors []error
retrier := NewRetrier(
NewFixedDelayPolicy(1*time.Millisecond),
WithMaxAttempts(3),
WithOnRetry(func(attempt int, err error) {
retryAttempts = append(retryAttempts, attempt)
retryErrors = append(retryErrors, err)
}),
)
callCount := 0
transientErr := &testError{message: "transient error", timeout: true}
_ = retrier.Do(func() error {
callCount++
return transientErr
})
expectedAttempts := []int{1, 2}
if len(retryAttempts) != len(expectedAttempts) {
t.Errorf("Expected %d retry callbacks, got %d", len(expectedAttempts), len(retryAttempts))
}
for i, attempt := range retryAttempts {
if attempt != expectedAttempts[i] {
t.Errorf("Expected attempt %d, got %d", expectedAttempts[i], attempt)
}
}
}