2020-02-29 20:06:40 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <cstddef>
|
2020-03-01 16:23:59 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/timer/timer.h"
|
|
|
|
#include "webfuse_provider/impl/timer/manager.h"
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
using std::size_t;
|
2020-03-01 16:23:59 +00:00
|
|
|
using namespace std::chrono_literals;
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
void on_timeout(struct wfp_timer * timer, void * user_data)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
(void) timer;
|
|
|
|
|
|
|
|
bool * triggered = reinterpret_cast<bool*>(user_data);
|
|
|
|
*triggered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, init)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
bool triggered = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer);
|
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, trigger)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
bool triggered = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_start(timer, -1);
|
|
|
|
wfp_timer_manager_check(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(triggered);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer);
|
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, trigger_on_dispose)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
bool triggered = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_start(timer, (5 * 60 * 1000));
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
ASSERT_TRUE(triggered);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, cancel)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
bool triggered = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_start(timer, 250);
|
2020-03-01 16:23:59 +00:00
|
|
|
std::this_thread::sleep_for(500ms);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_cancel(timer);
|
|
|
|
wfp_timer_manager_check(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(triggered);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer);
|
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, cancel_multiple_timers)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
static size_t const count = 5;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer[count];
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
bool triggered = false;
|
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
timer[i] = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
|
|
|
wfp_timer_start(timer[i], 0);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2020-02-29 20:06:40 +00:00
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_cancel(timer[i]);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_check(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
ASSERT_FALSE(triggered);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer[i]);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, multiple_timers)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
static size_t const count = 5;
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer[count];
|
2020-02-29 20:06:40 +00:00
|
|
|
bool triggered[count];
|
|
|
|
|
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
timer[i] = wfp_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
2020-02-29 20:06:40 +00:00
|
|
|
triggered[i] = false;
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_start(timer[i], (300 - (50 * i)));
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
2020-03-01 16:23:59 +00:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_check(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(triggered[i]);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer[i]);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-02-29 20:06:40 +00:00
|
|
|
}
|
2020-03-22 16:41:34 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_timer, dont_trigger_null_callback)
|
2020-03-22 16:41:34 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * manager = wfp_timer_manager_create();
|
|
|
|
struct wfp_timer * timer = wfp_timer_create(manager, nullptr, nullptr);
|
2020-03-22 16:41:34 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_start(timer, -1);
|
|
|
|
wfp_timer_manager_check(manager);
|
2020-03-22 16:41:34 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_dispose(timer);
|
|
|
|
wfp_timer_manager_dispose(manager);
|
2020-03-22 16:41:34 +00:00
|
|
|
}
|