diff --git a/CMakeLists.txt b/CMakeLists.txt index 25bd690..fccff9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ target_compile_options(fuse-wsfs-static PUBLIC ${EXTRA_CFLAGS}) add_executable(alltests + test/msleep.cc test/test_response_parser.cc test/test_server.cc test/test_timepoint.cc diff --git a/test/msleep.cc b/test/msleep.cc new file mode 100644 index 0000000..7719810 --- /dev/null +++ b/test/msleep.cc @@ -0,0 +1,19 @@ +#include "msleep.hpp" +#include + +namespace wsfs_test +{ + +void msleep(long millis) +{ + long const secs_per_msec = 1000; + long const msecs_per_nsec = (1000 * 1000); + + long const seconds = millis / secs_per_msec; + long const nanos = (millis & secs_per_msec) * msecs_per_nsec; + + struct timespec timeout = { seconds, nanos }; + while (0 != nanosleep(&timeout, &timeout)); +} + +} \ No newline at end of file diff --git a/test/msleep.hpp b/test/msleep.hpp new file mode 100644 index 0000000..ee0d79d --- /dev/null +++ b/test/msleep.hpp @@ -0,0 +1,12 @@ +#ifndef _WSFS_TEST_MSLEEP_HPP +#define _WSFS_TEST_MSLEEP_HPP + +namespace wsfs_test +{ + +extern void msleep(long millis); + +} + + +#endif diff --git a/test/test_response_parser.cc b/test/test_response_parser.cc index 7d873b7..67d86b3 100644 --- a/test/test_response_parser.cc +++ b/test/test_response_parser.cc @@ -1,9 +1,8 @@ #include #include -extern "C" { #include "wsfs/jsonrpc/response.h" -} + static void wsfs_response_parse_str( char const * buffer, diff --git a/test/test_timepoint.cc b/test/test_timepoint.cc index c52b12a..2eac48d 100644 --- a/test/test_timepoint.cc +++ b/test/test_timepoint.cc @@ -1,12 +1,14 @@ #include -#include + +#include "msleep.hpp" #include "wsfs/time/timepoint.h" +using wsfs_test::msleep; TEST(timepoint, now) { wsfs_timepoint start = wsfs_timepoint_now(); - usleep(42 * 1000); + msleep(42); wsfs_timepoint end = wsfs_timepoint_now(); ASSERT_LT(start, end); diff --git a/test/test_timer.cc b/test/test_timer.cc index 9bfbbec..a05604b 100644 --- a/test/test_timer.cc +++ b/test/test_timer.cc @@ -1,12 +1,13 @@ #include #include -#include +#include "msleep.hpp" #include "wsfs/time/timer.h" #include "wsfs/time/timeout_manager.h" using std::size_t; +using wsfs_test::msleep; namespace { @@ -39,7 +40,7 @@ TEST(timer, trigger) bool triggered = false; wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, reinterpret_cast(&triggered)); - usleep(500 * 1000); + msleep(500); wsfs_timeout_manager_check(&manager); ASSERT_TRUE(triggered); @@ -58,7 +59,7 @@ TEST(timer, cancel) bool triggered = false; wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, &triggered); - usleep(500 * 1000); + msleep(500); wsfs_timer_cancel(&timer); wsfs_timeout_manager_check(&manager); @@ -86,7 +87,7 @@ TEST(timer, multiple_timers) for(size_t i = 0; i < count; i++) { - usleep(100 * 1000); + msleep(100); wsfs_timeout_manager_check(&manager); }