Codebase list golang-gomega / bf3cba9
Allow StopTrying() to be wrapped Onsi Fakhouri 1 year, 6 months ago
2 changed file(s) with 44 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
66 "runtime"
77 "sync"
88 "time"
9 "errors"
910
1011 "github.com/onsi/gomega/types"
1112 )
1617 wasViaPanic() bool
1718 }
1819
20
21 func asStopTryingError(actual interface{}) (StopTryingError, bool) {
22 if actual == nil {
23 return nil, false
24 }
25 if actualErr, ok := actual.(error); ok {
26 var target *stopTryingError
27 if errors.As(actualErr, &target) {
28 return target, true
29 } else {
30 return nil, false
31 }
32 }
33
34 return nil, false
35 }
36
1937 type stopTryingError struct {
2038 message string
2139 viaPanic bool
3351 func (s *stopTryingError) wasViaPanic() bool {
3452 return s.viaPanic
3553 }
36
37 var stopTryingErrorType = reflect.TypeOf(&stopTryingError{})
3854
3955 var StopTrying = func(message string) StopTryingError {
4056 return &stopTryingError{message: message}
156172 return nil, fmt.Errorf("No values were returned by the function passed to Gomega"), stopTrying
157173 }
158174 actual := values[0].Interface()
159 if actual != nil && reflect.TypeOf(actual) == stopTryingErrorType {
160 stopTrying = actual.(StopTryingError)
175 if stopTryingErr, ok := asStopTryingError(actual); ok{
176 stopTrying = stopTryingErr
161177 }
162178 for i, extraValue := range values[1:] {
163179 extra := extraValue.Interface()
164180 if extra == nil {
165181 continue
166182 }
167 extraType := reflect.TypeOf(extra)
168 if extraType == stopTryingErrorType {
169 stopTrying = extra.(StopTryingError)
183 if stopTryingErr, ok := asStopTryingError(extra); ok{
184 stopTrying = stopTryingErr
170185 continue
171186 }
172 zero := reflect.Zero(extraType).Interface()
187 zero := reflect.Zero(reflect.TypeOf(extra)).Interface()
173188 if reflect.DeepEqual(extra, zero) {
174189 continue
175190 }
225240 return func() (actual interface{}, err error, stopTrying StopTryingError) {
226241 defer func() {
227242 if e := recover(); e != nil {
228 if reflect.TypeOf(e) == stopTryingErrorType {
229 stopTrying = e.(StopTryingError)
243 if stopTryingErr, ok := asStopTryingError(e); ok {
244 stopTrying = stopTryingErr
230245 } else {
231246 panic(e)
232247 }
290305 }
291306 }
292307 if e := recover(); e != nil {
293 if reflect.TypeOf(e) == stopTryingErrorType {
294 stopTrying = e.(StopTryingError)
308 if stopTryingErr, ok := asStopTryingError(e); ok {
309 stopTrying = stopTryingErr
295310 } else if assertionFailure == nil {
296311 panic(e)
297312 }
986986 Ω(ig.FailureMessage).Should(ContainSubstring("Reached the end - after"))
987987 Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <string>: C\nto equal\n <string>: D"))
988988 })
989
990 It("works even if the error is wrapped", func() {
991 possibilities := []string{"A", "B", "C"}
992 i := 0
993 ig.G.Eventually(func() (string, error) {
994 possibility := possibilities[i]
995 i += 1
996 if i == len(possibilities) {
997 return possibility, fmt.Errorf("Wrapped error: %w", StopTrying("Reached the end"))
998 } else {
999 return possibility, nil
1000 }
1001 }).Should(Equal("D"))
1002 Ω(i).Should(Equal(3))
1003 Ω(ig.FailureMessage).Should(ContainSubstring("Reached the end - after"))
1004 Ω(ig.FailureMessage).Should(ContainSubstring("Expected\n <string>: C\nto equal\n <string>: D"))
1005 })
9891006 })
9901007
9911008 Context("when StopTrying().Now() is called", func() {