-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper for limiting a test's duration? #220
Comments
There's some subtleties I missed in this the first time around, but here's what I've ended up with I'm on the fence a bit about whether it makes sense to include a context. I could imagine cases where you wouldn't want one, and also cases where you might want a custom one with extra stuff injected. func testWithTimeout(t *testing.T, timeout time.Duration, f func(ctx context.Context, t *testing.T)) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
finished := make(chan bool)
go t.Run("withTimeout", func(t *testing.T) {
defer cancel()
f(ctx, t)
finished <- true
})
select {
case <-finished:
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatalf("test timed out after %s", timeout)
}
}
} |
Hey Glen! Thanks for opening this issue, I think it's an interesting idea. I've worked with a few tests that have this problem. Often I see them either use a context with a timeout directly to limit runtime, or use a select, similar to the one in your example, with a I think my main concern with adding something like this, for code that doesn't itself support cancellation, would be that it actually leaves the goroutine running after the timeout. When all the code is inline in the test that's a bit easier to see. I wonder if a helper like this should actually panic to stop the tests from running (similar to how the Is it safe to call I would probably want to avoid passing in a In short, I like the idea, but I'm not sure exactly what the right interface or behaviour should be for a generic helper. |
Hello! Yeah, I'm on the same page as you here - the need is there but I'm not quite sure about the exact approach.
Yep, this was what led me to run the function as a sub-test, without that FaiNow and co don't work.
Good point, the test in question had non-timeout channel reads in the test body, so had this exact issue. It worked out ok for me since the test would either succeed in time, or block forever - so wrapping the whole test in this helper was preferable to cluttering the test body. I suspect this helper would be less useful as protection against a test which takes a bit too long, but still completes - I'd have to test that flow a bit more |
It looks like this proposal golang/go#48157 was accepted. So maybe |
I've run across a few cases recently where I've wanted to set a deadline on a test running. Generally when doing potentially complicated things that could deadlock, and as mistake could accidentally leave the test hanging.
Would you be interesting in accepting a new function/package for this functionality?
There's some related disucssion in golang/go#10529 where it was deemed not suitable for core.
The text was updated successfully, but these errors were encountered: