Skip to content

Commit 121c37f

Browse files
committedJan 17, 2024
Async assertions include context cancellation cause if present
1 parent dee1e3c commit 121c37f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎internal/async_assertion.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,12 @@ func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch
553553
lock.Unlock()
554554
}
555555
case <-contextDone:
556-
fail("Context was cancelled")
556+
err := context.Cause(assertion.ctx)
557+
if err != nil && err != context.Canceled {
558+
fail(fmt.Sprintf("Context was cancelled (cause: %s)", err))
559+
} else {
560+
fail("Context was cancelled")
561+
}
557562
return false
558563
case <-timeout:
559564
if assertion.asyncType == AsyncAssertionTypeEventually {

‎internal/async_assertion_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal_test
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"reflect"
@@ -10,7 +11,6 @@ import (
1011

1112
. "github.com/onsi/ginkgo/v2"
1213
. "github.com/onsi/gomega"
13-
"golang.org/x/net/context"
1414
)
1515

1616
type quickMatcher struct {
@@ -275,6 +275,22 @@ var _ = Describe("Asynchronous Assertions", func() {
275275
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
276276
Ω(ig.FailureMessage).Should(ContainSubstring("There is no failure as the matcher passed to Consistently has not yet failed"))
277277
})
278+
279+
It("includes the cancel cause if provided", func() {
280+
ctx, cancel := context.WithCancelCause(context.Background())
281+
counter := 0
282+
ig.G.Eventually(func() string {
283+
counter++
284+
if counter == 2 {
285+
cancel(fmt.Errorf("kaboom"))
286+
} else if counter == 10 {
287+
return MATCH
288+
}
289+
return NO_MATCH
290+
}, time.Hour, ctx).Should(SpecMatch())
291+
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled (cause: kaboom) after"))
292+
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
293+
})
278294
})
279295

280296
Context("when the passed-in context is a Ginkgo SpecContext that can take a progress reporter attachment", func() {

0 commit comments

Comments
 (0)
Please sign in to comment.