mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-08 11:53:00 -04:00
Add an (internal) usleep function for use by unit tests
This commit is contained in:
parent
18653fcae7
commit
f25d9d32b1
@ -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 \
|
||||
])
|
||||
|
||||
|
30
evutil.c
30
evutil.c
@ -67,7 +67,10 @@
|
||||
#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
|
||||
}
|
||||
|
@ -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,
|
||||
};
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user