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

102 lines
2.4 KiB
C++
Raw Normal View History

2019-02-09 18:02:53 +00:00
#include <gtest/gtest.h>
#include <cstddef>
2019-02-13 19:49:05 +00:00
#include "msleep.hpp"
2019-03-26 22:04:53 +00:00
#include "webfuse/adapter/impl/time/timer.h"
#include "webfuse/adapter/impl/time/timeout_manager.h"
2019-02-09 18:02:53 +00:00
using std::size_t;
2019-03-26 22:04:53 +00:00
using webfuse_test::msleep;
2019-02-09 18:02:53 +00:00
namespace
{
2019-03-26 22:04:53 +00:00
void on_timeout(struct wf_impl_timer * timer)
2019-02-09 18:02:53 +00:00
{
bool * triggered = reinterpret_cast<bool*>(timer->user_data);
*triggered = true;
}
}
TEST(timer, init)
{
2019-03-26 22:04:53 +00:00
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer;
2019-02-09 18:02:53 +00:00
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_init(&manager);
wf_impl_timer_init(&timer, &manager);
2019-02-09 18:02:53 +00:00
2019-03-26 22:04:53 +00:00
wf_impl_timer_cleanup(&timer);
wf_impl_timeout_manager_cleanup(&manager);
2019-02-09 18:02:53 +00:00
}
TEST(timer, trigger)
{
2019-03-26 22:04:53 +00:00
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer;
2019-02-09 18:02:53 +00:00
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_init(&manager);
wf_impl_timer_init(&timer, &manager);
2019-02-09 18:02:53 +00:00
bool triggered = false;
2019-03-26 22:04:53 +00:00
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, reinterpret_cast<void*>(&triggered));
2019-02-13 19:49:05 +00:00
msleep(500);
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_check(&manager);
2019-02-09 18:02:53 +00:00
ASSERT_TRUE(triggered);
2019-03-26 22:04:53 +00:00
wf_impl_timer_cleanup(&timer);
wf_impl_timeout_manager_cleanup(&manager);
2019-02-09 18:02:53 +00:00
}
TEST(timer, cancel)
{
2019-03-26 22:04:53 +00:00
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer;
2019-02-09 18:02:53 +00:00
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_init(&manager);
wf_impl_timer_init(&timer, &manager);
2019-02-09 18:02:53 +00:00
bool triggered = false;
2019-03-26 22:04:53 +00:00
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, &triggered);
2019-02-13 19:49:05 +00:00
msleep(500);
2019-03-26 22:04:53 +00:00
wf_impl_timer_cancel(&timer);
wf_impl_timeout_manager_check(&manager);
2019-02-09 18:02:53 +00:00
ASSERT_FALSE(triggered);
2019-03-26 22:04:53 +00:00
wf_impl_timer_cleanup(&timer);
wf_impl_timeout_manager_cleanup(&manager);
2019-02-09 18:02:53 +00:00
}
TEST(timer, multiple_timers)
{
static size_t const count = 5;
2019-03-26 22:04:53 +00:00
struct wf_impl_timeout_manager manager;
struct wf_impl_timer timer[count];
2019-02-09 18:02:53 +00:00
bool triggered[count];
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_init(&manager);
2019-02-09 18:02:53 +00:00
for(size_t i = 0; i < count; i++)
{
2019-03-26 22:04:53 +00:00
wf_impl_timer_init(&timer[i], &manager);
2019-02-09 18:02:53 +00:00
triggered[i] = false;
2019-03-26 22:04:53 +00:00
wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(300 - (50 * i)), &on_timeout, &triggered[i]);
2019-02-09 18:02:53 +00:00
}
for(size_t i = 0; i < count; i++)
{
2019-02-13 19:49:05 +00:00
msleep(100);
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_check(&manager);
2019-02-09 18:02:53 +00:00
}
for(size_t i = 0; i < count; i++)
{
ASSERT_TRUE(triggered[i]);
2019-03-26 22:04:53 +00:00
wf_impl_timer_cleanup(&timer[i]);
2019-02-09 18:02:53 +00:00
}
2019-03-26 22:04:53 +00:00
wf_impl_timeout_manager_cleanup(&manager);
2019-02-09 18:02:53 +00:00
}