onsi / gomega

Ginkgo's Preferred Matcher Library

Home Page:http://onsi.github.io/gomega/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

improve context support

pohly opened this issue · comments

When a context is canceled, context.Cause may be able to provide a better explanation that just "context canceled" (the error returned by Context.Err). This can be useful to figure out which timeout triggered, in particular when Ginkgo also supports it (onsi/ginkgo#1326).

fail("Context was cancelled")
could check for a cause and include that explanation. Bonus points for avoiding "Context was cancelled because context canceled" 😁

When a Gomega async assertion has its own timeout and the callback function accepts a context, create a context that contains that timeout and pass that to the callback. Right now, the context given to gomega.Eventually is passed through (

inValues = append(inValues, reflect.ValueOf(assertion.ctx))
), so if the callback blocks, it doesn't time out as requested via a per-assertion timeout.

It is a bit annoying that Go doesn't properly support "cause" in all variants of the context.With* calls. As it stands now (Go 1.21), one has to re-implement context.WithTimeout to get a cause both when the timeout occurs and for early cancelation.

test/utils/ktesting/contexthelper.go from kubernetes/kubernetes#122481 is some code that I wrote for this, feel free to copy whatever you find useful.

hey there - the latest code on master now supports emiting the context cancellation reason. i started working on the second bit (passing in a context myself) but quickly ran into complexity so i'm bailing out in the interest of getting the context cancellation reason done.