aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-04-30 12:48:46 -0400
committerNick Mathewson <nickm@torproject.org>2014-06-14 11:40:27 -0400
commite2e588175eac4ebe8fb47c0540954d0f78525cce (patch)
tree4326a57028c03348a7ddb7518eae7e244de9461a
parent34f8723dc784142b30d92bbbdeb37089ae7a3bc5 (diff)
downloadtor-e2e588175eac4ebe8fb47c0540954d0f78525cce.tar
tor-e2e588175eac4ebe8fb47c0540954d0f78525cce.tar.gz
New testing-only tor_sleep_msec function
In the unit tests I want to loop with a delay, but I want less than a 1 second delay. This, sadly, requires compatibility code.
-rw-r--r--configure.ac2
-rw-r--r--src/common/compat.c26
-rw-r--r--src/common/compat.h4
3 files changed, 32 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index c0c818757..c662a9fc1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -355,6 +355,7 @@ AC_CHECK_FUNCS(
sysconf \
sysctl \
uname \
+ usleep \
vasprintf \
_vscprintf
)
@@ -885,6 +886,7 @@ AC_CHECK_HEADERS(
sys/param.h \
sys/prctl.h \
sys/resource.h \
+ sys/select.h \
sys/socket.h \
sys/sysctl.h \
sys/syslimits.h \
diff --git a/src/common/compat.c b/src/common/compat.c
index 1ba264a0c..1c460b6ae 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -114,6 +114,12 @@
/* Only use the linux prctl; the IRIX prctl is totally different */
#include <sys/prctl.h>
#endif
+#ifdef TOR_UNIT_TESTS
+#if !defined(HAVE_USLEEP) && defined(HAVE_SYS_SELECT_H)
+/* as fallback implementation for tor_sleep_msec */
+#include <sys/select.h>
+#endif
+#endif
#include "torlog.h"
#include "util.h"
@@ -3450,3 +3456,23 @@ get_total_system_memory(size_t *mem_out)
return -1;
}
+#ifdef TOR_UNIT_TESTS
+/** Delay for <b>msec</b> milliseconds. Only used in tests. */
+void
+tor_sleep_msec(int msec)
+{
+#ifdef _WIN32
+ Sleep(msec);
+#elif defined(HAVE_USLEEP)
+ sleep(msec / 1000);
+ /* Some usleep()s hate sleeping more than 1 sec */
+ usleep((msec % 1000) * 1000);
+#elif defined(HAVE_SYS_SELECT_H)
+ struct timeval tv = { msec / 1000, (msec % 1000) * 1000};
+ select(0, NULL, NULL, NULL, &tv);
+#else
+ sleep(CEIL_DIV(msec, 1000));
+#endif
+}
+#endif
+
diff --git a/src/common/compat.h b/src/common/compat.h
index 314b1aa00..d723448fd 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -744,6 +744,10 @@ char *format_win32_error(DWORD err);
#endif
+#ifdef TOR_UNIT_TESTS
+void tor_sleep_msec(int msec);
+#endif
+
#ifdef COMPAT_PRIVATE
#if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
#define NEED_ERSATZ_SOCKETPAIR