-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Tests have an overall timeout for the entire binary but no timeout for a specific test case.
You often want to limit any particular test case to a time much shorter than the overall binary.
I propose to add the concept of per-test (function) timeouts to the go command user experience as follows.
-
Each test gets a per-test timeout. The timer for a given test only ticks down when the test is running. It does not tick down when the test is blocked in t.Parallel, nor when it is blocked in t.Run running a subtest.
-
The default per-test case timeout is 1m (one minute). If the new
-testtimeoutflag is specified explicitly, then that sets a different default. If the-testtimeoutflag is omitted but-timeoutis specified explicitly, then that sets the default too. This way, if you have one really long test and usego test -timeout=30m, the per-case timeout doesn't kick in after 1 minute and kill it anyway. -
There is a new testing.TB method
SetTimeout(d time.Duration)that allows a test to set its own timeout. Calling SetTimeout does not reset the timer. If a test runs for 30 seconds and then callst.SetTimeout(1*time.Second), it gets killed for having timed out. A timeout set this way is inherited by subtests. (They each have their own timer.) -
When a test timeout happens, the whole process still gets killed. There's nothing we can really do about that. But the failure does say which test function timed out.
This change will help users generally, and it will also help fuzzing, because any individual invocation of a fuzz function will now have a testing.T with a 1-minute timer, providing for detection of inputs that cause infinite loops or very slow execution.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status