mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: extracted timer function of adapter into separate library
This commit is contained in:
9
lib/wf/timer/include/wf/timer.h
Normal file
9
lib/wf/timer/include/wf/timer.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef WF_TIMER_H
|
||||
#define WF_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
#include <wf/timer/timer.h>
|
||||
#include <wf/timer/manager.h>
|
||||
|
||||
#endif
|
||||
8
lib/wf/timer/include/wf/timer/api.h
Normal file
8
lib/wf/timer/include/wf/timer/api.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef WF_TIMER_API_H
|
||||
#define WF_TIMER_API_H
|
||||
|
||||
#ifndef WF_TIMER_API
|
||||
#define WF_TIMER_API
|
||||
#endif
|
||||
|
||||
#endif
|
||||
29
lib/wf/timer/include/wf/timer/manager.h
Normal file
29
lib/wf/timer/include/wf/timer/manager.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
19
lib/wf/timer/include/wf/timer/on_timer_fn.h
Normal file
19
lib/wf/timer/include/wf/timer/on_timer_fn.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef WF_TIMER_ON_TIMER_FN_H
|
||||
#define WF_TIMER_ON_TIMER_FN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
typedef void wf_timer_on_timer_fn(
|
||||
struct wf_timer * timer,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
38
lib/wf/timer/include/wf/timer/timer.h
Normal file
38
lib/wf/timer/include/wf/timer/timer.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
59
lib/wf/timer/src/wf/timer/api.c
Normal file
59
lib/wf/timer/src/wf/timer/api.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "wf/timer.h"
|
||||
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
|
||||
// manager
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
return wf_timer_impl_manager_create();
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_dispose(manager);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_check(manager);
|
||||
}
|
||||
|
||||
// timer
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
return wf_timer_impl_create(manager, on_timer, user_data);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_dispose(timer);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
wf_timer_impl_start(timer, timeout_ms);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_cancel(timer);
|
||||
}
|
||||
96
lib/wf/timer/src/wf/timer/impl/manager.c
Normal file
96
lib/wf/timer/src/wf/timer/impl/manager.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_timer_manager
|
||||
{
|
||||
struct wf_timer * timers;
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
if (NULL != manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_impl_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
free(manager);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (wf_timer_impl_is_timeout(timer))
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(manager, timer);
|
||||
wf_timer_impl_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
struct wf_timer * prev = timer->prev;
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
||||
36
lib/wf/timer/src/wf/timer/impl/manager.h
Normal file
36
lib/wf/timer/src/wf/timer/impl/manager.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef WF_TIMER_IMPL_MANAGER_H
|
||||
#define WF_TIMER_IMPL_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
31
lib/wf/timer/src/wf/timer/impl/timepoint.c
Normal file
31
lib/wf/timer/src/wf/timer/impl/timepoint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000)
|
||||
#define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000)
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_timer_timepoint const now = (tp.tv_sec * WF_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WF_TIMER_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
||||
31
lib/wf/timer/src/wf/timer/impl/timepoint.h
Normal file
31
lib/wf/timer/src/wf/timer/impl/timepoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef WF_TIMER_IMPL_TIMEPOINT_H
|
||||
#define WF_TIMER_IMPL_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_timer_timepoint;
|
||||
typedef int64_t wf_timer_timediff;
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_impl_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
67
lib/wf/timer/src/wf/timer/impl/timer.c
Normal file
67
lib/wf/timer/src/wf/timer/impl/timer.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_timer * timer = malloc(sizeof(struct wf_timer));
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->on_timer = on_timer;
|
||||
timer->user_data = user_data;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_impl_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_impl_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->on_timer(timer, timer->user_data);
|
||||
}
|
||||
}
|
||||
59
lib/wf/timer/src/wf/timer/impl/timer.h
Normal file
59
lib/wf/timer/src/wf/timer/impl/timer.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
|
||||
#include "wf/timer/on_timer_fn.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
struct wf_timer
|
||||
{
|
||||
struct wf_timer_manager * manager;
|
||||
wf_timer_timepoint timeout;
|
||||
wf_timer_on_timer_fn * on_timer;
|
||||
void * user_data;
|
||||
struct wf_timer * next;
|
||||
struct wf_timer * prev;
|
||||
};
|
||||
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
36
lib/wf/timer/test/wf/timer/test_timepoint.cc
Normal file
36
lib/wf/timer/test/wf/timer/test_timepoint.cc
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
using webfuse_test::msleep;
|
||||
|
||||
TEST(wf_timer_timepoint, now)
|
||||
{
|
||||
wf_timer_timepoint start = wf_timer_impl_timepoint_now();
|
||||
msleep(42);
|
||||
wf_timer_timepoint end = wf_timer_impl_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, in_msec)
|
||||
{
|
||||
wf_timer_timepoint now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint later = wf_timer_impl_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, elapsed)
|
||||
{
|
||||
wf_timer_timepoint now;
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_TRUE(wf_timer_impl_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_FALSE(wf_timer_impl_timepoint_is_elapsed(now + 500));
|
||||
}
|
||||
136
lib/wf/timer/test/wf/timer/test_timer.cc
Normal file
136
lib/wf/timer/test/wf/timer/test_timer.cc
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "wf/timer/timer.h"
|
||||
#include "wf/timer/manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using webfuse_test::msleep;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void on_timeout(struct wf_timer * timer, void * user_data)
|
||||
{
|
||||
(void) timer;
|
||||
|
||||
bool * triggered = reinterpret_cast<bool*>(user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_timer, init)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
msleep(500);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger_on_dispose)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, (5 * 60 * 1000));
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
msleep(500);
|
||||
wf_timer_cancel(timer);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
wf_timer_start(timer[i], 0);
|
||||
}
|
||||
|
||||
msleep(10);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_cancel(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_check(manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
||||
triggered[i] = false;
|
||||
wf_timer_start(timer[i], (300 - (50 * i)));
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
msleep(100);
|
||||
wf_timer_manager_check(manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
Reference in New Issue
Block a user