1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

fixed issues regarding usleep

This commit is contained in:
Falk Werner 2019-02-13 20:49:05 +01:00
parent 7970cece50
commit bb917ab5c5
6 changed files with 42 additions and 8 deletions

View File

@ -125,6 +125,7 @@ target_compile_options(fuse-wsfs-static PUBLIC ${EXTRA_CFLAGS})
add_executable(alltests add_executable(alltests
test/msleep.cc
test/test_response_parser.cc test/test_response_parser.cc
test/test_server.cc test/test_server.cc
test/test_timepoint.cc test/test_timepoint.cc

19
test/msleep.cc Normal file
View File

@ -0,0 +1,19 @@
#include "msleep.hpp"
#include <ctime>
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));
}
}

12
test/msleep.hpp Normal file
View File

@ -0,0 +1,12 @@
#ifndef _WSFS_TEST_MSLEEP_HPP
#define _WSFS_TEST_MSLEEP_HPP
namespace wsfs_test
{
extern void msleep(long millis);
}
#endif

View File

@ -1,9 +1,8 @@
#include <cstring> #include <cstring>
#include <gtest/gtest.h> #include <gtest/gtest.h>
extern "C" {
#include "wsfs/jsonrpc/response.h" #include "wsfs/jsonrpc/response.h"
}
static void wsfs_response_parse_str( static void wsfs_response_parse_str(
char const * buffer, char const * buffer,

View File

@ -1,12 +1,14 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <unistd.h>
#include "msleep.hpp"
#include "wsfs/time/timepoint.h" #include "wsfs/time/timepoint.h"
using wsfs_test::msleep;
TEST(timepoint, now) TEST(timepoint, now)
{ {
wsfs_timepoint start = wsfs_timepoint_now(); wsfs_timepoint start = wsfs_timepoint_now();
usleep(42 * 1000); msleep(42);
wsfs_timepoint end = wsfs_timepoint_now(); wsfs_timepoint end = wsfs_timepoint_now();
ASSERT_LT(start, end); ASSERT_LT(start, end);

View File

@ -1,12 +1,13 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstddef> #include <cstddef>
#include <unistd.h>
#include "msleep.hpp"
#include "wsfs/time/timer.h" #include "wsfs/time/timer.h"
#include "wsfs/time/timeout_manager.h" #include "wsfs/time/timeout_manager.h"
using std::size_t; using std::size_t;
using wsfs_test::msleep;
namespace namespace
{ {
@ -39,7 +40,7 @@ TEST(timer, trigger)
bool triggered = false; bool triggered = false;
wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, reinterpret_cast<void*>(&triggered)); wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, reinterpret_cast<void*>(&triggered));
usleep(500 * 1000); msleep(500);
wsfs_timeout_manager_check(&manager); wsfs_timeout_manager_check(&manager);
ASSERT_TRUE(triggered); ASSERT_TRUE(triggered);
@ -58,7 +59,7 @@ TEST(timer, cancel)
bool triggered = false; bool triggered = false;
wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, &triggered); wsfs_timer_start(&timer, wsfs_timepoint_in_msec(250), &on_timeout, &triggered);
usleep(500 * 1000); msleep(500);
wsfs_timer_cancel(&timer); wsfs_timer_cancel(&timer);
wsfs_timeout_manager_check(&manager); wsfs_timeout_manager_check(&manager);
@ -86,7 +87,7 @@ TEST(timer, multiple_timers)
for(size_t i = 0; i < count; i++) for(size_t i = 0; i < count; i++)
{ {
usleep(100 * 1000); msleep(100);
wsfs_timeout_manager_check(&manager); wsfs_timeout_manager_check(&manager);
} }