tinytest: implement per-test timeout (via alarm() under !win32 only)

This commit is contained in:
Azat Khuzhin 2019-03-24 16:44:56 +03:00
parent 1f4f8769c4
commit 15b2f41d74
No known key found for this signature in database
GPG Key ID: B86086848EF8686D

View File

@ -61,6 +61,12 @@
#define LONGEST_TEST_NAME 16384
#ifndef _WIN32
#define DEFAULT_TESTCASE_TIMEOUT 30U
#else
#define DEFAULT_TESTCASE_TIMEOUT 0U
#endif
static int in_tinytest_main = 0; /**< true if we're in tinytest_main().*/
static int n_ok = 0; /**< Number of tests that have passed */
static int n_bad = 0; /**< Number of tests that have failed. */
@ -69,6 +75,7 @@ static int n_skipped = 0; /**< Number of tests that have been skipped. */
static int opt_forked = 0; /**< True iff we're called from inside a win32 fork*/
static int opt_nofork = 0; /**< Suppress calls to fork() for debugging. */
static int opt_verbosity = 1; /**< -==quiet,0==terse,1==normal,2==verbose */
static unsigned int opt_timeout = DEFAULT_TESTCASE_TIMEOUT; /**< Timeout for every test (using alarm()) */
const char *verbosity_flag = "";
const struct testlist_alias_t *cfg_aliases=NULL;
@ -88,6 +95,25 @@ static void usage(struct testgroup_t *groups, int list_groups)
__attribute__((noreturn));
static int process_test_option(struct testgroup_t *groups, const char *test);
static unsigned int testcase_set_timeout_(void)
{
if (!opt_timeout)
return 0;
#ifndef _WIN32
return alarm(opt_timeout);
#else
/** TODO: win32 support */
fprintf(stderr, "You cannot set alarm on windows\n");
exit(1);
#endif
}
static unsigned int testcase_reset_timeout_(void)
{
#ifndef _WIN32
return alarm(0);
#endif
}
static enum outcome
testcase_run_bare_(const struct testcase_t *testcase)
{
@ -102,7 +128,11 @@ testcase_run_bare_(const struct testcase_t *testcase)
}
cur_test_outcome = OK;
testcase->fn(env);
{
testcase_set_timeout_();
testcase->fn(env);
testcase_reset_timeout_();
}
outcome = cur_test_outcome;
if (testcase->setup) {
@ -406,8 +436,14 @@ tinytest_main(int c, const char **v, struct testgroup_t *groups)
usage(groups, 0);
} else if (!strcmp(v[i], "--list-tests")) {
usage(groups, 1);
} else if (!strcmp(v[i], "--timeout")) {
if (i < c) {
fprintf(stderr, "--timeout requires argument\n");
return -1;
}
opt_timeout = (unsigned)atoi(v[i]);
} else {
printf("Unknown option %s. Try --help\n",v[i]);
fprintf(stderr, "Unknown option %s. Try --help\n", v[i]);
return -1;
}
} else {