@@ -2760,19 +2760,30 @@ func TestEventuallyTrue(t *testing.T) {
27602760 True (t , Eventually (t , condition , 100 * time .Millisecond , 20 * time .Millisecond ))
27612761}
27622762
2763+ // errorsCapturingT is a mock implementation of TestingT that captures errors reported with Errorf.
2764+ type errorsCapturingT struct {
2765+ errors []error
2766+ }
2767+
2768+ func (t * errorsCapturingT ) Errorf (format string , args ... interface {}) {
2769+ t .errors = append (t .errors , fmt .Errorf (format , args ... ))
2770+ }
2771+
2772+ func (t * errorsCapturingT ) Helper () {}
2773+
27632774func TestEventuallyWithTFalse (t * testing.T ) {
2764- mockT := new (CollectT )
2775+ mockT := new (errorsCapturingT )
27652776
27662777 condition := func (collect * CollectT ) {
2767- True (collect , false )
2778+ Fail (collect , "condition fixed failure" )
27682779 }
27692780
27702781 False (t , EventuallyWithT (mockT , condition , 100 * time .Millisecond , 20 * time .Millisecond ))
27712782 Len (t , mockT .errors , 2 )
27722783}
27732784
27742785func TestEventuallyWithTTrue (t * testing.T ) {
2775- mockT := new (CollectT )
2786+ mockT := new (errorsCapturingT )
27762787
27772788 state := 0
27782789 condition := func (collect * CollectT ) {
@@ -2786,6 +2797,41 @@ func TestEventuallyWithTTrue(t *testing.T) {
27862797 Len (t , mockT .errors , 0 )
27872798}
27882799
2800+ func TestEventuallyWithT_ConcurrencySafe (t * testing.T ) {
2801+ mockT := new (errorsCapturingT )
2802+
2803+ condition := func (collect * CollectT ) {
2804+ Fail (collect , "condition fixed failure" )
2805+ }
2806+
2807+ // To trigger race conditions, we run EventuallyWithT with a nanosecond tick.
2808+ False (t , EventuallyWithT (mockT , condition , 100 * time .Millisecond , time .Nanosecond ))
2809+ Len (t , mockT .errors , 2 )
2810+ }
2811+
2812+ func TestEventuallyWithT_ReturnsTheLatestFinishedConditionErrors (t * testing.T ) {
2813+ // We'll use a channel to control whether a condition should sleep or not.
2814+ mustSleep := make (chan bool , 2 )
2815+ mustSleep <- false
2816+ mustSleep <- true
2817+ close (mustSleep )
2818+
2819+ condition := func (collect * CollectT ) {
2820+ if <- mustSleep {
2821+ // Sleep to ensure that the second condition runs longer than timeout.
2822+ time .Sleep (time .Second )
2823+ return
2824+ }
2825+
2826+ // The first condition will fail. We expect to get this error as a result.
2827+ Fail (collect , "condition fixed failure" )
2828+ }
2829+
2830+ mockT := new (errorsCapturingT )
2831+ False (t , EventuallyWithT (mockT , condition , 100 * time .Millisecond , 20 * time .Millisecond ))
2832+ Len (t , mockT .errors , 2 )
2833+ }
2834+
27892835func TestNeverFalse (t * testing.T ) {
27902836 condition := func () bool {
27912837 return false
0 commit comments