[v2] tests: add timeout

Submitted by Marek Chalupa on Nov. 12, 2014, 12:16 p.m.

Details

Message ID 1415794602-28206-1-git-send-email-mchqwerty@gmail.com
State Accepted
Commit 7bf8049c484959f7ccc3262724466172dce786b6
Headers show

Not browsing as part of any series.

Commit Message

Marek Chalupa Nov. 12, 2014, 12:16 p.m.
Add test_set_timeout() function that allows the test to
set timeout for its completition. Any other call to the function
re-sets the timeout to the new value. The timeouts can be turned off
(usefull when debugging) by setting evironment variable
WAYLAND_TESTS_NO_TIMEOUTS.

v2:
  rename NO_TIMEOUTS to WAYLAND_TESTS_NO_TIMEOUTS
  use unsigned int as argument of test_set_timeout()
  improve printing of the message about timeout

Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
---
 tests/test-runner.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-runner.h |  7 +++++++
 2 files changed, 54 insertions(+)

Patch hide | download patch | download mbox

diff --git a/tests/test-runner.c b/tests/test-runner.c
index 8f3d5d3..3b2e6a9 100644
--- a/tests/test-runner.c
+++ b/tests/test-runner.c
@@ -41,6 +41,11 @@  static void* (*sys_calloc)(size_t, size_t);
 
 int leak_check_enabled;
 
+/* when this var is set to 0, every call to test_set_timeout() is
+ * suppressed - handy when debugging the test. Can be set by
+ * WAYLAND_TESTS_NO_TIMEOUTS evnironment var */
+static int timeouts_enabled = 1;
+
 extern const struct test __start_test_section, __stop_test_section;
 
 __attribute__ ((visibility("default"))) void *
@@ -107,14 +112,55 @@  usage(const char *name, int status)
 	exit(status);
 }
 
