Skip to content

Commit

Permalink
Async assertions include context cancellation cause if present
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Jan 17, 2024
1 parent dee1e3c commit 121c37f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/async_assertion.go
Expand Up @@ -553,7 +553,12 @@ func (assertion *AsyncAssertion) match(matcher types.GomegaMatcher, desiredMatch
lock.Unlock()
}
case <-contextDone:
fail("Context was cancelled")
err := context.Cause(assertion.ctx)
if err != nil && err != context.Canceled {
fail(fmt.Sprintf("Context was cancelled (cause: %s)", err))
} else {
fail("Context was cancelled")
}
return false
case <-timeout:
if assertion.asyncType == AsyncAssertionTypeEventually {
Expand Down
18 changes: 17 additions & 1 deletion internal/async_assertion_test.go
@@ -1,6 +1,7 @@
package internal_test

import (
"context"
"errors"
"fmt"
"reflect"
Expand All @@ -10,7 +11,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
)

type quickMatcher struct {
Expand Down Expand Up @@ -275,6 +275,22 @@ var _ = Describe("Asynchronous Assertions", func() {
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled after"))
Ω(ig.FailureMessage).Should(ContainSubstring("There is no failure as the matcher passed to Consistently has not yet failed"))
})

It("includes the cancel cause if provided", func() {
ctx, cancel := context.WithCancelCause(context.Background())
counter := 0
ig.G.Eventually(func() string {
counter++
if counter == 2 {
cancel(fmt.Errorf("kaboom"))
} else if counter == 10 {
return MATCH
}
return NO_MATCH
}, time.Hour, ctx).Should(SpecMatch())
Ω(ig.FailureMessage).Should(ContainSubstring("Context was cancelled (cause: kaboom) after"))
Ω(ig.FailureMessage).Should(ContainSubstring("positive: no match"))
})
})

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

0 comments on commit 121c37f

Please sign in to comment.