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:
@@ -16,18 +16,16 @@ using std::size_t;
|
||||
#include <jsonrpc/send_fn.h>
|
||||
#include <jsonrpc/proxy_finished_fn.h>
|
||||
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct jsonrpc_proxy;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern JSONRPC_API struct jsonrpc_proxy *
|
||||
jsonrpc_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
struct jsonrpc_proxy *
|
||||
jsonrpc_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
|
||||
@@ -3,12 +3,14 @@
|
||||
#include "jsonrpc/impl/error.h"
|
||||
#include "jsonrpc/status.h"
|
||||
|
||||
#include <wf/timer/timer.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct jsonrpc_proxy *
|
||||
jsonrpc_impl_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
@@ -29,10 +31,10 @@ void jsonrpc_impl_proxy_dispose(
|
||||
free(proxy);
|
||||
}
|
||||
|
||||
static void jsonrpc_impl_proxy_timeout(
|
||||
struct wf_impl_timer * timer)
|
||||
static void jsonrpc_impl_proxy_on_timeout(
|
||||
struct wf_timer * timer, void * proxy_ptr)
|
||||
{
|
||||
struct jsonrpc_proxy * proxy = timer->user_data;
|
||||
struct jsonrpc_proxy * proxy = proxy_ptr;
|
||||
|
||||
if (proxy->request.is_pending)
|
||||
{
|
||||
@@ -43,7 +45,7 @@ static void jsonrpc_impl_proxy_timeout(
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
wf_timer_cancel(timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD_TIMEOUT, "Timeout");
|
||||
}
|
||||
@@ -95,7 +97,7 @@ static json_t * jsonrpc_impl_request_create(
|
||||
|
||||
void jsonrpc_impl_proxy_init(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * timeout_manager,
|
||||
struct wf_timer_manager * timeout_manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
@@ -104,8 +106,8 @@ void jsonrpc_impl_proxy_init(
|
||||
proxy->timeout = timeout;
|
||||
proxy->user_data = user_data;
|
||||
proxy->request.is_pending = false;
|
||||
|
||||
wf_impl_timer_init(&proxy->request.timer, timeout_manager);
|
||||
proxy->request.timer = wf_timer_create(timeout_manager,
|
||||
&jsonrpc_impl_proxy_on_timeout, proxy);
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_cleanup(
|
||||
@@ -120,12 +122,12 @@ void jsonrpc_impl_proxy_cleanup(
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
|
||||
wf_impl_timer_cleanup(&proxy->request.timer);
|
||||
wf_timer_dispose(proxy->request.timer);
|
||||
}
|
||||
|
||||
void jsonrpc_impl_proxy_invoke(
|
||||
@@ -143,8 +145,7 @@ void jsonrpc_impl_proxy_invoke(
|
||||
proxy->request.finished = finished;
|
||||
proxy->request.user_data = user_data;
|
||||
proxy->request.id = 42;
|
||||
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(proxy->timeout),
|
||||
&jsonrpc_impl_proxy_timeout, proxy);
|
||||
wf_timer_start(proxy->request.timer, proxy->timeout);
|
||||
|
||||
json_t * request = jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args);
|
||||
|
||||
@@ -155,7 +156,7 @@ void jsonrpc_impl_proxy_invoke(
|
||||
proxy->request.finished = NULL;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.id = 0;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
jsonrpc_impl_propate_error(finished, user_data, JSONRPC_BAD, "Bad");
|
||||
}
|
||||
@@ -203,7 +204,7 @@ void jsonrpc_impl_proxy_onresult(
|
||||
proxy->request.id = 0;
|
||||
proxy->request.user_data = NULL;
|
||||
proxy->request.finished = NULL;
|
||||
wf_impl_timer_cancel(&proxy->request.timer);
|
||||
wf_timer_cancel(proxy->request.timer);
|
||||
|
||||
finished(user_data, response.result, response.error);
|
||||
}
|
||||
|
||||
@@ -4,21 +4,21 @@
|
||||
#include "jsonrpc/proxy_finished_fn.h"
|
||||
#include "jsonrpc/send_fn.h"
|
||||
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
struct wf_timer;
|
||||
|
||||
struct jsonrpc_request
|
||||
{
|
||||
bool is_pending;
|
||||
jsonrpc_proxy_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_impl_timer timer;
|
||||
struct wf_timer * timer;
|
||||
};
|
||||
|
||||
struct jsonrpc_proxy
|
||||
@@ -32,7 +32,7 @@ struct jsonrpc_proxy
|
||||
extern void
|
||||
jsonrpc_impl_proxy_init(
|
||||
struct jsonrpc_proxy * proxy,
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
@@ -43,7 +43,7 @@ jsonrpc_impl_proxy_cleanup(
|
||||
|
||||
extern struct jsonrpc_proxy *
|
||||
jsonrpc_impl_proxy_create(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_timer_manager * manager,
|
||||
int timeout,
|
||||
jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "jsonrpc/proxy.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "wf/timer/manager.h"
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "webfuse/core/json_util.h"
|
||||
|
||||
@@ -87,27 +87,25 @@ namespace
|
||||
|
||||
TEST(jsonrpc_proxy, init)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext context;
|
||||
void * user_data = reinterpret_cast<void*>(&context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_FALSE(context.is_called);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -134,7 +132,7 @@ TEST(jsonrpc_proxy, invoke)
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
@@ -142,12 +140,11 @@ TEST(jsonrpc_proxy, invoke)
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context(false);
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -160,17 +157,16 @@ TEST(jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
||||
ASSERT_FALSE(nullptr == finished_context.error);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -189,17 +185,16 @@ TEST(jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
||||
ASSERT_EQ(WF_BAD_BUSY, wf_impl_jsonrpc_get_status(finished_context2.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -211,17 +206,16 @@ TEST(jsonrpc_proxy, invoke_fails_if_request_is_invalid)
|
||||
ASSERT_EQ(WF_BAD, wf_impl_jsonrpc_get_status(finished_context.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, on_result)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -246,17 +240,16 @@ TEST(jsonrpc_proxy, on_result)
|
||||
ASSERT_STREQ("okay", json_string_value(finished_context.result));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -278,17 +271,16 @@ TEST(jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, timeout)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 0, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -298,23 +290,22 @@ TEST(jsonrpc_proxy, timeout)
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
msleep(10);
|
||||
wf_impl_timeout_manager_check(&timeout_manager);
|
||||
wf_timer_manager_check(timer_manager);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_get_status(finished_context.error));
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, cleanup_pending_request)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, 10, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
||||
|
||||
FinishedContext finished_context;
|
||||
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
||||
@@ -324,26 +315,23 @@ TEST(jsonrpc_proxy, cleanup_pending_request)
|
||||
ASSERT_TRUE(json_is_object(send_context.response));
|
||||
|
||||
ASSERT_FALSE(finished_context.is_called);
|
||||
ASSERT_NE(nullptr, timeout_manager.timers);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
|
||||
ASSERT_TRUE(finished_context.is_called);
|
||||
ASSERT_EQ(nullptr, timeout_manager.timers);
|
||||
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(jsonrpc_proxy, notify)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
||||
|
||||
@@ -365,24 +353,22 @@ TEST(jsonrpc_proxy, notify)
|
||||
json_t * id = json_object_get(send_context.response, "id");
|
||||
ASSERT_EQ(nullptr, id);
|
||||
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
TEST(jsonrpc_proxy, notify_dont_send_invalid_request)
|
||||
{
|
||||
struct wf_impl_timeout_manager timeout_manager;
|
||||
wf_impl_timeout_manager_init(&timeout_manager);
|
||||
struct wf_timer_manager * timer_manager = wf_timer_manager_create();
|
||||
|
||||
SendContext send_context;
|
||||
void * send_data = reinterpret_cast<void*>(&send_context);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(&timeout_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
struct jsonrpc_proxy * proxy = jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
||||
|
||||
jsonrpc_proxy_notify(proxy, "foo", "?");
|
||||
|
||||
ASSERT_FALSE(send_context.is_called);
|
||||
|
||||
jsonrpc_proxy_dispose(proxy);
|
||||
wf_impl_timeout_manager_cleanup(&timeout_manager);
|
||||
wf_timer_manager_dispose(timer_manager);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user