Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ Command line arguments
a number of jobs ideal for your hardware configuration.
* ``--filter [PATTERN]``: Run tests whose string identifier matches
the given shell wildcard pattern (see dedicated section below). (\*nix only)
* ``--timeout [TIMEOUT]``: Set a timeout (in seconds) for all tests
* ``--timeout [TIMEOUT]``: Cap the timeout (in seconds) for all tests,
overriding any per-test value that exceeds it.
* ``--default-timeout [TIMEOUT]``: Set a fallback timeout (in seconds)
applied only to tests with no timeout set.
* ``--debug[=debugger]``: Run tests with a debugging server attached.
``debugger`` can be 'gdb', 'lldb', or 'windbg' (windows only).
* ``--debug-transport [TRANSPORT]``: Make the debugging server use the
Expand Down
9 changes: 9 additions & 0 deletions include/criterion/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ struct criterion_options {
*/
double timeout;

/**
* A default timeout applied to each test that has no timeout set, in seconds.
*
* If the value is non-positive, no default is applied.
*
* default: 0
*/
double default_timeout;

/**
* Fully report statistics from test workers, including those that are
* not reported by default (like passing assertions).
Expand Down
2 changes: 2 additions & 0 deletions src/core/runner_coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ static bxf_instance *run_test(struct run_next_context *ctx,
timeout = ctx->test->data->timeout;
if (criterion_options.timeout > 0 && timeout > criterion_options.timeout)
timeout = criterion_options.timeout;
if (timeout <= 0 && criterion_options.default_timeout > 0)
timeout = criterion_options.default_timeout;

sp.iquotas.runtime = timeout;

Expand Down
10 changes: 8 additions & 2 deletions src/entry/params.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
"name of the source file on a failure\n" \
" --filter [PATTERN]: run tests matching the " \
"given pattern\n" \
" --timeout [TIMEOUT]: set a timeout (in seconds) " \
"for all tests\n" \
" --timeout [TIMEOUT]: cap the timeout (in " \
"seconds) for all tests, overriding any per-test " \
"value that exceeds it\n" \
" --default-timeout [TIMEOUT]: set a fallback " \
"timeout (in seconds) applied only to tests with " \
"no timeout set\n" \
" --tap[=FILE]: writes TAP report in FILE " \
"(no file or \"-\" means stderr)\n" \
" --xml[=FILE]: writes XML report in FILE " \
Expand Down Expand Up @@ -264,6 +268,7 @@ CR_API int criterion_handle_args(int argc, char *argv[],
{ "ascii", no_argument, 0, 'k' },
{ "jobs", required_argument, 0, 'j' },
{ "timeout", required_argument, 0, 't' },
{ "default-timeout", required_argument, 0, 'g' },
{ "fail-fast", no_argument, 0, 'f' },
{ "short-filename", no_argument, 0, 'S' },
{ "single", required_argument, 0, 's' },
Expand Down Expand Up @@ -394,6 +399,7 @@ CR_API int criterion_handle_args(int argc, char *argv[],
case 'q': quiet = true; break;

case 't': criterion_options.timeout = atof(optarg); break;
case 'g': criterion_options.default_timeout = atof(optarg); break;

case 'd':
if (!parse_dbg(optarg))
Expand Down
6 changes: 4 additions & 2 deletions test/cram/help.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Display the help message
--ascii: don't use fancy unicode symbols or colors in the output
-S or --short-filename: only display the base name of the source file on a failure
--filter [PATTERN]: run tests matching the given pattern
--timeout [TIMEOUT]: set a timeout (in seconds) for all tests
--timeout [TIMEOUT]: cap the timeout (in seconds) for all tests, overriding any per-test value that exceeds it
--default-timeout [TIMEOUT]: set a fallback timeout (in seconds) applied only to tests with no timeout set
--tap[=FILE]: writes TAP report in FILE (no file or "-" means stderr)
--xml[=FILE]: writes XML report in FILE (no file or "-" means stderr)
--json[=FILE]: writes JSON report in FILE (no file or "-" means stderr)
Expand Down Expand Up @@ -49,7 +50,8 @@ C++ equivalents
--ascii: don't use fancy unicode symbols or colors in the output
-S or --short-filename: only display the base name of the source file on a failure
--filter [PATTERN]: run tests matching the given pattern
--timeout [TIMEOUT]: set a timeout (in seconds) for all tests
--timeout [TIMEOUT]: cap the timeout (in seconds) for all tests, overriding any per-test value that exceeds it
--default-timeout [TIMEOUT]: set a fallback timeout (in seconds) applied only to tests with no timeout set
--tap[=FILE]: writes TAP report in FILE (no file or "-" means stderr)
--xml[=FILE]: writes XML report in FILE (no file or "-" means stderr)
--json[=FILE]: writes JSON report in FILE (no file or "-" means stderr)
Expand Down
10 changes: 10 additions & 0 deletions test/cram/timeout.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--default-timeout applies to tests with no explicit timeout

$ default-timeout.c.bin --default-timeout 1 --filter='timeout/no_timeout_set'
\[FAIL\] timeout::no_timeout_set: Timed out. \([0-9.]*s\) (re)
[====] Synthesis: Tested: 1 | Passing: 0 | Failing: 1 | Crashing: 0

--default-timeout does not override an explicit per-test timeout

$ default-timeout.c.bin --default-timeout 1 --filter='timeout/explicit_not_overridden'
[====] Synthesis: Tested: 1 | Passing: 1 | Failing: 0 | Crashing: 0
18 changes: 18 additions & 0 deletions test/full/default-timeout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <criterion/criterion.h>

#ifdef _WIN32
# include <windows.h>
# define sleep(x) Sleep(x * 1000)
#else
# include <unistd.h>
#endif

/* No .timeout set, should be killed when --default-timeout is passed. */
Test(timeout, no_timeout_set) {
sleep(10);
}

/* Explicit .timeout = 5, --default-timeout 1 must not override it. */
Test(timeout, explicit_not_overridden, .timeout = 5.) {
sleep(2);
}
4 changes: 4 additions & 0 deletions test/full/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ if has_cxx
endif
endif

executable('default-timeout.c.bin', 'default-timeout.c',
include_directories: [criterion_api],
link_with: libcriterion.get_shared_lib())

foreach tst : full_tests
e = executable(tst + '.bin', tst,
include_directories: [criterion_api],
Expand Down
Loading