-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait_test.go
495 lines (437 loc) · 10.2 KB
/
wait_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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Copyright 2014 The Kubernetes 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 wait
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestUntil(t *testing.T) {
t.Parallel()
ch := make(chan struct{})
close(ch)
Until(func() {
t.Fatal("should not have been invoked")
}, 0, ch)
ch = make(chan struct{})
called := make(chan struct{})
go func() {
Until(func() {
called <- struct{}{}
}, 0, ch)
close(called)
}()
<-called
close(ch)
<-called
}
func TestNonSlidingUntil(t *testing.T) {
t.Parallel()
ch := make(chan struct{})
close(ch)
NonSlidingUntil(func() {
t.Fatal("should not have been invoked")
}, 0, ch)
ch = make(chan struct{})
called := make(chan struct{})
go func() {
NonSlidingUntil(func() {
called <- struct{}{}
}, 0, ch)
close(called)
}()
<-called
close(ch)
<-called
}
func TestUntilReturnsImmediately(t *testing.T) {
t.Parallel()
now := time.Now()
ch := make(chan struct{})
Until(func() {
close(ch)
}, 30*time.Second, ch)
if now.Add(25 * time.Second).Before(time.Now()) {
t.Errorf("Until did not return immediately when the stop chan was closed inside the func")
}
}
func TestJitterUntil(t *testing.T) {
t.Parallel()
ch := make(chan struct{})
// if a channel is closed JitterUntil never calls function f
// and returns immediately
close(ch)
JitterUntil(func() {
t.Fatal("should not have been invoked")
}, 0, 1.0, true, ch)
ch = make(chan struct{})
called := make(chan struct{})
go func() {
JitterUntil(func() {
called <- struct{}{}
}, 0, 1.0, true, ch)
close(called)
}()
<-called
close(ch)
<-called
}
func TestJitterUntilReturnsImmediately(t *testing.T) {
t.Parallel()
now := time.Now()
ch := make(chan struct{})
JitterUntil(func() {
close(ch)
}, 30*time.Second, 1.0, true, ch)
if now.Add(25 * time.Second).Before(time.Now()) {
t.Errorf("JitterUntil did not return immediately when the stop chan was closed inside the func")
}
}
func TestJitterUntilNegativeFactor(t *testing.T) {
t.Parallel()
now := time.Now()
ch := make(chan struct{})
called := make(chan struct{})
received := make(chan struct{})
go func() {
JitterUntil(func() {
called <- struct{}{}
<-received
}, time.Second, -30.0, true, ch)
}()
// first loop
<-called
received <- struct{}{}
// second loop
<-called
close(ch)
received <- struct{}{}
// it should take at most 2 seconds + some overhead, not 3
if now.Add(3 * time.Second).Before(time.Now()) {
t.Errorf("JitterUntil did not returned after predefined period with negative jitter factor when the stop chan was closed inside the func")
}
}
func TestExponentialBackoff(t *testing.T) {
t.Parallel()
opts := Backoff{Factor: 1.0, Steps: 3}
// waits up to steps
i := 0
err := ExponentialBackoff(opts, func() (bool, error) {
i++
return false, nil
})
if err != ErrWaitTimeout || i != opts.Steps {
t.Errorf("unexpected error: %v", err)
}
// returns immediately
i = 0
err = ExponentialBackoff(opts, func() (bool, error) {
i++
return true, nil
})
if err != nil || i != 1 {
t.Errorf("unexpected error: %v", err)
}
// returns immediately on error
testErr := fmt.Errorf("some other error")
err = ExponentialBackoff(opts, func() (bool, error) {
return false, testErr
})
if err != testErr {
t.Errorf("unexpected error: %v", err)
}
// invoked multiple times
i = 1
err = ExponentialBackoff(opts, func() (bool, error) {
if i < opts.Steps {
i++
return false, nil
}
return true, nil
})
if err != nil || i != opts.Steps {
t.Errorf("unexpected error: %v", err)
}
}
func TestPoller(t *testing.T) {
t.Parallel()
done := make(chan struct{})
defer close(done)
w := poller(time.Millisecond, 2*time.Millisecond)
ch := w(done)
count := 0
DRAIN:
for {
select {
case _, open := <-ch:
if !open {
break DRAIN
}
count++
case <-time.After(ForeverTestTimeout):
t.Errorf("unexpected timeout after poll")
}
}
if count > 3 {
t.Errorf("expected up to three values, got %d", count)
}
}
type fakePoller struct {
max int
used int32 // accessed with atomics
wg sync.WaitGroup
}
func fakeTicker(max int, used *int32, doneFunc func()) Func {
return func(done <-chan struct{}) <-chan struct{} {
ch := make(chan struct{})
go func() {
defer doneFunc()
defer close(ch)
for i := 0; i < max; i++ {
select {
case ch <- struct{}{}:
case <-done:
return
}
if used != nil {
atomic.AddInt32(used, 1)
}
}
}()
return ch
}
}
func (fp *fakePoller) GetFunc() Func {
fp.wg.Add(1)
return fakeTicker(fp.max, &fp.used, fp.wg.Done)
}
func TestPoll(t *testing.T) {
t.Parallel()
invocations := 0
f := ConditionFunc(func() (bool, error) {
invocations++
return true, nil
})
fp := fakePoller{max: 1}
if err := pollInternal(fp.GetFunc(), f); err != nil {
t.Fatalf("unexpected error %v", err)
}
fp.wg.Wait()
if invocations != 1 {
t.Errorf("Expected exactly one invocation, got %d", invocations)
}
used := atomic.LoadInt32(&fp.used)
if used != 1 {
t.Errorf("Expected exactly one tick, got %d", used)
}
}
func TestPollError(t *testing.T) {
t.Parallel()
expectedError := errors.New("Expected error")
f := ConditionFunc(func() (bool, error) {
return false, expectedError
})
fp := fakePoller{max: 1}
if err := pollInternal(fp.GetFunc(), f); err == nil || err != expectedError {
t.Fatalf("Expected error %v, got none %v", expectedError, err)
}
fp.wg.Wait()
used := atomic.LoadInt32(&fp.used)
if used != 1 {
t.Errorf("Expected exactly one tick, got %d", used)
}
}
func TestPollImmediate(t *testing.T) {
t.Parallel()
invocations := 0
f := ConditionFunc(func() (bool, error) {
invocations++
return true, nil
})
fp := fakePoller{max: 0}
if err := pollImmediateInternal(fp.GetFunc(), f); err != nil {
t.Fatalf("unexpected error %v", err)
}
// We don't need to wait for fp.wg, as pollImmediate shouldn't call Func at all.
if invocations != 1 {
t.Errorf("Expected exactly one invocation, got %d", invocations)
}
used := atomic.LoadInt32(&fp.used)
if used != 0 {
t.Errorf("Expected exactly zero ticks, got %d", used)
}
}
func TestPollImmediateError(t *testing.T) {
t.Parallel()
expectedError := errors.New("Expected error")
f := ConditionFunc(func() (bool, error) {
return false, expectedError
})
fp := fakePoller{max: 0}
if err := pollImmediateInternal(fp.GetFunc(), f); err == nil || err != expectedError {
t.Fatalf("Expected error %v, got none %v", expectedError, err)
}
// We don't need to wait for fp.wg, as pollImmediate shouldn't call Func at all.
used := atomic.LoadInt32(&fp.used)
if used != 0 {
t.Errorf("Expected exactly zero ticks, got %d", used)
}
}
func TestPollForever(t *testing.T) {
t.Parallel()
ch := make(chan struct{})
done := make(chan struct{}, 1)
complete := make(chan struct{})
go func() {
f := ConditionFunc(func() (bool, error) {
ch <- struct{}{}
select {
case <-done:
return true, nil
default:
}
return false, nil
})
if err := PollInfinite(time.Microsecond, f); err != nil {
t.Fatalf("unexpected error %v", err)
}
close(ch)
complete <- struct{}{}
}()
// ensure the condition is opened
<-ch
// ensure channel sends events
for i := 0; i < 10; i++ {
select {
case _, open := <-ch:
if !open {
t.Fatalf("did not expect channel to be closed")
}
case <-time.After(ForeverTestTimeout):
t.Fatalf("channel did not return at least once within the poll interval")
}
}
// at most one poll notification should be sent once we return from the condition
done <- struct{}{}
go func() {
for i := 0; i < 2; i++ {
_, open := <-ch
if !open {
return
}
}
t.Fatalf("expected closed channel after two iterations")
}()
<-complete
}
func TestFor(t *testing.T) {
t.Parallel()
var invocations int
testCases := map[string]struct {
F ConditionFunc
Ticks int
Invoked int
Err bool
}{
"invoked once": {
func() (bool, error) {
invocations++
return true, nil
},
2,
1,
false,
},
"invoked and returns a timeout": {
func() (bool, error) {
invocations++
return false, nil
},
2,
3, // the contract of For() says the func is called once more at the end of the wait
true,
},
"returns immediately on error": {
func() (bool, error) {
invocations++
return false, errors.New("test")
},
2,
1,
true,
},
}
for k, c := range testCases {
invocations = 0
ticker := fakeTicker(c.Ticks, nil, func() {})
err := func() error {
done := make(chan struct{})
defer close(done)
return For(ticker, c.F, done)
}()
switch {
case c.Err && err == nil:
t.Errorf("%s: Expected error, got nil", k)
continue
case !c.Err && err != nil:
t.Errorf("%s: Expected no error, got: %#v", k, err)
continue
}
if invocations != c.Invoked {
t.Errorf("%s: Expected %d invocations, got %d", k, c.Invoked, invocations)
}
}
}
func TestForWithDelay(t *testing.T) {
t.Parallel()
done := make(chan struct{})
defer close(done)
For(poller(time.Millisecond, ForeverTestTimeout), func() (bool, error) {
time.Sleep(10 * time.Millisecond)
return true, nil
}, done)
// If polling goroutine doesn't see the done signal it will leak timers.
select {
case done <- struct{}{}:
case <-time.After(ForeverTestTimeout):
t.Errorf("expected an ack of the done signal.")
}
}
func TestPollUntil(t *testing.T) {
t.Parallel()
stopCh := make(chan struct{})
called := make(chan bool)
pollDone := make(chan struct{})
go func() {
PollUntil(time.Microsecond, ConditionFunc(func() (bool, error) {
called <- true
return false, nil
}), stopCh)
close(pollDone)
}()
// make sure we're called once
<-called
// this should trigger a "done"
close(stopCh)
go func() {
// release the condition func if needed
for {
<-called
}
}()
// make sure we finished the poll
<-pollDone
}