@@ -13,6 +13,7 @@ import (
1313 "runtime"
1414 "runtime/debug"
1515 "strings"
16+ "sync/atomic"
1617 "time"
1718 "unicode"
1819 "unicode/utf8"
@@ -2151,7 +2152,8 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21512152 conditionSucceeded
21522153 )
21532154
2154- var lastFinishedTickErrs []error
2155+ var lastFinishedTickErrs atomic.Value // of []error
2156+ lastFinishedTickErrs .Store ([]error {})
21552157 ch := make (chan int , 1 )
21562158
21572159 checkCond := func () {
@@ -2165,7 +2167,9 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21652167 }
21662168 // Keep the collected tick errors, so that they can be copied to 't'
21672169 // when timeout is reached or there is an unexpected exit.
2168- lastFinishedTickErrs = collect .errors
2170+ // Always store the actual value of collect.errors, even if nil
2171+ lastFinishedTickErrs .Store (collect .errors )
2172+
21692173 ch <- result
21702174 }()
21712175 condition (collect )
@@ -2176,6 +2180,16 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21762180 }
21772181 }
21782182
2183+ applyLastFinishedTickErrs := func () {
2184+ errs , ok := lastFinishedTickErrs .Load ().([]error )
2185+ if ! ok {
2186+ return
2187+ }
2188+ for _ , err := range errs {
2189+ t .Errorf ("%v" , err )
2190+ }
2191+ }
2192+
21792193 timer := time .NewTimer (waitFor )
21802194 defer timer .Stop ()
21812195
@@ -2190,19 +2204,15 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
21902204 for {
21912205 select {
21922206 case <- timer .C :
2193- for _ , err := range lastFinishedTickErrs {
2194- t .Errorf ("%v" , err )
2195- }
2207+ applyLastFinishedTickErrs ()
21962208 return Fail (t , "Condition never satisfied" , msgAndArgs ... )
21972209 case <- tickC :
21982210 tickC = nil
21992211 go checkCond ()
22002212 case result := <- ch :
22012213 switch result {
22022214 case conditionExitedUnexpectedly :
2203- for _ , err := range lastFinishedTickErrs {
2204- t .Errorf ("%v" , err )
2205- }
2215+ applyLastFinishedTickErrs ()
22062216 return Fail (t , "Condition exited unexpectedly" , msgAndArgs ... )
22072217 case conditionSucceeded :
22082218 return true
0 commit comments