-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_eventually_test.go
More file actions
786 lines (590 loc) · 16.8 KB
/
Copy pathassert_eventually_test.go
File metadata and controls
786 lines (590 loc) · 16.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package testastic_test
import (
"errors"
"os"
"os/exec"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/monkescience/testastic"
)
func TestEventually(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a condition that is immediately true
// when: calling Eventually
// then: it succeeds without waiting
start := time.Now()
testastic.Eventually(t, func() bool {
return true
}, 1*time.Second)
elapsed := time.Since(start)
if elapsed > 50*time.Millisecond {
t.Errorf("expected immediate success, took %v", elapsed)
}
})
t.Run("success after retries", func(t *testing.T) {
t.Parallel()
// given: a condition that becomes true after a few attempts
var counter atomic.Int32
condition := func() bool {
return counter.Add(1) >= 3
}
// when: calling Eventually with short interval
testastic.Eventually(t, condition, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
// then: it succeeds after retries
if counter.Load() < 3 {
t.Errorf("expected at least 3 calls, got %d", counter.Load())
}
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a condition that never becomes true
mt := &mockT{}
// when: calling Eventually with short timeout
testastic.Eventually(mt, func() bool {
return false
}, 100*time.Millisecond, testastic.WithInterval(20*time.Millisecond))
// then: it fails with timeout error
if !mt.failed {
t.Error("expected Eventually to fail on timeout")
}
if !strings.Contains(mt.message, "timed out") {
t.Errorf("expected timeout message, got: %s", mt.message)
}
})
t.Run("with message", func(t *testing.T) {
t.Parallel()
// given: a condition that fails with a custom message
mt := &mockT{}
// when: calling Eventually with WithMessage
testastic.Eventually(mt, func() bool {
return false
}, 50*time.Millisecond, testastic.WithMessage("waiting for server"))
// then: the failure message includes the custom message
if !mt.failed {
t.Error("expected Eventually to fail")
}
if !strings.Contains(mt.message, "waiting for server") {
t.Errorf("expected custom message, got: %s", mt.message)
}
})
t.Run("with interval", func(t *testing.T) {
t.Parallel()
// given: a condition that counts calls
var counter atomic.Int32
condition := func() bool {
counter.Add(1)
return false
}
mt := &mockT{}
// when: calling Eventually with 50ms interval for 150ms
testastic.Eventually(mt, condition, 150*time.Millisecond, testastic.WithInterval(50*time.Millisecond))
// then: it should be called approximately 3-4 times (initial + 2-3 retries)
calls := counter.Load()
if calls < 2 || calls > 5 {
t.Errorf("expected 2-5 calls with 50ms interval over 150ms, got %d", calls)
}
})
t.Run("zero interval does not panic", func(t *testing.T) {
t.Parallel()
// given: a zero polling interval
mt := &mockT{}
defer func() {
if recovered := recover(); recovered != nil {
t.Fatalf("expected Eventually not to panic, recovered: %v", recovered)
}
}()
// when: calling Eventually with a zero interval
testastic.Eventually(mt, func() bool {
return false
}, time.Millisecond, testastic.WithInterval(0))
// then: it fails normally on timeout
if !mt.failed {
t.Error("expected Eventually to fail on timeout")
}
})
t.Run("timeout wins over interval", func(t *testing.T) {
// given: a timeout much shorter than the polling interval
mt := &mockT{}
start := time.Now()
// when: calling Eventually with a condition that never passes
testastic.Eventually(mt, func() bool {
return false
}, 30*time.Millisecond, testastic.WithInterval(500*time.Millisecond))
// then: it times out near the deadline instead of waiting for the interval
elapsed := time.Since(start)
if !mt.failed {
t.Error("expected Eventually to fail on timeout")
}
if elapsed > 300*time.Millisecond {
t.Errorf("expected timeout before polling interval, took %v", elapsed)
}
})
}
func TestEventuallyTrue(t *testing.T) {
t.Parallel()
t.Run("pass", func(t *testing.T) {
t.Parallel()
// given: a condition that becomes true
var ready atomic.Bool
go func() {
time.Sleep(50 * time.Millisecond)
ready.Store(true)
}()
// when: calling EventuallyTrue
// then: it succeeds when the condition becomes true
testastic.EventuallyTrue(t, ready.Load, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
}
func TestEventuallyFalse(t *testing.T) {
t.Parallel()
t.Run("pass", func(t *testing.T) {
t.Parallel()
// given: a condition that becomes false
var processing atomic.Bool
processing.Store(true)
go func() {
time.Sleep(50 * time.Millisecond)
processing.Store(false)
}()
// when: calling EventuallyFalse
// then: it succeeds when the condition becomes false
testastic.EventuallyFalse(t, processing.Load, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a condition that stays true
mt := &mockT{}
// when: calling EventuallyFalse
testastic.EventuallyFalse(mt, func() bool {
return true
}, 50*time.Millisecond)
// then: it fails on timeout
if !mt.failed {
t.Error("expected EventuallyFalse to fail")
}
})
}
func TestEventuallyEqual(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a value that immediately matches
// when: calling EventuallyEqual
// then: it succeeds immediately
testastic.EventuallyEqual(t, "ready", func() string {
return "ready"
}, 1*time.Second)
})
t.Run("success after change", func(t *testing.T) {
t.Parallel()
// given: a value that changes to match expected
var status atomic.Value
status.Store("starting")
go func() {
time.Sleep(50 * time.Millisecond)
status.Store("ready")
}()
// when: calling EventuallyEqual
// then: it succeeds when the value matches
testastic.EventuallyEqual(t, "ready", func() string {
v, ok := status.Load().(string)
if !ok {
return ""
}
return v
}, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a value that never matches
mt := &mockT{}
// when: calling EventuallyEqual
testastic.EventuallyEqual(mt, "expected", func() string {
return "actual"
}, 50*time.Millisecond)
// then: it fails with expected and actual values
if !mt.failed {
t.Error("expected EventuallyEqual to fail")
}
if !strings.Contains(mt.message, "expected") {
t.Errorf("expected message to contain 'expected', got: %s", mt.message)
}
if !strings.Contains(mt.message, "actual") {
t.Errorf("expected message to contain 'actual', got: %s", mt.message)
}
})
t.Run("integer", func(t *testing.T) {
t.Parallel()
// given: an integer counter
var counter atomic.Int32
go func() {
for range 5 {
time.Sleep(20 * time.Millisecond)
counter.Add(1)
}
}()
// when: calling EventuallyEqual with integer type
// then: it succeeds when the value matches
testastic.EventuallyEqual(t, int32(5), counter.Load, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
}
func TestEventuallyNil(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a value that is immediately nil
// when: calling EventuallyNil
// then: it succeeds immediately
testastic.EventuallyNil(t, func() any {
return nil
}, 1*time.Second)
})
t.Run("success after clear", func(t *testing.T) {
t.Parallel()
// given: a value that becomes nil (using pointer to track nil state)
var cleared atomic.Bool
go func() {
time.Sleep(50 * time.Millisecond)
cleared.Store(true)
}()
// when: calling EventuallyNil
// then: it succeeds when the value becomes nil
testastic.EventuallyNil(t, func() any {
if cleared.Load() {
return nil
}
return "something"
}, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a value that is never nil
mt := &mockT{}
// when: calling EventuallyNil
testastic.EventuallyNil(mt, func() any {
return "not nil"
}, 50*time.Millisecond)
// then: it fails on timeout
if !mt.failed {
t.Error("expected EventuallyNil to fail")
}
})
}
func TestEventuallyNotNil(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a value that is immediately not nil
// when: calling EventuallyNotNil
// then: it succeeds immediately
testastic.EventuallyNotNil(t, func() any {
return "value"
}, 1*time.Second)
})
t.Run("success after set", func(t *testing.T) {
t.Parallel()
// given: a value that becomes non-nil
var value atomic.Value
go func() {
time.Sleep(50 * time.Millisecond)
value.Store("something")
}()
// when: calling EventuallyNotNil
// then: it succeeds when the value becomes non-nil
testastic.EventuallyNotNil(t, value.Load, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a value that is always nil
mt := &mockT{}
// when: calling EventuallyNotNil
testastic.EventuallyNotNil(mt, func() any {
return nil
}, 50*time.Millisecond)
// then: it fails on timeout
if !mt.failed {
t.Error("expected EventuallyNotNil to fail")
}
})
}
func TestEventuallyNoError(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a function that returns no error
// when: calling EventuallyNoError
// then: it succeeds immediately
testastic.EventuallyNoError(t, func() error {
return nil
}, 1*time.Second)
})
t.Run("success after recovery", func(t *testing.T) {
t.Parallel()
// given: a function that eventually succeeds
var counter atomic.Int32
getErr := func() error {
if counter.Add(1) < 3 {
return errors.New("not ready")
}
return nil
}
// when: calling EventuallyNoError
// then: it succeeds after retries
testastic.EventuallyNoError(t, getErr, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a function that always returns an error
mt := &mockT{}
// when: calling EventuallyNoError
testastic.EventuallyNoError(mt, func() error {
return errors.New("persistent error")
}, 50*time.Millisecond)
// then: it fails and shows the last error
if !mt.failed {
t.Error("expected EventuallyNoError to fail")
}
if !strings.Contains(mt.message, "persistent error") {
t.Errorf("expected message to contain last error, got: %s", mt.message)
}
})
}
func TestEventuallyError(t *testing.T) {
t.Parallel()
t.Run("immediate success", func(t *testing.T) {
t.Parallel()
// given: a function that returns an error
// when: calling EventuallyError
// then: it succeeds immediately
testastic.EventuallyError(t, func() error {
return errors.New("expected error")
}, 1*time.Second)
})
t.Run("success after failure", func(t *testing.T) {
t.Parallel()
// given: a function that eventually fails
var counter atomic.Int32
getErr := func() error {
if counter.Add(1) < 3 {
return nil
}
return errors.New("service unhealthy")
}
// when: calling EventuallyError
// then: it succeeds when an error is returned
testastic.EventuallyError(t, getErr, 1*time.Second, testastic.WithInterval(10*time.Millisecond))
})
t.Run("timeout", func(t *testing.T) {
t.Parallel()
// given: a function that never returns an error
mt := &mockT{}
// when: calling EventuallyError
testastic.EventuallyError(mt, func() error {
return nil
}, 50*time.Millisecond)
// then: it fails on timeout
if !mt.failed {
t.Error("expected EventuallyError to fail")
}
})
}
func TestEventually_BoundsBlockingConditionByTimeout(t *testing.T) {
t.Parallel()
const (
timeout = 20 * time.Millisecond
maximumWait = 500 * time.Millisecond
)
release := make(chan struct{})
firstCheckStarted := make(chan struct{})
firstCheckDone := make(chan struct{})
var calls atomic.Int32
mt := &mockT{}
done := make(chan struct{})
go func() {
testastic.Eventually(mt, func() bool {
call := calls.Add(1)
if call == 1 {
close(firstCheckStarted)
defer close(firstCheckDone)
}
<-release
return false
}, timeout, testastic.WithInterval(time.Millisecond))
close(done)
}()
<-firstCheckStarted
timer := time.NewTimer(maximumWait)
defer timer.Stop()
returnedBeforeLimit := false
select {
case <-done:
returnedBeforeLimit = true
case <-timer.C:
}
close(release)
<-firstCheckDone
if !returnedBeforeLimit {
<-done
t.Error("Eventually did not return when its timeout elapsed")
}
if !mt.failed {
t.Error("expected Eventually to fail on timeout")
}
if got := calls.Load(); got != 1 {
t.Errorf("expected one condition check, got %d", got)
}
}
func TestEventually_RejectsNonPositiveTimeout(t *testing.T) {
previousMaxProcs := runtime.GOMAXPROCS(1)
defer runtime.GOMAXPROCS(previousMaxProcs)
timeouts := []time.Duration{0, -time.Second}
for _, timeout := range timeouts {
t.Run(timeout.String(), func(t *testing.T) {
mt := &mockT{}
var calls atomic.Int32
testastic.Eventually(mt, func() bool {
calls.Add(1)
return true
}, timeout)
runtime.Gosched()
if got := calls.Load(); got != 0 {
t.Errorf("condition calls = %d, want 0", got)
}
if !mt.failed {
t.Error("expected nonpositive timeout to fail")
}
if !strings.Contains(mt.message, "timeout must be greater than zero") {
t.Errorf("unexpected failure message: %s", mt.message)
}
})
}
}
func TestEventually_PropagatesConditionPanic(t *testing.T) {
t.Parallel()
const panicValue = "condition panic"
var recovered any
func() {
defer func() {
recovered = recover()
}()
testastic.Eventually(&mockT{}, func() bool {
panic(panicValue)
}, time.Second)
}()
if recovered != panicValue {
t.Errorf("recovered panic = %v, want %q", recovered, panicValue)
}
}
func TestEventually_PropagatesNilConditionPanic(t *testing.T) {
t.Parallel()
const maximumWait = 500 * time.Millisecond
var eventuallyReturned atomic.Bool
var continuedAfterRecover atomic.Bool
done := make(chan struct{})
go func() {
defer close(done)
func() {
defer func() {
_ = recover()
}()
testastic.Eventually(&mockT{}, func() bool {
panic(nil)
}, time.Second)
eventuallyReturned.Store(true)
}()
continuedAfterRecover.Store(true)
}()
timer := time.NewTimer(maximumWait)
defer timer.Stop()
select {
case <-done:
case <-timer.C:
t.Fatal("Eventually did not propagate nil condition panic")
}
if eventuallyReturned.Load() {
t.Error("Eventually returned after a nil condition panic")
}
if !continuedAfterRecover.Load() {
t.Error("nil condition panic was treated as Goexit")
}
}
func TestEventually_PreservesLateConditionPanic(t *testing.T) {
const (
helperEnv = "TESTASTIC_EVENTUALLY_LATE_PANIC"
panicValue = "late condition panic"
callbackDelay = 50 * time.Millisecond
)
if os.Getenv(helperEnv) == "1" {
started := make(chan struct{})
testastic.Eventually(&mockT{}, func() bool {
close(started)
time.Sleep(callbackDelay)
panic(panicValue)
}, 10*time.Millisecond)
<-started
time.Sleep(2 * callbackDelay)
return
}
cmd := exec.CommandContext(
t.Context(), os.Args[0], "-test.run=^TestEventually_PreservesLateConditionPanic$",
)
cmd.Env = append(os.Environ(), helperEnv+"=1")
output, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("late condition panic was discarded, subprocess output:\n%s", output)
}
if !strings.Contains(string(output), panicValue) {
t.Errorf("subprocess output did not contain panic %q:\n%s", panicValue, output)
}
}
func TestEventually_PropagatesConditionGoexit(t *testing.T) {
t.Parallel()
const (
timeout = 20 * time.Millisecond
maximumWait = 500 * time.Millisecond
)
mt := &mockT{}
var returned atomic.Bool
done := make(chan struct{})
go func() {
defer close(done)
testastic.Eventually(mt, func() bool {
runtime.Goexit()
return false
}, timeout)
returned.Store(true)
}()
timer := time.NewTimer(maximumWait)
defer timer.Stop()
select {
case <-done:
case <-timer.C:
t.Fatal("Eventually did not propagate condition Goexit")
}
if returned.Load() {
t.Error("Eventually returned after its condition called Goexit")
}
if mt.failed {
t.Errorf("Eventually reported a timeout after condition Goexit: %s", mt.message)
}
}
func TestEventually_DoesNotStartCheckAtTimeout(t *testing.T) {
t.Parallel()
var calls atomic.Int32
mt := &mockT{}
testastic.Eventually(mt, func() bool {
calls.Add(1)
return false
}, 20*time.Millisecond, testastic.WithInterval(time.Hour))
if !mt.failed {
t.Error("expected Eventually to fail on timeout")
}
if got := calls.Load(); got != 1 {
t.Errorf("expected one condition check, got %d", got)
}
}