diff options
author | Marek Chalupa <mchqwerty@gmail.com> | 2014-11-12 13:14:46 +0100 |
---|---|---|
committer | Pekka Paalanen <pekka.paalanen@collabora.co.uk> | 2014-11-17 16:48:14 +0200 |
commit | 6ebe55060e6337f56e295032cc776fce694acbdb (patch) | |
tree | 686e80bc077b17487edc0e3c79ab2aab47c8cc34 | |
parent | 7bf8049c484959f7ccc3262724466172dce786b6 (diff) |
tests: add test_usleep and test_sleep functions
The former one was already used in tests, but was private.
These functions can be shared across the tests, so make them
public.
Signed-off-by: Marek Chalupa <mchqwerty@gmail.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
-rw-r--r-- | tests/display-test.c | 15 | ||||
-rw-r--r-- | tests/test-helpers.c | 32 | ||||
-rw-r--r-- | tests/test-runner.h | 12 |
3 files changed, 44 insertions, 15 deletions
diff --git a/tests/display-test.c b/tests/display-test.c index a1e45b1..f8aac64 100644 --- a/tests/display-test.c +++ b/tests/display-test.c @@ -333,21 +333,6 @@ register_reading(struct wl_display *display) assert(wl_display_flush(display) >= 0); } -#define USEC_TO_NSEC(n) (1000 * (n)) - -/* since we are using alarm() and SIGABRT, we can not - * use usleep function (see 'man usleep') */ -static void -test_usleep(useconds_t usec) -{ - struct timespec ts = { - .tv_sec = 0, - .tv_nsec = USEC_TO_NSEC(usec) - }; - - assert(nanosleep(&ts, NULL) == 0); -} - /* create thread that will call prepare+read so that * it will block */ static pthread_t diff --git a/tests/test-helpers.c b/tests/test-helpers.c index 4761b09..c05f4b1 100644 --- a/tests/test-helpers.c +++ b/tests/test-helpers.c @@ -25,6 +25,7 @@ #include <dirent.h> #include <stdio.h> #include <unistd.h> +#include <time.h> #include "test-runner.h" @@ -62,3 +63,34 @@ exec_fd_leak_check(int nr_expected_fds) execl(exe, exe, number, (char *)NULL); assert(0 && "execing fd leak checker failed"); } + +#define USEC_TO_NSEC(n) (1000 * (n)) + +/* our implementation of usleep and sleep functions that are safe to use with + * timeouts (timeouts are implemented using alarm(), so it is not safe use + * usleep and sleep. See man pages of these functions) + */ +void +test_usleep(useconds_t usec) +{ + struct timespec ts = { + .tv_sec = 0, + .tv_nsec = USEC_TO_NSEC(usec) + }; + + assert(nanosleep(&ts, NULL) == 0); +} + +/* we must write the whole function instead of + * wrapping test_usleep, because useconds_t may not + * be able to contain such a big number of microseconds */ +void +test_sleep(unsigned int sec) +{ + struct timespec ts = { + .tv_sec = sec, + .tv_nsec = 0 + }; + + assert(nanosleep(&ts, NULL) == 0); +} diff --git a/tests/test-runner.h b/tests/test-runner.h index 3295e1c..0e03530 100644 --- a/tests/test-runner.h +++ b/tests/test-runner.h @@ -5,6 +5,8 @@ #error "Tests must not be built with NDEBUG defined, they rely on assert()." #endif +#include <unistd.h> + struct test { const char *name; void (*run)(void); @@ -44,4 +46,14 @@ exec_fd_leak_check(int nr_expected_fds); /* never returns */ void test_set_timeout(unsigned int); +/* test-runner uses alarm() and SIGALRM, so we can not + * use usleep and sleep functions in tests (see 'man usleep' + * or 'man sleep', respectively). Following functions are safe + * to use in tests */ +void +test_usleep(useconds_t); + +void +test_sleep(unsigned int); + #endif |