Skip to content

Commit d63d67e

Browse files
committed
Rename AsyncSignalError to PollingSignalError
...and use it everywhere
1 parent abd25f0 commit d63d67e

6 files changed

+132
-130
lines changed

gomega_dsl.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ When `TryAgainAfter(<duration>` is triggered `Eventually` and `Consistently` wil
443443
*/
444444
var TryAgainAfter = internal.TryAgainAfter
445445

446+
/*
447+
PollingSignalError is the error returned by StopTrying() and TryAgainAfter()
448+
*/
449+
type PollingSignalError = internal.PollingSignalError
450+
446451
// SetDefaultEventuallyTimeout sets the default timeout duration for Eventually. Eventually will repeatedly poll your condition until it succeeds, or until this timeout elapses.
447452
func SetDefaultEventuallyTimeout(t time.Duration) {
448453
Default.SetDefaultEventuallyTimeout(t)

internal/async_assertion.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (assertion *AsyncAssertion) processReturnValues(values []reflect.Value) (in
134134
}
135135

136136
actual := values[0].Interface()
137-
if _, ok := AsAsyncSignalError(actual); ok {
137+
if _, ok := AsPollingSignalError(actual); ok {
138138
return actual, actual.(error)
139139
}
140140

@@ -144,7 +144,7 @@ func (assertion *AsyncAssertion) processReturnValues(values []reflect.Value) (in
144144
if extra == nil {
145145
continue
146146
}
147-
if _, ok := AsAsyncSignalError(extra); ok {
147+
if _, ok := AsPollingSignalError(extra); ok {
148148
return actual, extra.(error)
149149
}
150150
extraType := reflect.TypeOf(extra)
@@ -253,13 +253,13 @@ func (assertion *AsyncAssertion) buildActualPoller() (func() (interface{}, error
253253
actual = assertionFailure
254254
} else {
255255
actual, err = assertion.processReturnValues(values)
256-
_, isAsyncError := AsAsyncSignalError(err)
256+
_, isAsyncError := AsPollingSignalError(err)
257257
if assertionFailure != nil && !isAsyncError {
258258
err = assertionFailure
259259
}
260260
}
261261
if e := recover(); e != nil {
262-
if _, isAsyncError := AsAsyncSignalError(e); isAsyncError {
262+
if _, isAsyncError := AsPollingSignalError(e); isAsyncError {
263263
err = e.(error)
264264
} else if assertionFailure == nil {
265265
panic(e)
@@ -308,7 +308,7 @@ func (assertion *AsyncAssertion) matcherSaysStopTrying(matcher types.GomegaMatch
308308
func (assertion *AsyncAssertion) pollMatcher(matcher types.GomegaMatcher, value interface{}) (matches bool, err error) {
309309
defer func() {
310310
if e := recover(); e != nil {
311-
if _, isAsyncError := AsAsyncSignalError(e); isAsyncError {
311+
if _, isAsyncError := AsPollingSignalError(e); isAsyncError {
312312
err = e.(error)
313313
} else {
314314
panic(e)
@@ -350,10 +350,9 @@ func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch
350350
defer lock.Unlock()
351351
message := ""
352352
if err != nil {
353-
//TODO - formatting for TryAgainAfter?
354-
if asyncSignal, ok := AsAsyncSignalError(err); ok && asyncSignal.IsStopTrying() {
353+
if pollingSignalErr, ok := AsPollingSignalError(err); ok && pollingSignalErr.IsStopTrying() {
355354
message = err.Error()
356-
for _, attachment := range asyncSignal.Attachments {
355+
for _, attachment := range pollingSignalErr.Attachments {
357356
message += fmt.Sprintf("\n%s:\n", attachment.Description)
358357
message += format.Object(attachment.Object, 1)
359358
}
@@ -389,13 +388,13 @@ func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch
389388
var nextPoll <-chan time.Time = nil
390389
var isTryAgainAfterError = false
391390

392-
if asyncSignal, ok := AsAsyncSignalError(err); ok {
393-
if asyncSignal.IsStopTrying() {
391+
if pollingSignalErr, ok := AsPollingSignalError(err); ok {
392+
if pollingSignalErr.IsStopTrying() {
394393
fail("Told to stop trying")
395394
return false
396395
}
397-
if asyncSignal.IsTryAgainAfter() {
398-
nextPoll = time.After(asyncSignal.TryAgainDuration())
396+
if pollingSignalErr.IsTryAgainAfter() {
397+
nextPoll = time.After(pollingSignalErr.TryAgainDuration())
399398
isTryAgainAfterError = true
400399
}
401400
}

internal/async_assertion_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,6 @@ sprocket:
14481448
ig.G.Eventually(42).Should(HaveLen(1), "foo", ContainElement(42))
14491449
}).NotTo(Panic())
14501450
})
1451-
14521451
})
14531452

14541453
Context("eventual nil-ism", func() { // issue #555

internal/async_signal_error.go

Lines changed: 0 additions & 107 deletions
This file was deleted.

internal/polling_signal_error.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package internal
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
)
8+
9+
type PollingSignalErrorType int
10+
11+
const (
12+
PollingSignalErrorTypeStopTrying PollingSignalErrorType = iota
13+
PollingSignalErrorTypeTryAgainAfter
14+
)
15+
16+
type PollingSignalError interface {
17+
error
18+
Wrap(err error) PollingSignalError
19+
Attach(description string, obj any) PollingSignalError
20+
Now()
21+
}
22+
23+
var StopTrying = func(message string) PollingSignalError {
24+
return &PollingSignalErrorImpl{
25+
message: message,
26+
pollingSignalErrorType: PollingSignalErrorTypeStopTrying,
27+
}
28+
}
29+
30+
var TryAgainAfter = func(duration time.Duration) PollingSignalError {
31+
return &PollingSignalErrorImpl{
32+
message: fmt.Sprintf("told to try again after %s", duration),
33+
duration: duration,
34+
pollingSignalErrorType: PollingSignalErrorTypeTryAgainAfter,
35+
}
36+
}
37+
38+
type PollingSignalErrorAttachment struct {
39+
Description string
40+
Object any
41+
}
42+
43+
type PollingSignalErrorImpl struct {
44+
message string
45+
wrappedErr error
46+
pollingSignalErrorType PollingSignalErrorType
47+
duration time.Duration
48+
Attachments []PollingSignalErrorAttachment
49+
}
50+
51+
func (s *PollingSignalErrorImpl) Wrap(err error) PollingSignalError {
52+
s.wrappedErr = err
53+
return s
54+
}
55+
56+
func (s *PollingSignalErrorImpl) Attach(description string, obj any) PollingSignalError {
57+
s.Attachments = append(s.Attachments, PollingSignalErrorAttachment{description, obj})
58+
return s
59+
}
60+
61+
func (s *PollingSignalErrorImpl) Error() string {
62+
if s.wrappedErr == nil {
63+
return s.message
64+
} else {
65+
return s.message + ": " + s.wrappedErr.Error()
66+
}
67+
}
68+
69+
func (s *PollingSignalErrorImpl) Unwrap() error {
70+
if s == nil {
71+
return nil
72+
}
73+
return s.wrappedErr
74+
}
75+
76+
func (s *PollingSignalErrorImpl) Now() {
77+
panic(s)
78+
}
79+
80+
func (s *PollingSignalErrorImpl) IsStopTrying() bool {
81+
return s.pollingSignalErrorType == PollingSignalErrorTypeStopTrying
82+
}
83+
84+
func (s *PollingSignalErrorImpl) IsTryAgainAfter() bool {
85+
return s.pollingSignalErrorType == PollingSignalErrorTypeTryAgainAfter
86+
}
87+
88+
func (s *PollingSignalErrorImpl) TryAgainDuration() time.Duration {
89+
return s.duration
90+
}
91+
92+
func AsPollingSignalError(actual interface{}) (*PollingSignalErrorImpl, bool) {
93+
if actual == nil {
94+
return nil, false
95+
}
96+
if actualErr, ok := actual.(error); ok {
97+
var target *PollingSignalErrorImpl
98+
if errors.As(actualErr, &target) {
99+
return target, true
100+
} else {
101+
return nil, false
102+
}
103+
}
104+
105+
return nil, false
106+
}

internal/async_signal_error_test.go renamed to internal/polling_signal_error_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/onsi/gomega/internal"
1010
)
1111

12-
var _ = Describe("AsyncSignalError", func() {
12+
var _ = Describe("PollingSignalError", func() {
1313
Describe("StopTrying", func() {
1414
Describe("building StopTrying errors", func() {
1515
It("returns a correctly configured StopTrying error", func() {
1616
st := StopTrying("I've tried 17 times - give up!")
1717
Ω(st.Error()).Should(Equal("I've tried 17 times - give up!"))
1818
Ω(errors.Unwrap(st)).Should(BeNil())
19-
Ω(st.(*internal.AsyncSignalErrorImpl).IsStopTrying()).Should(BeTrue())
19+
Ω(st.(*internal.PollingSignalErrorImpl).IsStopTrying()).Should(BeTrue())
2020
})
2121
})
2222

@@ -32,35 +32,35 @@ var _ = Describe("AsyncSignalError", func() {
3232

3333
Describe("When attaching objects", func() {
3434
It("attaches them, with their descriptions", func() {
35-
st := StopTrying("Welp!").Attach("Max retries attained", 17).Attach("Got this response", "FLOOP").(*internal.AsyncSignalErrorImpl)
35+
st := StopTrying("Welp!").Attach("Max retries attained", 17).Attach("Got this response", "FLOOP").(*internal.PollingSignalErrorImpl)
3636
Ω(st.Attachments).Should(HaveLen(2))
37-
Ω(st.Attachments[0]).Should(Equal(internal.AsyncSignalErrorAttachment{"Max retries attained", 17}))
38-
Ω(st.Attachments[1]).Should(Equal(internal.AsyncSignalErrorAttachment{"Got this response", "FLOOP"}))
37+
Ω(st.Attachments[0]).Should(Equal(internal.PollingSignalErrorAttachment{"Max retries attained", 17}))
38+
Ω(st.Attachments[1]).Should(Equal(internal.PollingSignalErrorAttachment{"Got this response", "FLOOP"}))
3939
})
4040
})
4141

4242
Describe("when invoking Now()", func() {
4343
It("should panic with itself", func() {
44-
st := StopTrying("bam").(*internal.AsyncSignalErrorImpl)
44+
st := StopTrying("bam").(*internal.PollingSignalErrorImpl)
4545
Ω(st.Now).Should(PanicWith(st))
4646
})
4747
})
4848

49-
Describe("AsAsyncSignalError", func() {
49+
Describe("AsPollingSignalError", func() {
5050
It("should return false for nils", func() {
51-
st, ok := internal.AsAsyncSignalError(nil)
51+
st, ok := internal.AsPollingSignalError(nil)
5252
Ω(st).Should(BeNil())
5353
Ω(ok).Should(BeFalse())
5454
})
5555

5656
It("should work when passed a StopTrying error", func() {
57-
st, ok := internal.AsAsyncSignalError(StopTrying("bam"))
57+
st, ok := internal.AsPollingSignalError(StopTrying("bam"))
5858
Ω(st).Should(Equal(StopTrying("bam")))
5959
Ω(ok).Should(BeTrue())
6060
})
6161

6262
It("should work when passed a wrapped error", func() {
63-
st, ok := internal.AsAsyncSignalError(fmt.Errorf("STOP TRYING %w", StopTrying("bam")))
63+
st, ok := internal.AsPollingSignalError(fmt.Errorf("STOP TRYING %w", StopTrying("bam")))
6464
Ω(st).Should(Equal(StopTrying("bam")))
6565
Ω(ok).Should(BeTrue())
6666
})

0 commit comments

Comments
 (0)