From 121c37f100b97f1d2c3f68c436f92bd7df4a120a Mon Sep 17 00:00:00 2001 From: Onsi Fakhouri Date: Wed, 17 Jan 2024 13:57:16 -0700 Subject: [PATCH] Async assertions include context cancellation cause if present --- internal/async_assertion.go | 7 ++++++- internal/async_assertion_test.go | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/async_assertion.go b/internal/async_assertion.go index 1188b0bce..cde9e2ec8 100644 --- a/internal/async_assertion.go +++ b/internal/async_assertion.go @@ -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 { diff --git a/internal/async_assertion_test.go b/internal/async_assertion_test.go index accdfe55c..c966dead2 100644 --- a/internal/async_assertion_test.go +++ b/internal/async_assertion_test.go @@ -1,6 +1,7 @@ package internal_test import ( + "context" "errors" "fmt" "reflect" @@ -10,7 +11,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "golang.org/x/net/context" ) type quickMatcher struct { @@ -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() {