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

replaced callback by mock object

This commit is contained in:
Falk Werner 2020-06-12 19:00:28 +02:00
parent cd76427f83
commit 1753841353
4 changed files with 101 additions and 32 deletions

View File

@ -218,6 +218,7 @@ alltests = executable('alltests',
'test/webfuse/mocks/mock_fuse.cc', 'test/webfuse/mocks/mock_fuse.cc',
'test/webfuse/mocks/mock_operation_context.cc', 'test/webfuse/mocks/mock_operation_context.cc',
'test/webfuse/mocks/mock_jsonrpc_proxy.cc', 'test/webfuse/mocks/mock_jsonrpc_proxy.cc',
'test/webfuse/mocks/mock_adapter_client_callback.cc',
'test/webfuse//tests/core/test_util.cc', 'test/webfuse//tests/core/test_util.cc',
'test/webfuse/tests/core/test_container_of.cc', 'test/webfuse/tests/core/test_container_of.cc',
'test/webfuse/tests/core/test_string.cc', 'test/webfuse/tests/core/test_string.cc',

View File

@ -0,0 +1,44 @@
#include "webfuse/mocks/mock_adapter_client_callback.hpp"
#include "webfuse/adapter/client.h"
extern "C"
{
static void
webfuse_test_MockAdapterClientCallback_callback(
wf_client * client,
int reason,
void * args)
{
void * user_data = wf_client_get_userdata(client);
auto * callback = reinterpret_cast<webfuse_test::MockAdapterClientCallback*>(user_data);
callback->Invoke(client, reason, args);
}
}
namespace webfuse_test
{
MockAdapterClientCallback::MockAdapterClientCallback()
{
}
MockAdapterClientCallback::~MockAdapterClientCallback()
{
}
void * MockAdapterClientCallback::GetUserData()
{
return reinterpret_cast<void*>(this);
}
wf_client_callback_fn * MockAdapterClientCallback::GetCallbackFn()
{
return &webfuse_test_MockAdapterClientCallback_callback;
}
}

View File

@ -0,0 +1,22 @@
#ifndef WF_MOCK_ADAPTER_CLIENT_CALLBACK_HPP
#define WF_MOCK_ADAPTER_CLIENT_CALLBACK_HPP
#include <gmock/gmock.h>
#include "webfuse/adapter/client_callback.h"
namespace webfuse_test
{
class MockAdapterClientCallback
{
public:
MockAdapterClientCallback();
virtual ~MockAdapterClientCallback();
MOCK_METHOD3(Invoke, void (wf_client *, int, void *));
void * GetUserData();
wf_client_callback_fn * GetCallbackFn();
};
}
#endif

View File

@ -1,11 +1,15 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "webfuse/adapter/client.h" #include "webfuse/adapter/client.h"
#include "webfuse/adapter/credentials.h" #include "webfuse/adapter/credentials.h"
#include "webfuse/core/protocol_names.h" #include "webfuse/core/protocol_names.h"
#include "webfuse/utils/ws_server.h" #include "webfuse/utils/ws_server.h"
#include "webfuse/mocks/mock_adapter_client_callback.hpp"
using webfuse_test::WsServer; using webfuse_test::WsServer;
using webfuse_test::MockAdapterClientCallback;
using testing::_;
namespace namespace
{ {
@ -67,33 +71,9 @@ void callback(
} }
} }
void callback2(
wf_client * client,
int reason,
void * args)
{
auto * ctx = reinterpret_cast<context*>(wf_client_get_userdata(client));
switch (reason)
{
case WF_CLIENT_CREATED:
ctx->state = connection_state::connecting;
break;
case WF_CLIENT_CONNECTED:
ctx->state = connection_state::connected;
break;
case WF_CLIENT_DISCONNECTED:
ctx->state = connection_state::disconnected;
break;
default:
break;
}
} }
TEST(AdapterClient, GeneralUsage)
}
TEST(client, general_usage)
{ {
context ctx; context ctx;
ctx.state = connection_state::connecting; ctx.state = connection_state::connecting;
@ -109,24 +89,46 @@ TEST(client, general_usage)
wf_client_dispose(client); wf_client_dispose(client);
} }
TEST(client, connect) TEST(AdapterClient, CreateAndDispose)
{ {
WsServer server(WF_PROTOCOL_NAME_PROVIDER_SERVER); MockAdapterClientCallback callback;
context ctx; EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
ctx.state = connection_state::connecting; EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
wf_client * client = wf_client_create( wf_client * client = wf_client_create(
&callback2, reinterpret_cast<void*>(&ctx)); callback.GetCallbackFn(), callback.GetUserData());
wf_client_dispose(client);
}
TEST(AdapterClient, Connect)
{
WsServer server(WF_PROTOCOL_NAME_PROVIDER_SERVER);
MockAdapterClientCallback callback;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1);
wf_client * client = wf_client_create(
callback.GetCallbackFn(), callback.GetUserData());
wf_client_connect(client, server.GetUrl().c_str()); wf_client_connect(client, server.GetUrl().c_str());
while (ctx.state != connection_state::connected) while (!server.IsConnected())
{ {
wf_client_service(client); wf_client_service(client);
} }
wf_client_disconnect(client); wf_client_disconnect(client);
while (ctx.state != connection_state::disconnected) while (server.IsConnected())
{ {
wf_client_service(client); wf_client_service(client);
} }