-
Notifications
You must be signed in to change notification settings - Fork 0
/
retryer_test.go
257 lines (221 loc) · 6.12 KB
/
retryer_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
// Copyright 2021 The s3iot authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package s3iot
import (
"context"
"errors"
"testing"
"time"
)
var _ RetryerFactory = &NoRetryerFactory{}
var _ RetryerFactory = &ExponentialBackoffRetryerFactory{}
var _ RetryerFactory = &PauseOnFailRetryerFactory{}
var _ RetryerFactory = &RetryerHookFactory{}
var errDummy = errors.New("dummy")
func TestNoRetryer(t *testing.T) {
f := &NoRetryerFactory{}
r := f.New(nil)
if cont := r.OnFail(context.TODO(), 0, nil); cont {
t.Error("Unexpected retry")
}
r.OnSuccess(0)
}
func TestExponentialBackoffRetryer(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
f := &ExponentialBackoffRetryerFactory{
WaitBase: 50 * time.Millisecond,
WaitMax: 250 * time.Millisecond,
RetryMax: 4,
}
r := f.New(nil)
ts := time.Now()
for i := 0; i < 4; i++ {
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("Unexpected failure before reaching RetryMax")
}
}
te := time.Now()
// 50 + 100 + 200 + 250 = 600ms
expectedWait := 600 * time.Millisecond
tolerance := 50 * time.Millisecond
if diff := te.Sub(ts) - expectedWait; diff < -tolerance || tolerance < diff {
t.Errorf("Expected wait: %v, actual: %v", te.Sub(ts), expectedWait)
}
if cont := r.OnFail(context.TODO(), 1, errDummy); !cont {
t.Error("Unexpected failure on different id")
}
if cont := r.OnFail(context.TODO(), 0, errDummy); cont {
t.Error("Unexpected retry after reaching RetryMax")
}
r.OnSuccess(0)
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("Unexpected failure after resetting failure")
}
})
t.Run("CancelDuringWait", func(t *testing.T) {
f := &ExponentialBackoffRetryerFactory{
WaitBase: time.Second,
WaitMax: time.Second,
RetryMax: 4,
}
r := f.New(nil)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
go func() {
select {
case <-ctx.Done():
case <-time.After(100 * time.Millisecond):
panic("timeout")
}
}()
if cont := r.OnFail(ctx, 0, errDummy); cont {
t.Error("Unexpected retry")
}
})
}
func TestPauseOnFailRetryer(t *testing.T) {
t.Run("WithExponentialBackoffRetryer", func(t *testing.T) {
f := &PauseOnFailRetryerFactory{
Base: &ExponentialBackoffRetryerFactory{
WaitBase: 50 * time.Millisecond,
WaitMax: 50 * time.Millisecond,
RetryMax: 1,
},
}
uc := &dummyUploadContext{
chPause: make(chan struct{}, 1),
}
r := f.New(uc)
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("Unexpected failure before reaching RetryMax")
}
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("PauseOnFailRetryer should not abort")
}
select {
case <-uc.chPause:
case <-time.After(time.Second):
t.Fatal("Timeout")
}
r.OnSuccess(0)
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("PauseOnFailRetryer should not abort")
}
})
t.Run("WithNoRetryer", func(t *testing.T) {
f := &PauseOnFailRetryerFactory{}
uc := &dummyUploadContext{
chPause: make(chan struct{}, 1),
}
r := f.New(uc)
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("PauseOnFailRetryer should not abort")
}
select {
case <-uc.chPause:
case <-time.After(time.Second):
t.Fatal("Timeout")
}
r.OnSuccess(0)
if cont := r.OnFail(context.TODO(), 0, errDummy); !cont {
t.Error("PauseOnFailRetryer should not abort")
}
})
}
func TestRetryerHookFactory(t *testing.T) {
testCases := map[string]struct {
uploadContext Pauser
bucket string
key string
}{
"BucketKeyer": {
uploadContext: &dummyUploadContext{},
bucket: "dummyBucket",
key: "dummyKey",
},
"NoBucketKeyer": {
uploadContext: &dummyPauser{},
},
}
for name, tt := range testCases {
tt := tt
t.Run(name, func(t *testing.T) {
var bucketStored, keyStored string
var errStored error
f := &RetryerHookFactory{
Base: &dummyRetryerFactory{},
OnError: func(bucket, key string, err error) {
if errStored != nil {
t.Error("OnError is expected to be called once")
}
bucketStored, keyStored, errStored = bucket, key, err
},
}
r := f.New(tt.uploadContext)
if cont := r.OnFail(context.TODO(), 0, errDummy); cont {
t.Error("Expected no-continue")
}
r.OnSuccess(0)
if n := f.Base.(*dummyRetryerFactory).numFail; n != 1 {
t.Errorf("Base retryer OnFail must be called once, but called %d times", n)
}
if n := f.Base.(*dummyRetryerFactory).numSuccess; n != 1 {
t.Errorf("Base retryer OnSuccess must be called once, but called %d times", n)
}
if bucketStored != tt.bucket {
t.Errorf("Expected %s, got %s", tt.bucket, bucketStored)
}
if keyStored != tt.key {
t.Errorf("Expected %s, got %s", tt.key, keyStored)
}
if errStored != errDummy {
t.Errorf("Expected '%v', got '%v'", errDummy, errStored)
}
})
}
}
type dummyRetryerFactory struct {
numFail int
numSuccess int
}
func (f *dummyRetryerFactory) New(Pauser) Retryer {
return &dummyRetryer{dummyRetryerFactory: f}
}
type dummyRetryer struct {
*dummyRetryerFactory
}
func (r *dummyRetryer) OnFail(context.Context, int64, error) bool {
r.numFail++
return false
}
func (r *dummyRetryer) OnSuccess(int64) {
r.numSuccess++
}
type dummyUploadContext struct {
UploadContext
chPause chan struct{}
}
func (dummyUploadContext) BucketKey() (bucket, key string) {
return "dummyBucket", "dummyKey"
}
func (uc *dummyUploadContext) Pause() {
uc.chPause <- struct{}{}
}
type dummyPauser struct {
UploadContext
chPause chan struct{}
}
func (uc *dummyPauser) Pause() {
uc.chPause <- struct{}{}
}