Skip to content

proposal: testing: allow per-test timeouts #58549

@seebs

Description

@seebs

A lot of test cases could be most cleanly expressed by specifying a weak timeout estimate and doing something that may or may not successfully complete. In practice, tests that are going to hit a deadlock on failure manifest as the entire test run getting killed at the global test timeout, and dumping all of its goroutines, which can be a lot to sort through. It also means that at most one such test actually reports a failure.

It would be really convenient if you could write tests that expect a thing to happen in some time window, and if it doesn't, the test fails WITHOUT killing the entire go test process, dumping goroutines, and so on.

Proposed API addition:

func (*T) Timeout(time.Time)

This specifies a per-test timeout. If the timeout is reached before the test completes, the test is considered to have failed, as though by t.Fatalf("test duration (%v) exceeded", duration).

Right now, if I really, really, want to write a test that tries to check for a deadlock, I'm probably doing something messy with channels and goroutines. Because t.Fatalf and friends can't be called from other goroutines, I can't use that for both failures within the execution path of the test, and timeout-style failures; one or the other of them is in a different goroutine. There's no really good way to express this, but I think the testing internals could do it significantly more cleanly. And even if it's hard to get right, the testing package only has to get it right once.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions