]> granicus.if.org Git - libevent/commitdiff
tinytest: implement per-test timeout (via alarm() under !win32 only)
authorAzat Khuzhin <azat@libevent.org>
Sun, 24 Mar 2019 13:44:56 +0000 (16:44 +0300)
committerAzat Khuzhin <azat@libevent.org>
Sun, 24 Mar 2019 13:56:01 +0000 (16:56 +0300)
test/tinytest.c

index a27a906a0ade7fbc8b1de89d5d506ba12f90edc9..1588e89a27d522162fc83301fc9e24df4aec71b1 100644 (file)
 
 #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 {