+void
+test_set_timeout(unsigned int to)
+{
+	int re;
+
+	if (!timeouts_enabled) {
+		fprintf(stderr, "Timeouts suppressed.\n");
+		return;
+	}
+
+	re = alarm(to);
+	fprintf(stderr, "Timeout was %sset", re ? "re-" : "");
+
+	if (to != 0)
+		fprintf(stderr, " to %d second%c from now.\n",
+			to, to > 1 ? 's' : 0);
+	else
+		fprintf(stderr, " off.\n");
+}
+
+static void
+sigalrm_handler(int signum)
+{
+	fprintf(stderr, "Test timed out.\n");
+	abort();
+}
+
 static void
 run_test(const struct test *t)
 {
 	int cur_alloc = num_alloc;
 	int cur_fds, num_fds;
+	struct sigaction sa;
 
 	cur_fds = count_open_fds();
+
+	if (timeouts_enabled) {
+		sa.sa_handler = sigalrm_handler;
+		sa.sa_flags = 0;
+		sigemptyset(&sa.sa_mask);
+		assert(sigaction(SIGALRM, &sa, NULL) == 0);
+	}
+
 	t->run();
+
+	/* turn off timeout (if any) after test completition */
+	if (timeouts_enabled)
+		alarm(0);
+
 	if (leak_check_enabled) {
 		if (cur_alloc != num_alloc) {
 			fprintf(stderr, "Memory leak detected in test. "
@@ -147,6 +193,7 @@  int main(int argc, char *argv[])
 	sys_free = dlsym(RTLD_NEXT, "free");
 
 	leak_check_enabled = !getenv("NO_ASSERT_LEAK_CHECK");
+	timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
 
 	if (argc == 2 && strcmp(argv[1], "--help") == 0)
 		usage(argv[0], EXIT_SUCCESS);
diff --git a/tests/test-runner.h b/tests/test-runner.h
index 707504c..3295e1c 100644
--- a/tests/test-runner.h
+++ b/tests/test-runner.h
@@ -37,4 +37,11 @@  count_open_fds(void);
 void
 exec_fd_leak_check(int nr_expected_fds); /* never returns */
 
+/*
+ * set/reset the timeout in seconds. The timeout starts
+ * at the point of invoking this function
+ */
+void
+test_set_timeout(unsigned int);
+
 #endif

Comments

On Wed, 12 Nov 2014 13:16:42 +0100
Marek Chalupa <mchqwerty@gmail.com> wrote:

> Add test_set_timeout() function that allows the test to
> set timeout for its completition. Any other call to the function
> re-sets the timeout to the new value. The timeouts can be turned off
> (usefull when debugging) by setting evironment variable
> WAYLAND_TESTS_NO_TIMEOUTS.
> 
> v2:
>   rename NO_TIMEOUTS to WAYLAND_TESTS_NO_TIMEOUTS
>   use unsigned int as argument of test_set_timeout()
>   improve printing of the message about timeout
> 
> Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
> ---
>  tests/test-runner.c | 47
> +++++++++++++++++++++++++++++++++++++++++++++++ tests/test-runner.h
> |  7 +++++++ 2 files changed, 54 insertions(+)
> 
> diff --git a/tests/test-runner.c b/tests/test-runner.c
> index 8f3d5d3..3b2e6a9 100644
> --- a/tests/test-runner.c
> +++ b/tests/test-runner.c
> @@ -41,6 +41,11 @@ static void* (*sys_calloc)(size_t, size_t);
>  
>  int leak_check_enabled;
>  
> +/* when this var is set to 0, every call to test_set_timeout() is
> + * suppressed - handy when debugging the test. Can be set by
> + * WAYLAND_TESTS_NO_TIMEOUTS evnironment var */
> +static int timeouts_enabled = 1;
> +
>  extern const struct test __start_test_section, __stop_test_section;
>  
>  __attribute__ ((visibility("default"))) void *
> @@ -107,14 +112,55 @@ usage(const char *name, int status)
>  	exit(status);
>  }
>  
> +void
> +test_set_timeout(unsigned int to)
> +{
> +	int re;
> +
> +	if (!timeouts_enabled) {
> +		fprintf(stderr, "Timeouts suppressed.\n");
> +		return;
> +	}
> +
> +	re = alarm(to);
> +	fprintf(stderr, "Timeout was %sset", re ? "re-" : "");
> +
> +	if (to != 0)
> +		fprintf(stderr, " to %d second%c from now.\n",
> +			to, to > 1 ? 's' : 0);
> +	else
> +		fprintf(stderr, " off.\n");
> +}
> +
> +static void
> +sigalrm_handler(int signum)
> +{
> +	fprintf(stderr, "Test timed out.\n");
> +	abort();
> +}
> +
>  static void
>  run_test(const struct test *t)
>  {
>  	int cur_alloc = num_alloc;
>  	int cur_fds, num_fds;
> +	struct sigaction sa;
>  
>  	cur_fds = count_open_fds();
> +
> +	if (timeouts_enabled) {
> +		sa.sa_handler = sigalrm_handler;
> +		sa.sa_flags = 0;
> +		sigemptyset(&sa.sa_mask);
> +		assert(sigaction(SIGALRM, &sa, NULL) == 0);
> +	}
> +
>  	t->run();
> +
> +	/* turn off timeout (if any) after test completition */
> +	if (timeouts_enabled)
> +		alarm(0);
> +
>  	if (leak_check_enabled) {
>  		if (cur_alloc != num_alloc) {
>  			fprintf(stderr, "Memory leak detected in
> test. " @@ -147,6 +193,7 @@ int main(int argc, char *argv[])
>  	sys_free = dlsym(RTLD_NEXT, "free");
>  
>  	leak_check_enabled = !getenv("NO_ASSERT_LEAK_CHECK");
> +	timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
>  
>  	if (argc == 2 && strcmp(argv[1], "--help") == 0)
>  		usage(argv[0], EXIT_SUCCESS);
> diff --git a/tests/test-runner.h b/tests/test-runner.h
> index 707504c..3295e1c 100644
> --- a/tests/test-runner.h
> +++ b/tests/test-runner.h
> @@ -37,4 +37,11 @@ count_open_fds(void);
>  void
>  exec_fd_leak_check(int nr_expected_fds); /* never returns */
>  
> +/*
> + * set/reset the timeout in seconds. The timeout starts
> + * at the point of invoking this function
> + */
> +void
> +test_set_timeout(unsigned int);
> +
>  #endif

Nice, pushed.

However, would you like to follow up with a patch to fix the NUL byte
printing in test_set_timeout()? ;-)


Thanks,
pq
Oh, yes, sure.

Thanks,
Marek

On 17 November 2014 16:02, Pekka Paalanen <ppaalanen@gmail.com> wrote:

> On Wed, 12 Nov 2014 13:16:42 +0100
> Marek Chalupa <mchqwerty@gmail.com> wrote:
>
> > Add test_set_timeout() function that allows the test to
> > set timeout for its completition. Any other call to the function
> > re-sets the timeout to the new value. The timeouts can be turned off
> > (usefull when debugging) by setting evironment variable
> > WAYLAND_TESTS_NO_TIMEOUTS.
> >
> > v2:
> >   rename NO_TIMEOUTS to WAYLAND_TESTS_NO_TIMEOUTS
> >   use unsigned int as argument of test_set_timeout()
> >   improve printing of the message about timeout
> >
> > Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
> > ---
> >  tests/test-runner.c | 47
> > +++++++++++++++++++++++++++++++++++++++++++++++ tests/test-runner.h
> > |  7 +++++++ 2 files changed, 54 insertions(+)
> >
> > diff --git a/tests/test-runner.c b/tests/test-runner.c
> > index 8f3d5d3..3b2e6a9 100644
> > --- a/tests/test-runner.c
> > +++ b/tests/test-runner.c
> > @@ -41,6 +41,11 @@ static void* (*sys_calloc)(size_t, size_t);
> >
> >  int leak_check_enabled;
> >
> > +/* when this var is set to 0, every call to test_set_timeout() is
> > + * suppressed - handy when debugging the test. Can be set by
> > + * WAYLAND_TESTS_NO_TIMEOUTS evnironment var */
> > +static int timeouts_enabled = 1;
> > +
> >  extern const struct test __start_test_section, __stop_test_section;
> >
> >  __attribute__ ((visibility("default"))) void *
> > @@ -107,14 +112,55 @@ usage(const char *name, int status)
> >       exit(status);
> >  }
> >
> > +void
> > +test_set_timeout(unsigned int to)
> > +{
> > +     int re;
> > +
> > +     if (!timeouts_enabled) {
> > +             fprintf(stderr, "Timeouts suppressed.\n");
> > +             return;
> > +     }
> > +
> > +     re = alarm(to);
> > +     fprintf(stderr, "Timeout was %sset", re ? "re-" : "");
> > +
> > +     if (to != 0)
> > +             fprintf(stderr, " to %d second%c from now.\n",
> > +                     to, to > 1 ? 's' : 0);
> > +     else
> > +             fprintf(stderr, " off.\n");
> > +}
> > +
> > +static void
> > +sigalrm_handler(int signum)
> > +{
> > +     fprintf(stderr, "Test timed out.\n");
> > +     abort();
> > +}
> > +
> >  static void
> >  run_test(const struct test *t)
> >  {
> >       int cur_alloc = num_alloc;
> >       int cur_fds, num_fds;
> > +     struct sigaction sa;
> >
> >       cur_fds = count_open_fds();
> > +
> > +     if (timeouts_enabled) {
> > +             sa.sa_handler = sigalrm_handler;
> > +             sa.sa_flags = 0;
> > +             sigemptyset(&sa.sa_mask);
> > +             assert(sigaction(SIGALRM, &sa, NULL) == 0);
> > +     }
> > +
> >       t->run();
> > +
> > +     /* turn off timeout (if any) after test completition */
> > +     if (timeouts_enabled)
> > +             alarm(0);
> > +
> >       if (leak_check_enabled) {
> >               if (cur_alloc != num_alloc) {
> >                       fprintf(stderr, "Memory leak detected in
> > test. " @@ -147,6 +193,7 @@ int main(int argc, char *argv[])
> >       sys_free = dlsym(RTLD_NEXT, "free");
> >
> >       leak_check_enabled = !getenv("NO_ASSERT_LEAK_CHECK");
> > +     timeouts_enabled = !getenv("WAYLAND_TESTS_NO_TIMEOUTS");
> >
> >       if (argc == 2 && strcmp(argv[1], "--help") == 0)
> >               usage(argv[0], EXIT_SUCCESS);
> > diff --git a/tests/test-runner.h b/tests/test-runner.h
> > index 707504c..3295e1c 100644
> > --- a/tests/test-runner.h
> > +++ b/tests/test-runner.h
> > @@ -37,4 +37,11 @@ count_open_fds(void);
> >  void
> >  exec_fd_leak_check(int nr_expected_fds); /* never returns */
> >
> > +/*
> > + * set/reset the timeout in seconds. The timeout starts
> > + * at the point of invoking this function
> > + */
> > +void
> > +test_set_timeout(unsigned int);
> > +
> >  #endif
>
> Nice, pushed.
>
> However, would you like to follow up with a patch to fix the NUL byte
> printing in test_set_timeout()? ;-)
>
>
> Thanks,
> pq
>