]> granicus.if.org Git - libevent/commitdiff
Add an (internal) usleep function for use by unit tests
authorNick Mathewson <nickm@torproject.org>
Tue, 24 Jan 2012 16:42:26 +0000 (11:42 -0500)
committerNick Mathewson <nickm@torproject.org>
Tue, 24 Jan 2012 16:42:26 +0000 (11:42 -0500)
configure.in
evutil.c
test/regress_util.c
util-internal.h

index 8a9211d4ccd48a21f18008c1131a8dc86ff98401..b60669baefd17a9e6d29e4662e0bc200ceebe7eb 100644 (file)
@@ -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 \
 ])
 
index 4f0cfcd2b0ba971d6720e86fd0b1004022448a83..ae90d7017f1576ce9ed0f70ea6f6b3f0268027cc 100644 (file)
--- a/evutil.c
+++ b/evutil.c
 #ifdef _EVENT_HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #endif
-
+#if !defined(_EVENT_HAVE_NANOSLEEP) && !defined(EVENT_HAVE_USLEEP) && \
+       !defined(_WIN32)
+#include <sys/select.h>
+#endif
 #ifndef _EVENT_HAVE_GETTIMEOFDAY
 #include <sys/timeb.h>
 #include <time.h>
@@ -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
+}
index 585b63d5d0e56e03b730e0188b18a0b48f290f69..376ac8b57a6dca62ee74be7756bc81f9d77230c7 100644 (file)
@@ -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,
 };
 
index 6278cecbf9a1d6428611046dddcf7ff49109794e..d1045e90b163f8ac16bf59da4d95880ba13a773d 100644 (file)
@@ -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