-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
207 lines (173 loc) · 4.4 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
package retry
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func TestDoWithResult(t *testing.T) {
t.Parallel()
const retries = 5
const want = 10
count := 0
got, err := DoR(context.Background(), Zero(), func(ctx context.Context) (int, error) {
if count == retries {
return want, nil
}
count++
return 0, errors.New("")
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if count != retries {
t.Errorf("unexpected count of retries: %d, expected: %d", count, retries)
}
if got != want {
t.Errorf("value got: %v, want: %v", got, want)
}
}
func TestDoWithResult_Error(t *testing.T) {
t.Parallel()
delays := Delays{time.Second, time.Second}
count := 0
_, err := DoR(context.Background(), delays, func(ctx context.Context) (int, error) {
count++
return 0, errors.New("")
})
if err == nil {
t.Error("expected error but got nil")
}
if count != len(delays)+1 {
t.Errorf("unexpected count of retries: %s", err)
}
}
func TestRetryWithResult_Context(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
_, err := DoR(ctx, Zero(), func(ctx context.Context) (int, error) {
cancel()
return 0, errors.New("")
})
if err == nil {
t.Error("expected error but got nil")
}
_, err = DoR(ctx, Constant(time.Second), func(ctx context.Context) (int, error) {
return 0, errors.New("")
})
if err == nil {
t.Error("expected error but got nil")
}
}
func TestRetryWithResult_Timeout(t *testing.T) {
t.Parallel()
_, err := DoR(context.Background(), Constant(100*time.Millisecond), func(ctx context.Context) (int, error) {
return 0, errors.New("")
}, WithTimeout(time.Second))
if err == nil {
t.Error("expected error but got nil")
}
}
func TestRetryWithResult_MaxRetrying(t *testing.T) {
t.Parallel()
const retries = 5
count := 0
_, err := DoRN(context.Background(), Zero(), func(ctx context.Context) (int, error) {
count++
return 0, errors.New("")
}, retries)
if err == nil {
t.Error("expected error but got nil")
}
if count != (1 /* first call */ + retries /* additional calls */) {
t.Errorf("unexpected count of retries: %d, expected: %d", count, retries)
}
}
func TestRetryWithResult_MaxElapsedTime(t *testing.T) {
t.Parallel()
_, err := DoRE(context.Background(), Constant(100*time.Millisecond), func(ctx context.Context) (int, error) {
return 0, errors.New("")
}, time.Second)
if err == nil {
t.Error("expected error but got nil")
}
}
func TestRetry(t *testing.T) {
t.Parallel()
const retries = 5
count := 0
err := Do(context.Background(), Zero(), func(ctx context.Context) error {
if count == retries {
return nil
}
count++
return errors.New("")
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if count != retries {
t.Errorf("unexpected count of retries: %d, expected: %d", count, retries)
}
}
func TestPermanent(t *testing.T) {
t.Parallel()
want := errors.New("want")
err := Permanent(want)
got := errors.Unwrap(err)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
if fmt.Sprintf("%s", want) != fmt.Sprintf("%s", err) {
t.Errorf("Error() = %v, want %v", err.Error(), want.Error())
}
}
func TestDo_PermanentError(t *testing.T) {
t.Parallel()
const retries = 5
count := 0
wantErr := errors.New("error")
err := Do(context.Background(), Zero(), func(ctx context.Context) error {
count++
return Permanent(wantErr)
})
if !errors.Is(err, wantErr) {
t.Errorf("expected error: %s, got: %s", wantErr, err)
}
if count != 1 {
t.Errorf("unexpected count of retries: %d, expected: %d", count, 1)
}
}
func TestDo_Notify(t *testing.T) {
t.Parallel()
const retries = 1
const constDelay = time.Microsecond
_ = Do(context.Background(), Constant(constDelay), func(ctx context.Context) error {
return errors.New("error")
}, WithNotify(func(err error, delay time.Duration, try int, elapsed time.Duration) {
if delay != constDelay {
t.Errorf("want delay: %s, got: %s", constDelay, delay)
}
if try != 1 {
t.Errorf("want try: %d, got: %d", 1, try)
}
}), WithMaxRetries(retries))
}
func TestAs(t *testing.T) {
t.Parallel()
e := &Error{}
if got := As(e); e != got {
t.Errorf("As() = %v, want %v", got, e)
}
if got := As(errors.New("error")); got != nil {
t.Errorf("As() = %v, want nil", got)
}
}
func TestUnwrap(t *testing.T) {
t.Parallel()
err := errors.New("error")
if got := Unwrap(&Error{Err: err}); got != err {
t.Errorf("As() = %v, want %v", got, err)
}
}