From: Nick Mathewson Date: Tue, 24 Jan 2012 16:42:26 +0000 (-0500) Subject: Add an (internal) usleep function for use by unit tests X-Git-Tag: release-2.1.1-alpha~131 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f25d9d32b101e97e4eb27e5e0a4d70acede61a8b;p=libevent Add an (internal) usleep function for use by unit tests --- diff --git a/configure.in b/configure.in index 8a9211d4..b60669ba 100644 --- a/configure.in +++ b/configure.in @@ -321,6 +321,7 @@ AC_CHECK_FUNCS([ \ inet_pton \ issetugid \ mmap \ + nanosleep \ pipe \ putenv \ sendfile \ @@ -334,6 +335,7 @@ AC_CHECK_FUNCS([ \ strtoll \ sysctl \ unsetenv \ + usleep \ vasprintf \ ]) diff --git a/evutil.c b/evutil.c index 4f0cfcd2..ae90d701 100644 --- a/evutil.c +++ b/evutil.c @@ -67,7 +67,10 @@ #ifdef _EVENT_HAVE_ARPA_INET_H #include #endif - +#if !defined(_EVENT_HAVE_NANOSLEEP) && !defined(EVENT_HAVE_USLEEP) && \ + !defined(_WIN32) +#include +#endif #ifndef _EVENT_HAVE_GETTIMEOFDAY #include #include @@ -2266,3 +2269,28 @@ evutil_load_windows_system_library(const TCHAR *library_name) } #endif +void +evutil_usleep(const struct timeval *tv) +{ + if (!tv) + return; +#if defined(_WIN32) + { + long msec = evutil_tv_to_msec(tv); + Sleep((DWORD)msec); + } +#elif defined(_EVENT_HAVE_NANOSLEEP_X) + { + struct timespec ts; + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec*1000; + nanosleep(&ts, NULL); + } +#elif defined(_EVENT_HAVE_USLEEP) + /* Some systems don't like to usleep more than 999999 usec */ + sleep(tv->tv_sec); + usleep(tv->tv_usec); +#else + select(0, NULL, NULL, NULL, tv); +#endif +} diff --git a/test/regress_util.c b/test/regress_util.c index 585b63d5..376ac8b5 100644 --- a/test/regress_util.c +++ b/test/regress_util.c @@ -1174,6 +1174,34 @@ test_event_strdup(void *arg) return; } +static void +test_evutil_usleep(void *arg) +{ + struct timeval tv1, tv2, tv3, diff1, diff2; + const struct timeval quarter_sec = {0, 250*1000}; + const struct timeval tenth_sec = {0, 100*1000}; + long usec1, usec2; + + evutil_gettimeofday(&tv1, NULL); + evutil_usleep(&quarter_sec); + evutil_gettimeofday(&tv2, NULL); + evutil_usleep(&tenth_sec); + evutil_gettimeofday(&tv3, NULL); + + evutil_timersub(&tv2, &tv1, &diff1); + evutil_timersub(&tv3, &tv2, &diff2); + usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec; + usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec; + + tt_int_op(usec1, >, 200000); + tt_int_op(usec1, <, 300000); + tt_int_op(usec2, >, 80000); + tt_int_op(usec2, <, 120000); + +end: + ; +} + struct testcase_t util_testcases[] = { { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, @@ -1195,6 +1223,7 @@ struct testcase_t util_testcases[] = { { "mm_malloc", test_event_malloc, 0, NULL, NULL }, { "mm_calloc", test_event_calloc, 0, NULL, NULL }, { "mm_strdup", test_event_strdup, 0, NULL, NULL }, + { "usleep", test_evutil_usleep, 0, NULL, NULL }, END_OF_TESTCASES, }; diff --git a/util-internal.h b/util-internal.h index 6278cecb..d1045e90 100644 --- a/util-internal.h +++ b/util-internal.h @@ -268,6 +268,8 @@ long evutil_tv_to_msec(const struct timeval *tv); int evutil_hex_char_to_int(char c); +void evutil_usleep(const struct timeval *tv); + #ifdef _WIN32 HANDLE evutil_load_windows_system_library(const TCHAR *library_name); #endif