@@ -1956,6 +1956,9 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
19561956
19571957// CollectT implements the TestingT interface and collects all errors.
19581958type CollectT struct {
1959+ // A slice of errors. Non-nil slice denotes a failure.
1960+ // If it's non-nil but len(c.errors) == 0, this is also a failure
1961+ // obtained by direct c.FailNow() call.
19591962 errors []error
19601963}
19611964
@@ -1964,9 +1967,10 @@ func (c *CollectT) Errorf(format string, args ...interface{}) {
19641967 c .errors = append (c .errors , fmt .Errorf (format , args ... ))
19651968}
19661969
1967- // FailNow panics.
1968- func (* CollectT ) FailNow () {
1969- panic ("Assertion failed" )
1970+ // FailNow stops execution by calling runtime.Goexit.
1971+ func (c * CollectT ) FailNow () {
1972+ c .fail ()
1973+ runtime .Goexit ()
19701974}
19711975
19721976// Deprecated: That was a method for internal usage that should not have been published. Now just panics.
@@ -1979,6 +1983,16 @@ func (*CollectT) Copy(TestingT) {
19791983 panic ("Copy() is deprecated" )
19801984}
19811985
1986+ func (c * CollectT ) fail () {
1987+ if ! c .failed () {
1988+ c .errors = []error {} // Make it non-nil to mark a failure.
1989+ }
1990+ }
1991+
1992+ func (c * CollectT ) failed () bool {
1993+ return c .errors != nil
1994+ }
1995+
19821996// EventuallyWithT asserts that given condition will be met in waitFor time,
19831997// periodically checking target function each tick. In contrast to Eventually,
19841998// it supplies a CollectT to the condition function, so that the condition
@@ -2003,7 +2017,7 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
20032017 }
20042018
20052019 var lastFinishedTickErrs []error
2006- ch := make (chan [] error , 1 )
2020+ ch := make (chan * CollectT , 1 )
20072021
20082022 timer := time .NewTimer (waitFor )
20092023 defer timer .Stop ()
@@ -2023,16 +2037,16 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
20232037 go func () {
20242038 collect := new (CollectT )
20252039 defer func () {
2026- ch <- collect . errors
2040+ ch <- collect
20272041 }()
20282042 condition (collect )
20292043 }()
2030- case errs := <- ch :
2031- if len ( errs ) == 0 {
2044+ case collect := <- ch :
2045+ if ! collect . failed () {
20322046 return true
20332047 }
20342048 // Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached.
2035- lastFinishedTickErrs = errs
2049+ lastFinishedTickErrs = collect . errors
20362050 tick = ticker .C
20372051 }
20382052 }
0 commit comments