-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathwait_test.go
More file actions
446 lines (421 loc) · 12.8 KB
/
wait_test.go
File metadata and controls
446 lines (421 loc) · 12.8 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package wait
import (
"context"
"fmt"
"net/http"
"testing"
"testing/synctest"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
)
// Options used for comparing AsyncActionHandler
var cmpOpts = []cmp.Option{
cmp.AllowUnexported(AsyncActionHandler[interface{}]{}),
cmpopts.IgnoreFields(AsyncActionHandler[interface{}]{}, "checkFn"), // cmp won't compare functions well
}
func TestNew(t *testing.T) {
checkFn := func() (waitFinished bool, res *interface{}, err error) { return true, nil, nil }
got := New(checkFn)
want := &AsyncActionHandler[interface{}]{
checkFn: checkFn,
sleepBeforeWait: 0 * time.Second,
throttle: 5 * time.Second,
timeout: 30 * time.Minute,
tempErrRetryLimit: 5,
}
diff := cmp.Diff(got, want, cmpOpts...)
if diff != "" {
t.Errorf("Data does not match: %s", diff)
}
}
func TestSetThrottle(t *testing.T) {
checkFn := func() (waitFinished bool, res *interface{}, err error) { return true, nil, nil }
for _, tt := range []struct {
desc string
throttle time.Duration
}{
{
"base_1",
time.Hour,
},
{
"base_2",
10 * time.Millisecond,
},
{
"base_3",
0,
},
} {
t.Run(tt.desc, func(t *testing.T) {
want := New(checkFn)
want.throttle = tt.throttle
got := New(checkFn)
got.SetThrottle(tt.throttle)
diff := cmp.Diff(got, want, cmpOpts...)
if diff != "" {
t.Errorf("Data does not match: %s", diff)
}
})
}
}
func TestSetTimeout(t *testing.T) {
checkFn := func() (waitFinished bool, res *interface{}, err error) { return true, nil, nil }
for _, tt := range []struct {
desc string
timeout time.Duration
}{
{
"base_1",
time.Hour,
},
{
"base_2",
10 * time.Millisecond,
},
{
"base_3",
0,
},
} {
t.Run(tt.desc, func(t *testing.T) {
want := New(checkFn)
want.timeout = tt.timeout
got := New(checkFn)
got.SetTimeout(tt.timeout)
diff := cmp.Diff(got, want, cmpOpts...)
if diff != "" {
t.Errorf("Data does not match: %s", diff)
}
})
}
}
func TestSetSleepBeforeWait(t *testing.T) {
checkFn := func() (waitFinished bool, res *interface{}, err error) { return true, nil, nil }
for _, tt := range []struct {
desc string
sleepBeforeWait time.Duration
}{
{
"base_1",
time.Hour,
},
{
"base_2",
10 * time.Millisecond,
},
{
"base_3",
0,
},
} {
t.Run(tt.desc, func(t *testing.T) {
want := New(checkFn)
want.sleepBeforeWait = tt.sleepBeforeWait
got := New(checkFn)
got.SetSleepBeforeWait(tt.sleepBeforeWait)
diff := cmp.Diff(got, want, cmpOpts...)
if diff != "" {
t.Errorf("Data does not match: %s", diff)
}
})
}
}
func TestSetTempErrRetryLimit(t *testing.T) {
checkFn := func() (waitFinished bool, res *interface{}, err error) { return true, nil, nil }
for _, tt := range []struct {
desc string
tempErrRetryLimit int
}{
{
"base_1",
2,
},
{
"base_3",
0,
},
} {
t.Run(tt.desc, func(t *testing.T) {
want := New(checkFn)
want.tempErrRetryLimit = tt.tempErrRetryLimit
got := New(checkFn)
got.SetTempErrRetryLimit(tt.tempErrRetryLimit)
diff := cmp.Diff(got, want, cmpOpts...)
if diff != "" {
t.Errorf("Data does not match: %s", diff)
}
})
}
}
func TestWaitWithContext(t *testing.T) {
for _, tt := range []struct {
desc string
checkFnNumberCallsToFinishWait int
checkFnWaitSucceeds bool
checkFnNumberCallsUntilErr int
checkFnReturnsTempErr bool
handlerSleepBeforeWait time.Duration
handlerThrottle time.Duration
handlerTimeout time.Duration
handlerTempErrRetryLimit int
contextTimeout time.Duration
wantCheckFnNumberCalls int
wantErr bool
}{
{
desc: "base",
checkFnNumberCallsToFinishWait: 1,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 1,
wantErr: false,
},
{
desc: "bad_trottle",
checkFnNumberCallsToFinishWait: 1,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 0,
handlerThrottle: 0 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 0,
wantErr: true,
},
{
desc: "throttle",
checkFnNumberCallsToFinishWait: 2,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 2,
wantErr: false,
},
{
desc: "throttle_timeout_1",
checkFnNumberCallsToFinishWait: 999999,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 3,
wantErr: true,
},
{
desc: "throttle_timeout_2",
checkFnNumberCallsToFinishWait: 999999,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 1000 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 3,
wantErr: true,
},
{
desc: "set_sleep_before_wait_and_throttle",
checkFnNumberCallsToFinishWait: 999999,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 30 * time.Millisecond,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 2,
wantErr: true,
},
{
desc: "set_sleep_before_wait_timeout_1",
checkFnNumberCallsToFinishWait: 2,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 200 * time.Millisecond,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 100 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 0,
wantErr: true,
},
{
desc: "set_sleep_before_wait_timeout_2",
checkFnNumberCallsToFinishWait: 2,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 999999,
handlerSleepBeforeWait: 200 * time.Millisecond,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 1000 * time.Millisecond,
handlerTempErrRetryLimit: 0,
contextTimeout: 100 * time.Millisecond,
wantCheckFnNumberCalls: 0,
wantErr: true,
},
{
desc: "retry_limit_temp_err_1",
checkFnNumberCallsToFinishWait: 999999,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 0,
checkFnReturnsTempErr: false,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 1000 * time.Millisecond,
handlerTempErrRetryLimit: 5,
contextTimeout: 1000 * time.Millisecond,
wantCheckFnNumberCalls: 1,
wantErr: true,
},
{
desc: "retry_limit_temp_err_2",
checkFnNumberCallsToFinishWait: 999999,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 1,
checkFnReturnsTempErr: true,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 1000 * time.Millisecond,
handlerTempErrRetryLimit: 5,
contextTimeout: 1000 * time.Millisecond,
wantCheckFnNumberCalls: 5,
wantErr: true,
},
{
desc: "retry_limit_temp_err_3",
checkFnNumberCallsToFinishWait: 3,
checkFnWaitSucceeds: true,
checkFnNumberCallsUntilErr: 1,
checkFnReturnsTempErr: true,
handlerSleepBeforeWait: 0,
handlerThrottle: 40 * time.Millisecond,
handlerTimeout: 1000 * time.Millisecond,
handlerTempErrRetryLimit: 5,
contextTimeout: 1000 * time.Millisecond,
wantCheckFnNumberCalls: 3,
wantErr: false,
},
} {
t.Run(tt.desc, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
type respType struct{}
numberCheckFnCalls := 0
checkFn := func() (waitFinished bool, response *respType, err error) {
numberCheckFnCalls++
if numberCheckFnCalls == tt.checkFnNumberCallsToFinishWait {
if tt.checkFnWaitSucceeds {
return true, &respType{}, nil
}
return true, &respType{}, fmt.Errorf("the async action couldn't be done")
}
if numberCheckFnCalls < tt.checkFnNumberCallsUntilErr {
return false, nil, nil
}
if tt.checkFnReturnsTempErr {
return false, nil, &oapierror.GenericOpenAPIError{
StatusCode: RetryHttpErrorStatusCodes[0],
ErrorMessage: "something bad happened when checking if the async action was finished",
}
}
return false, nil, fmt.Errorf("something bad happened when checking if the async action was finished")
}
handler := AsyncActionHandler[respType]{
checkFn: checkFn,
sleepBeforeWait: tt.handlerSleepBeforeWait,
throttle: tt.handlerThrottle,
timeout: tt.handlerTimeout,
tempErrRetryLimit: tt.handlerTempErrRetryLimit,
}
ctx, cancel := context.WithTimeout(context.Background(), tt.contextTimeout)
defer cancel()
resp, err := handler.WaitWithContext(ctx)
if tt.wantErr && (err == nil) {
t.Errorf("expected error but got none")
}
if !tt.wantErr && (err != nil) {
t.Errorf("expected no error but got \"%v\"", err)
}
if (err == nil) && (resp == nil) {
t.Errorf("got nil err but nil resp")
}
if numberCheckFnCalls != tt.wantCheckFnNumberCalls {
t.Errorf("expected %d calls to checkFn but got %d instead", tt.wantCheckFnNumberCalls, numberCheckFnCalls)
}
})
})
}
}
func TestHandleError(t *testing.T) {
for _, tt := range []struct {
desc string
reqErr error
tempErrRetryLimit int
wantErr bool
}{
{
desc: "handle_oapi_error",
reqErr: &oapierror.GenericOpenAPIError{
StatusCode: http.StatusInternalServerError,
},
tempErrRetryLimit: 5,
wantErr: true,
},
{
desc: "not_generic_oapi_error",
reqErr: fmt.Errorf("some error"),
tempErrRetryLimit: 5,
wantErr: true,
},
{
desc: "bad_gateway_error",
reqErr: &oapierror.GenericOpenAPIError{
StatusCode: http.StatusBadGateway,
},
tempErrRetryLimit: 5,
wantErr: false,
},
{
desc: "gateway_timeout_error",
reqErr: &oapierror.GenericOpenAPIError{
StatusCode: http.StatusBadGateway,
},
tempErrRetryLimit: 5,
wantErr: false,
},
{
desc: "temp_error_retry_limit_reached",
reqErr: &oapierror.GenericOpenAPIError{
StatusCode: http.StatusBadGateway,
},
tempErrRetryLimit: 1,
wantErr: true,
},
} {
t.Run(tt.desc, func(t *testing.T) {
w := &AsyncActionHandler[interface{}]{
tempErrRetryLimit: tt.tempErrRetryLimit,
}
_, err := w.handleError(0, tt.reqErr)
if (err != nil) != tt.wantErr {
t.Errorf("handleError() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}