/
async_assertion_test.go
1515 lines (1374 loc) · 55.2 KB
/
async_assertion_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
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package internal_test
import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
)
type quickMatcher struct {
matchFunc func(actual any) (bool, error)
oracleFunc func(actual any) bool
}
func (q quickMatcher) Match(actual any) (bool, error) {
return q.matchFunc(actual)
}
func (q quickMatcher) FailureMessage(actual any) (message string) {
return "QM failure message"
}
func (q quickMatcher) NegatedFailureMessage(actual any) (message string) {
return "QM negated failure message"
}
func (q quickMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
if q.oracleFunc == nil {
return true
}
return q.oracleFunc(actual)
}
func QuickMatcher(matchFunc func(actual any) (bool, error)) OmegaMatcher {
return quickMatcher{matchFunc, nil}
}
func QuickMatcherWithOracle(matchFunc func(actual any) (bool, error), oracleFunc func(actual any) bool) OmegaMatcher {
return quickMatcher{matchFunc, oracleFunc}
}
type FakeGinkgoSpecContext struct {
Attached func() string
Cancelled bool
}
func (f *FakeGinkgoSpecContext) AttachProgressReporter(v func() string) func() {
f.Attached = v
return func() { f.Cancelled = true }
}
var _ = Describe("Asynchronous Assertions", func() {
var ig *InstrumentedGomega
BeforeEach(func() {
ig = NewInstrumentedGomega()
})
Describe("Basic Eventually support", func() {
Context("the positive case", func() {
It("polls the function and matcher until a match occurs", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 5 {
return MATCH
}
return NO_MATCH
}).Should(SpecMatch())
Ω(counter).Should(Equal(6))
Ω(ig.FailureMessage).Should(BeZero())
})
It("continues polling even if the matcher errors", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 5 {
return MATCH
}
return ERR_MATCH
}).Should(SpecMatch())
Ω(counter).Should(Equal(6))
Ω(ig.FailureMessage).Should(BeZero())
})
It("times out eventually if the assertion doesn't match in time", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 100 {
return MATCH
}
return NO_MATCH
}).WithTimeout(200 * time.Millisecond).WithPolling(20 * time.Millisecond).Should(SpecMatch())
Ω(counter).Should(BeNumerically(">", 2))
Ω(counter).Should(BeNumerically("<", 20))
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
Ω(ig.FailureSkip).Should(Equal([]int{3}))
})
It("maps Within() correctly to timeout and polling intervals", func() {
counter := 0
ig.G.Eventually(func() bool {
counter++
return false
}).WithTimeout(0).WithPolling(20 * time.Millisecond).Within(200 * time.Millisecond).Should(BeTrue())
Ω(counter).Should(BeNumerically(">", 2))
Ω(counter).Should(BeNumerically("<", 20))
counter = 0
ig.G.Eventually(func() bool {
counter++
return false
}).WithTimeout(0).WithPolling(0). // first zero intervals, then set them
Within(200 * time.Millisecond).ProbeEvery(20 * time.Millisecond).
Should(BeTrue())
Ω(counter).Should(BeNumerically(">", 2))
Ω(counter).Should(BeNumerically("<", 20))
})
})
Context("the negative case", func() {
It("polls the function and matcher until a match does not occur", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 5 {
return NO_MATCH
}
return MATCH
}).ShouldNot(SpecMatch())
Ω(counter).Should(Equal(6))
Ω(ig.FailureMessage).Should(BeZero())
})
It("continues polling when the matcher errors - an error does not count as a successful non-match", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 5 {
return NO_MATCH
}
return ERR_MATCH
}).ShouldNot(SpecMatch())
Ω(counter).Should(Equal(6))
Ω(ig.FailureMessage).Should(BeZero())
})
It("times out eventually if the assertion doesn't match in time", func() {
counter := 0
ig.G.Eventually(func() string {
counter++
if counter > 100 {
return NO_MATCH
}
return MATCH
}).WithTimeout(200 * time.Millisecond).WithPolling(20 * time.Millisecond).ShouldNot(SpecMatch())
Ω(counter).Should(BeNumerically(">", 2))
Ω(counter).Should(BeNumerically("<", 20))
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
Ω(ig.FailureMessage).Should(ContainSubstring("negative: match"))
Ω(ig.FailureSkip).Should(Equal([]int{3}))
})
})
Context("when a failure occurs", func() {
It("registers the appropriate helper functions", func() {
ig.G.Eventually(NO_MATCH).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
Ω(ig.FailureSkip).Should(Equal([]int{3}))
Ω(ig.RegisteredHelpers).Should(ContainElement("(*AsyncAssertion).Should"))
Ω(ig.RegisteredHelpers).Should(ContainElement("(*AsyncAssertion).match"))
})
It("renders the matcher's error if an error occured", func() {
ig.G.Eventually(ERR_MATCH).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: spec matcher error"))
})
It("renders the optional description", func() {
ig.G.Eventually(NO_MATCH).WithTimeout(50*time.Millisecond).WithPolling(10*time.Millisecond).Should(SpecMatch(), "boop")
Ω(ig.FailureMessage).Should(ContainSubstring("boop"))
})
It("formats and renders the optional description when there are multiple arguments", func() {
ig.G.Eventually(NO_MATCH).WithTimeout(50*time.Millisecond).WithPolling(10*time.Millisecond).Should(SpecMatch(), "boop %d", 17)
Ω(ig.FailureMessage).Should(ContainSubstring("boop 17"))
})
It("calls the optional description if it is a function", func() {
ig.G.Eventually(NO_MATCH).WithTimeout(50*time.Millisecond).WithPolling(10*time.Millisecond).Should(SpecMatch(), func() string { return "boop" })
Ω(ig.FailureMessage).Should(ContainSubstring("boop"))
})
})
Context("with a passed-in context", func() {
Context("when the passed-in context is cancelled", func() {
It("stops and returns a failure", func() {
ctx, cancel := context.WithCancel(context.Background())
counter := 0
ig.G.Eventually(func() string {
counter++
if counter == 2 {
cancel()
} else if counter == 10 {
return MATCH
}
return NO_MATCH
}, time.Hour, ctx).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
})
It("can also be configured via WithContext()", func() {
ctx, cancel := context.WithCancel(context.Background())
counter := 0
ig.G.Eventually(func() string {
counter++
if counter == 2 {
cancel()
} else if counter == 10 {
return MATCH
}
return NO_MATCH
}, time.Hour).WithContext(ctx).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
})
It("can also be configured with the context up front", func() {
ctx, cancel := context.WithCancel(context.Background())
counter := 0
ig.G.Eventually(ctx, func() string {
counter++
if counter == 2 {
cancel()
} else if counter == 10 {
return MATCH
}
return NO_MATCH
}, time.Hour).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
})
It("treats a leading context as an actual, even if valid durations are passed in", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Eventually(ctx).Should(Equal(ctx))
Eventually(ctx, 0.1).Should(Equal(ctx))
})
It("counts as a failure for Consistently", func() {
ctx, cancel := context.WithCancel(context.Background())
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 2 {
cancel()
} else if counter == 10 {
return NO_MATCH
}
return MATCH
}, time.Hour).WithContext(ctx).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: match"))
})
})
Context("when the passed-in context is a Ginkgo SpecContext that can take a progress reporter attachment", func() {
It("attaches a progress reporter context that allows it to report on demand", func() {
fakeSpecContext := &FakeGinkgoSpecContext{}
var message string
ctx := context.WithValue(context.Background(), "GINKGO_SPEC_CONTEXT", fakeSpecContext)
ig.G.Eventually(func() string {
if fakeSpecContext.Attached != nil {
message = fakeSpecContext.Attached()
}
return NO_MATCH
}).WithTimeout(time.Millisecond * 20).WithContext(ctx).Should(Equal(MATCH))
Ω(message).Should(Equal("Expected\n <string>: no match\nto equal\n <string>: match"))
Ω(fakeSpecContext.Cancelled).Should(BeTrue())
})
})
Describe("the interaction between the context and the timeout", func() {
It("only relies on context cancellation when no explicit timeout is specified", func() {
ig.G.SetDefaultEventuallyTimeout(time.Millisecond * 10)
ig.G.SetDefaultEventuallyPollingInterval(time.Millisecond * 40)
t := time.Now()
ctx, cancel := context.WithCancel(context.Background())
iterations := 0
ig.G.Eventually(func() string {
iterations += 1
if time.Since(t) > time.Millisecond*200 {
cancel()
}
return "A"
}).WithContext(ctx).Should(Equal("B"))
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*200, time.Millisecond*100))
Ω(iterations).Should(BeNumerically("~", 200/40, 2))
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
})
It("uses the explicit timeout when it is provided", func() {
t := time.Now()
ctx, cancel := context.WithCancel(context.Background())
iterations := 0
ig.G.Eventually(func() string {
iterations += 1
if time.Since(t) > time.Millisecond*200 {
cancel()
}
return "A"
}).WithContext(ctx).WithTimeout(time.Millisecond * 80).ProbeEvery(time.Millisecond * 40).Should(Equal("B"))
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*80, time.Millisecond*40))
Ω(iterations).Should(BeNumerically("~", 80/40, 2))
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
})
})
})
})
Describe("Basic Consistently support", func() {
Context("the positive case", func() {
It("polls the function and matcher ensuring a match occurs consistently", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
return MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
Ω(counter).Should(BeNumerically(">", 1))
Ω(counter).Should(BeNumerically("<", 7))
Ω(ig.FailureMessage).Should(BeZero())
})
It("fails if the matcher ever errors", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 3 {
return ERR_MATCH
}
return MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
Ω(counter).Should(Equal(3))
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: spec matcher error"))
})
It("fails if the matcher doesn't match at any point", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 3 {
return NO_MATCH
}
return MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(SpecMatch())
Ω(counter).Should(Equal(3))
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
})
})
Context("the negative case", func() {
It("polls the function and matcher ensuring a match never occurs", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
return NO_MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(SpecMatch())
Ω(counter).Should(BeNumerically(">", 1))
Ω(counter).Should(BeNumerically("<", 7))
Ω(ig.FailureMessage).Should(BeZero())
})
It("fails if the matcher ever errors", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 3 {
return ERR_MATCH
}
return NO_MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(SpecMatch())
Ω(counter).Should(Equal(3))
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: spec matcher error"))
})
It("fails if the matcher matches at any point", func() {
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 3 {
return MATCH
}
return NO_MATCH
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(SpecMatch())
Ω(counter).Should(Equal(3))
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("negative: match"))
})
})
Context("when a failure occurs", func() {
It("registers the appropriate helper functions", func() {
ig.G.Consistently(NO_MATCH).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
Ω(ig.FailureSkip).Should(Equal([]int{3}))
Ω(ig.RegisteredHelpers).Should(ContainElement("(*AsyncAssertion).Should"))
Ω(ig.RegisteredHelpers).Should(ContainElement("(*AsyncAssertion).match"))
})
It("renders the matcher's error if an error occured", func() {
ig.G.Consistently(ERR_MATCH).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: spec matcher error"))
})
It("renders the optional description", func() {
ig.G.Consistently(NO_MATCH).Should(SpecMatch(), "boop")
Ω(ig.FailureMessage).Should(ContainSubstring("boop"))
})
It("formats and renders the optional description when there are multiple arguments", func() {
ig.G.Consistently(NO_MATCH).Should(SpecMatch(), "boop %d", 17)
Ω(ig.FailureMessage).Should(ContainSubstring("boop 17"))
})
It("calls the optional description if it is a function", func() {
ig.G.Consistently(NO_MATCH).Should(SpecMatch(), func() string { return "boop" })
Ω(ig.FailureMessage).Should(ContainSubstring("boop"))
})
})
Context("with a passed-in context", func() {
Context("when the passed-in context is cancelled", func() {
It("counts as a failure for Consistently", func() {
ctx, cancel := context.WithCancel(context.Background())
counter := 0
ig.G.Consistently(func() string {
counter++
if counter == 2 {
cancel()
} else if counter == 10 {
return NO_MATCH
}
return MATCH
}, time.Hour).WithContext(ctx).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: match"))
})
})
Describe("the interaction between the context and the timeout", func() {
It("only always uses the default interval even if not explicit duration is provided", func() {
ig.G.SetDefaultConsistentlyDuration(time.Millisecond * 200)
ig.G.SetDefaultConsistentlyPollingInterval(time.Millisecond * 40)
t := time.Now()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
iterations := 0
ig.G.Consistently(func() string {
iterations += 1
return "A"
}).WithContext(ctx).Should(Equal("A"))
Ω(time.Since(t)).Should(BeNumerically("~", time.Millisecond*200, time.Millisecond*100))
Ω(iterations).Should(BeNumerically("~", 200/40, 2))
Ω(ig.FailureMessage).Should(BeZero())
})
})
})
})
Describe("the passed-in actual", func() {
type Foo struct{ Bar string }
Context("when passed a value", func() {
It("(eventually) continuously checks on the value until a match occurs", func() {
c := make(chan bool)
go func() {
time.Sleep(100 * time.Millisecond)
close(c)
}()
ig.G.Eventually(c).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(BeClosed())
Ω(ig.FailureMessage).Should(BeZero())
})
It("(consistently) continuously checks on the value ensuring a match always occurs", func() {
c := make(chan bool)
close(c)
ig.G.Consistently(c).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeClosed())
Ω(ig.FailureMessage).Should(BeZero())
})
})
Context("when passed a function that takes no arguments and returns one value", func() {
It("(eventually) polls the function until the returned value satisfies the matcher", func() {
counter := 0
ig.G.Eventually(func() int {
counter += 1
return counter
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(BeNumerically(">", 5))
Ω(ig.FailureMessage).Should(BeZero())
})
It("(consistently) polls the function ensuring the returned value satisfies the matcher", func() {
counter := 0
ig.G.Consistently(func() int {
counter += 1
return counter
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 20))
Ω(counter).Should(BeNumerically(">", 2))
Ω(ig.FailureMessage).Should(BeZero())
})
It("works when the function returns nil", func() {
counter := 0
ig.G.Eventually(func() error {
counter += 1
if counter > 5 {
return nil
}
return errors.New("oops")
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(BeNil())
Ω(ig.FailureMessage).Should(BeZero())
})
})
Context("when passed a function that takes no arguments and returns multiple values", func() {
Context("with Eventually", func() {
It("polls the function until the first returned value satisfies the matcher _and_ all additional values are zero", func() {
counter, s, f, err := 0, "hi", Foo{Bar: "hi"}, errors.New("hi")
ig.G.Eventually(func() (int, string, Foo, error) {
switch counter += 1; counter {
case 2:
s = ""
case 3:
f = Foo{}
case 4:
err = nil
}
return counter, s, f, err
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(Equal(4))
})
It("reports on the non-zero value if it times out", func() {
ig.G.Eventually(func() (int, string, Foo, error) {
return 1, "", Foo{Bar: "hi"}, nil
}).WithTimeout(30 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Unexpected non-nil/non-zero return value at index 2:"))
Ω(ig.FailureMessage).Should(ContainSubstring(`Foo{Bar:"hi"}`))
})
It("has a meaningful message if all the return values are zero except the final return value, and it is an error", func() {
ig.G.Eventually(func() (int, string, Foo, error) {
return 1, "", Foo{}, errors.New("welp!")
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: function returned error: welp!"))
})
Context("when making a ShouldNot assertion", func() {
It("doesn't succeed until the matcher is (not) satisfied with the first returned value _and_ all additional values are zero", func() {
counter, s, f, err := 0, "hi", Foo{Bar: "hi"}, errors.New("hi")
ig.G.Eventually(func() (int, string, Foo, error) {
switch counter += 1; counter {
case 2:
s = ""
case 3:
f = Foo{}
case 4:
err = nil
}
return counter, s, f, err
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).ShouldNot(BeNumerically("<", 0))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(Equal(4))
})
})
})
Context("with Consistently", func() {
It("polls the function and succeeds if all the values are zero and the matcher is consistently satisfied", func() {
var err error
counter, s, f := 0, "", Foo{}
ig.G.Consistently(func() (int, string, Foo, error) {
counter += 1
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(BeNumerically(">", 2))
})
It("polls the function and fails any of the values are non-zero", func() {
var err error
counter, s, f := 0, "", Foo{}
ig.G.Consistently(func() (int, string, Foo, error) {
counter += 1
if counter == 3 {
f = Foo{Bar: "welp"}
}
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Unexpected non-nil/non-zero return value at index 2:"))
Ω(ig.FailureMessage).Should(ContainSubstring(`Foo{Bar:"welp"}`))
Ω(counter).Should(Equal(3))
})
Context("when making a ShouldNot assertion", func() {
It("succeeds if all additional values are zero", func() {
var err error
counter, s, f := 0, "", Foo{}
ig.G.Consistently(func() (int, string, Foo, error) {
counter += 1
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(BeNumerically(">", 100))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(BeNumerically(">", 2))
})
It("fails if any additional values are ever non-zero", func() {
var err error
counter, s, f := 0, "", Foo{}
ig.G.Consistently(func() (int, string, Foo, error) {
counter += 1
if counter == 3 {
s = "welp"
}
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(BeNumerically(">", 100))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Unexpected non-nil/non-zero return value at index 1:"))
Ω(ig.FailureMessage).Should(ContainSubstring(`<string>: "welp"`))
Ω(counter).Should(Equal(3))
})
})
})
})
Context("when passed a function that takes a Gomega argument and returns values", func() {
Context("with Eventually", func() {
It("passes in a Gomega and passes if the matcher matches, all extra values are zero, and there are no failed assertions", func() {
counter, s, f, err := 0, "hi", Foo{Bar: "hi"}, errors.New("hi")
ig.G.Eventually(func(g Gomega) (int, string, Foo, error) {
switch counter += 1; counter {
case 2:
s = ""
case 3:
f = Foo{}
case 4:
err = nil
}
if counter == 5 {
g.Expect(true).To(BeTrue())
} else {
g.Expect(false).To(BeTrue())
panic("boom") //never see since the expectation stops execution
}
return counter, s, f, err
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(Equal(5))
})
It("times out if assertions in the function never succeed and reports on the error", func() {
_, file, line, _ := runtime.Caller(0)
ig.G.Eventually(func(g Gomega) int {
g.Expect(false).To(BeTrue())
return 10
}).WithTimeout(30 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Equal(10))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Assertion in callback at %s:%d failed:", file, line+2))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <bool>: false\nto be true"))
})
It("forwards panics", func() {
Ω(func() {
ig.G.Eventually(func(g Gomega) int {
g.Expect(true).To(BeTrue())
panic("boom")
}).WithTimeout(30 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Equal(10))
}).Should(PanicWith("boom"))
Ω(ig.FailureMessage).Should(BeEmpty())
})
It("correctly handles the case (in concert with Ginkgo) when an assertion fails in a goroutine", func() {
count := 0
ig.G.Eventually(func(g Gomega) {
c := make(chan interface{})
go func() {
defer GinkgoRecover()
defer close(c)
count += 1
g.Expect(count).To(Equal(3)) //panics!
}()
<-c
}).WithTimeout(30 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Succeed())
Ω(count).Should(Equal(3))
})
Context("when making a ShouldNot assertion", func() {
It("doesn't succeed until all extra values are zero, there are no failed assertions, and the matcher is (not) satisfied", func() {
counter, s, f, err := 0, "hi", Foo{Bar: "hi"}, errors.New("hi")
ig.G.Eventually(func(g Gomega) (int, string, Foo, error) {
switch counter += 1; counter {
case 2:
s = ""
case 3:
f = Foo{}
case 4:
err = nil
}
if counter == 5 {
g.Expect(true).To(BeTrue())
} else {
g.Expect(false).To(BeTrue())
panic("boom") //never see since the expectation stops execution
}
return counter, s, f, err
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).ShouldNot(BeNumerically("<", 0))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(Equal(5))
})
})
It("fails if an assertion is never satisfied", func() {
_, file, line, _ := runtime.Caller(0)
ig.G.Eventually(func(g Gomega) int {
g.Expect(false).To(BeTrue())
return 9
}).WithTimeout(30 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Equal(10))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Assertion in callback at %s:%d failed:", file, line+2))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <bool>: false\nto be true"))
})
})
Context("with Consistently", func() {
It("passes in a Gomega and passes if the matcher matches, all extra values are zero, and there are no failed assertions", func() {
var err error
counter, s, f := 0, "", Foo{}
ig.G.Consistently(func(g Gomega) (int, string, Foo, error) {
counter += 1
g.Expect(true).To(BeTrue())
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(BeZero())
Ω(counter).Should(BeNumerically(">", 2))
})
It("fails if the passed-in gomega ever hits a failure", func() {
var err error
counter, s, f := 0, "", Foo{}
_, file, line, _ := runtime.Caller(0)
ig.G.Consistently(func(g Gomega) (int, string, Foo, error) {
counter += 1
g.Expect(true).To(BeTrue())
if counter == 3 {
g.Expect(false).To(BeTrue())
panic("boom") //never see this
}
return counter, s, f, err
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(BeNumerically("<", 100))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Assertion in callback at %s:%d failed:", file, line+5))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <bool>: false\nto be true"))
Ω(counter).Should(Equal(3))
})
It("forwards panics", func() {
Ω(func() {
ig.G.Consistently(func(g Gomega) int {
g.Expect(true).To(BeTrue())
panic("boom")
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Equal(10))
}).Should(PanicWith("boom"))
Ω(ig.FailureMessage).Should(BeEmpty())
})
Context("when making a ShouldNot assertion", func() {
It("succeeds if any interior assertions always pass", func() {
ig.G.Consistently(func(g Gomega) int {
g.Expect(true).To(BeTrue())
return 9
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Equal(10))
Ω(ig.FailureMessage).Should(BeEmpty())
})
It("fails if any interior assertions ever fail", func() {
counter := 0
_, file, line, _ := runtime.Caller(0)
ig.G.Consistently(func(g Gomega) int {
g.Expect(true).To(BeTrue())
counter += 1
if counter == 3 {
g.Expect(false).To(BeTrue())
panic("boom") //never see this
}
return 9
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Equal(10))
Ω(ig.FailureMessage).Should(ContainSubstring("Error: Assertion in callback at %s:%d failed:", file, line+5))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <bool>: false\nto be true"))
})
})
})
})
Context("when passed a function that takes a Gomega argument and returns nothing", func() {
Context("with Eventually", func() {
It("returns the first failed assertion as an error and so should Succeed() if the callback ever runs without issue", func() {
counter := 0
ig.G.Eventually(func(g Gomega) {
counter += 1
if counter < 5 {
g.Expect(false).To(BeTrue())
g.Expect("bloop").To(Equal("blarp"))
}
}).WithTimeout(1 * time.Second).WithPolling(10 * time.Millisecond).Should(Succeed())
Ω(counter).Should(Equal(5))
Ω(ig.FailureMessage).Should(BeZero())
})
It("returns the first failed assertion as an error and so should timeout if the callback always fails", func() {
counter := 0
ig.G.Eventually(func(g Gomega) {
counter += 1
if counter < 5000 {
g.Expect(false).To(BeTrue())
g.Expect("bloop").To(Equal("blarp"))
}
}).WithTimeout(100 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Succeed())
Ω(counter).Should(BeNumerically(">", 1))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected success, but got an error"))
Ω(ig.FailureMessage).Should(ContainSubstring("<bool>: false"))
Ω(ig.FailureMessage).Should(ContainSubstring("to be true"))
Ω(ig.FailureMessage).ShouldNot(ContainSubstring("bloop"))
})
It("returns the first failed assertion as an error and should satisy ShouldNot(Succeed) eventually", func() {
counter := 0
ig.G.Eventually(func(g Gomega) {
counter += 1
if counter > 5 {
g.Expect(false).To(BeTrue())
g.Expect("bloop").To(Equal("blarp"))
}
}).WithTimeout(100 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Succeed())
Ω(counter).Should(Equal(6))
Ω(ig.FailureMessage).Should(BeZero())
})
It("should fail to ShouldNot(Succeed) eventually if an error never occurs", func() {
ig.G.Eventually(func(g Gomega) {
g.Expect(true).To(BeTrue())
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Succeed())
Ω(ig.FailureMessage).Should(ContainSubstring("Timed out after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected failure, but got no error."))
})
})
Context("with Consistently", func() {
It("returns the first failed assertion as an error and so should Succeed() if the callback always runs without issue", func() {
counter := 0
ig.G.Consistently(func(g Gomega) {
counter += 1
g.Expect(true).To(BeTrue())
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Succeed())
Ω(counter).Should(BeNumerically(">", 2))
Ω(ig.FailureMessage).Should(BeZero())
})
It("returns the first failed assertion as an error and so should fail if the callback ever fails", func() {
counter := 0
ig.G.Consistently(func(g Gomega) {
counter += 1
g.Expect(true).To(BeTrue())
if counter == 3 {
g.Expect(false).To(BeTrue())
g.Expect("bloop").To(Equal("blarp"))
}
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).Should(Succeed())
Ω(ig.FailureMessage).Should(ContainSubstring("Expected success, but got an error"))
Ω(ig.FailureMessage).Should(ContainSubstring("<bool>: false"))
Ω(ig.FailureMessage).Should(ContainSubstring("to be true"))
Ω(ig.FailureMessage).ShouldNot(ContainSubstring("bloop"))
Ω(counter).Should(Equal(3))
})
It("returns the first failed assertion as an error and should satisy ShouldNot(Succeed) consistently if an error always occur", func() {
counter := 0
ig.G.Consistently(func(g Gomega) {
counter += 1
g.Expect(true).To(BeFalse())
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Succeed())
Ω(counter).Should(BeNumerically(">", 2))
Ω(ig.FailureMessage).Should(BeZero())
})
It("should fail to satisfy ShouldNot(Succeed) consistently if an error ever does not occur", func() {
counter := 0
ig.G.Consistently(func(g Gomega) {
counter += 1
if counter == 3 {
g.Expect(true).To(BeTrue())
} else {
g.Expect(false).To(BeTrue())
}
}).WithTimeout(50 * time.Millisecond).WithPolling(10 * time.Millisecond).ShouldNot(Succeed())
Ω(ig.FailureMessage).Should(ContainSubstring("Failed after"))
Ω(ig.FailureMessage).Should(ContainSubstring("Expected failure, but got no error."))
Ω(counter).Should(Equal(3))
})
})
})
Context("when passed a function that takes a context", func() {
It("forwards its own configured context", func() {
ctx := context.WithValue(context.Background(), "key", "value")
Eventually(func(ctx context.Context) string {
return ctx.Value("key").(string)
}).WithContext(ctx).Should(Equal("value"))
})
It("forwards its own configured context _and_ a Gomega if requested", func() {
ctx := context.WithValue(context.Background(), "key", "value")
Eventually(func(g Gomega, ctx context.Context) {
g.Expect(ctx.Value("key").(string)).To(Equal("schmalue"))
}).WithContext(ctx).Should(MatchError(ContainSubstring("Expected\n <string>: value\nto equal\n <string>: schmalue")))
})
Context("when the assertion does not have an attached context", func() {
It("errors", func() {
ig.G.Eventually(func(ctx context.Context) string {
return ctx.Value("key").(string)
}).Should(Equal("value"))
Ω(ig.FailureMessage).Should(ContainSubstring("The function passed to Eventually requested a context.Context, but no context has been provided. Please pass one in using Eventually().WithContext()."))
Ω(ig.FailureSkip).Should(Equal([]int{2}))
})
})
})
Context("when passed a function that takes additional arguments", func() {
Context("with just arguments", func() {
It("forwards those arguments along", func() {
Eventually(func(a int, b string) string {
return fmt.Sprintf("%d - %s", a, b)
}).WithArguments(10, "four").Should(Equal("10 - four"))
Eventually(func(a int, b string, c ...int) string {
return fmt.Sprintf("%d - %s (%d%d%d)", a, b, c[0], c[1], c[2])
}).WithArguments(10, "four", 5, 1, 0).Should(Equal("10 - four (510)"))
})
})
Context("with a Gomega arugment as well", func() {
It("can also forward arguments alongside a Gomega", func() {
Eventually(func(g Gomega, a int, b int) {
g.Expect(a).To(Equal(b))
}).WithArguments(10, 3).ShouldNot(Succeed())
Eventually(func(g Gomega, a int, b int) {
g.Expect(a).To(Equal(b))
}).WithArguments(3, 3).Should(Succeed())
})
})
Context("with a context arugment as well", func() {
It("can also forward arguments alongside a context", func() {
ctx := context.WithValue(context.Background(), "key", "value")
Eventually(func(ctx context.Context, animal string) string {
return ctx.Value("key").(string) + " " + animal
}).WithArguments("pony").WithContext(ctx).Should(Equal("value pony"))
})
})
Context("with Gomega and context arugments", func() {
It("forwards arguments alongside both", func() {
ctx := context.WithValue(context.Background(), "key", "I have")
f := func(g Gomega, ctx context.Context, count int, zoo ...string) {
sentence := fmt.Sprintf("%s %d animals: %s", ctx.Value("key"), count, strings.Join(zoo, ", "))
g.Expect(sentence).To(Equal("I have 3 animals: dog, cat, pony"))
}
Eventually(f).WithArguments(3, "dog", "cat", "pony").WithContext(ctx).Should(Succeed())
Eventually(f).WithArguments(2, "dog", "cat").WithContext(ctx).Should(MatchError(ContainSubstring("Expected\n <string>: I have 2 animals: dog, cat\nto equal\n <string>: I have 3 animals: dog, cat, pony")))
})
})
Context("with a context that is in the argument list", func() {
It("does not forward the configured context", func() {