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

fixed tests

This commit is contained in:
Falk Werner 2020-06-25 19:21:00 +02:00
parent dc6e05b1b8
commit aa06846c15
5 changed files with 95 additions and 40 deletions

View File

@ -4,24 +4,40 @@
#include "webfuse_provider/test_util/client.hpp" #include "webfuse_provider/test_util/client.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <future>
#include <chrono>
using webfuse_test::WebfuseServer; using webfuse_test::WebfuseServer;
using webfuse_test::MockProviderClient; using webfuse_test::MockProviderClient;
using webfuse_test::Client; using webfuse_test::Client;
using testing::Invoke;
#define TIMEOUT (std::chrono::seconds(10))
TEST(Client, Connect) TEST(Client, Connect)
{ {
MockProviderClient provider; MockProviderClient provider;
EXPECT_CALL(provider, OnConnected()).Times(1);
EXPECT_CALL(provider, OnDisconnected()).Times(1); std::promise<void> connected;
EXPECT_CALL(provider, OnConnected()).Times(1)
.WillOnce(Invoke([&]() { connected.set_value(); }));
std::promise<void> disconnected;
EXPECT_CALL(provider, OnDisconnected()).Times(1)
.WillOnce(Invoke([&]() { disconnected.set_value(); }));
wfp_client_config * config = wfp_client_config_create(); wfp_client_config * config = wfp_client_config_create();
provider.AttachTo(config); provider.AttachTo(config);
{
WebfuseServer server; WebfuseServer server;
Client client(config, server.GetUrl()); Client client(config, server.GetUrl());
server.AwaitConnection(); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config); wfp_client_config_dispose(config);
} }
@ -29,8 +45,14 @@ TEST(Client, Connect)
TEST(Client, ConnectWithTls) TEST(Client, ConnectWithTls)
{ {
MockProviderClient provider; MockProviderClient provider;
EXPECT_CALL(provider, OnConnected()).Times(1);
EXPECT_CALL(provider, OnDisconnected()).Times(1); std::promise<void> connected;
EXPECT_CALL(provider, OnConnected()).Times(1)
.WillOnce(Invoke([&]() { connected.set_value(); }));
std::promise<void> disconnected;
EXPECT_CALL(provider, OnDisconnected()).Times(1)
.WillOnce(Invoke([&]() { disconnected.set_value(); }));
wfp_client_config * config = wfp_client_config_create(); wfp_client_config * config = wfp_client_config_create();
wfp_client_config_set_certpath(config, "client-cert.pem"); wfp_client_config_set_certpath(config, "client-cert.pem");
@ -38,10 +60,15 @@ TEST(Client, ConnectWithTls)
wfp_client_config_set_ca_filepath(config, "server-cert.pem"); wfp_client_config_set_ca_filepath(config, "server-cert.pem");
provider.AttachTo(config); provider.AttachTo(config);
{
WebfuseServer server(true); WebfuseServer server(true);
Client client(config, server.GetUrl()); Client client(config, server.GetUrl());
server.AwaitConnection(); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config); wfp_client_config_dispose(config);
} }

View File

@ -3,6 +3,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <queue>
namespace webfuse_test namespace webfuse_test
{ {
@ -20,35 +21,76 @@ public:
~Private() ~Private()
{ {
{ invoke(shutdown);
std::unique_lock<std::mutex> lock(mutex);
is_shutdown_requested = true;
}
wfp_client_interrupt(client);
thread.join(); thread.join();
wfp_client_disconnect(client);
wfp_client_dispose(client); wfp_client_dispose(client);
} }
void Disconnect()
{
invoke(disconnect);
}
private: private:
enum command
{
run,
shutdown,
disconnect
};
void invoke(command cmd)
{
{
std::unique_lock<std::mutex> lock(mutex);
commands.push(cmd);
}
wfp_client_interrupt(client);
}
static void Run(Private * self) static void Run(Private * self)
{ {
bool is_running = true; bool is_running = true;
while (is_running) while (is_running)
{ {
wfp_client_service(self->client); switch (self->get_command())
{ {
std::unique_lock<std::mutex> lock(self->mutex); case run:
is_running = !self->is_shutdown_requested; wfp_client_service(self->client);
break;
case shutdown:
is_running = false;
break;
case disconnect:
wfp_client_disconnect(self->client);
break;
default:
break;
} }
} }
} }
command get_command()
{
command result = run;
std::unique_lock<std::mutex> lock(mutex);
if (!commands.empty())
{
result = commands.front();
commands.pop();
}
return result;
}
wfp_client * client; wfp_client * client;
bool is_shutdown_requested; bool is_shutdown_requested;
std::thread thread; std::thread thread;
std::mutex mutex; std::mutex mutex;
std::queue<command> commands;
}; };
Client::Client(wfp_client_config * config, std::string const & url) Client::Client(wfp_client_config * config, std::string const & url)
@ -62,5 +104,10 @@ Client::~Client()
delete d; delete d;
} }
void Client::Disconnect()
{
d->Disconnect();
}
} }

View File

@ -14,6 +14,7 @@ class Client
public: public:
Client(wfp_client_config * config, std::string const & url); Client(wfp_client_config * config, std::string const & url);
~Client(); ~Client();
void Disconnect();
private: private:
class Private; class Private;
Private * d; Private * d;

View File

@ -74,8 +74,7 @@ class WebfuseServer::Private: public IServer
{ {
public: public:
Private(bool use_tls) Private(bool use_tls)
: is_connected(false) : is_shutdown_requested(false)
, is_shutdown_requested(false)
, client(nullptr) , client(nullptr)
{ {
wfp_impl_lwslog_disable(); wfp_impl_lwslog_disable();
@ -133,17 +132,6 @@ public:
return url; return url;
} }
void AwaitConnection()
{
bool is_finished = false;
while (!is_finished)
{
std::this_thread::yield();
std::unique_lock<std::mutex> lock(mutex);
is_finished = is_connected;
}
}
json_t * Invoke(std::string const & method, json_t * params) json_t * Invoke(std::string const & method, json_t * params)
{ {
throw std::runtime_error("not implemented"); throw std::runtime_error("not implemented");
@ -183,7 +171,6 @@ public:
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
write_queue.push(response_text); write_queue.push(response_text);
is_connected = true;
} }
free(response_text); free(response_text);
json_decref(response); json_decref(response);
@ -238,7 +225,6 @@ private:
} }
} }
bool is_connected;
bool is_shutdown_requested; bool is_shutdown_requested;
lws * client; lws * client;
std::string url; std::string url;
@ -267,11 +253,6 @@ std::string const & WebfuseServer::GetUrl()
return d->GetUrl(); return d->GetUrl();
} }
void WebfuseServer::AwaitConnection()
{
d->AwaitConnection();
}
json_t * WebfuseServer::Invoke(std::string method, json_t * params) json_t * WebfuseServer::Invoke(std::string method, json_t * params)
{ {
return d->Invoke(method, params); return d->Invoke(method, params);

View File

@ -15,7 +15,6 @@ public:
WebfuseServer(bool use_tls = false); WebfuseServer(bool use_tls = false);
~WebfuseServer(); ~WebfuseServer();
std::string const & GetUrl(); std::string const & GetUrl();
void AwaitConnection();
json_t * Invoke(std::string method, json_t * params); json_t * Invoke(std::string method, json_t * params);
private: private:
class Private; class Private;