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:
parent
dc6e05b1b8
commit
aa06846c15
@ -4,24 +4,40 @@
|
||||
#include "webfuse_provider/test_util/client.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <future>
|
||||
#include <chrono>
|
||||
|
||||
using webfuse_test::WebfuseServer;
|
||||
using webfuse_test::MockProviderClient;
|
||||
using webfuse_test::Client;
|
||||
using testing::Invoke;
|
||||
|
||||
#define TIMEOUT (std::chrono::seconds(10))
|
||||
|
||||
TEST(Client, Connect)
|
||||
{
|
||||
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();
|
||||
provider.AttachTo(config);
|
||||
|
||||
WebfuseServer server;
|
||||
Client client(config, server.GetUrl());
|
||||
{
|
||||
WebfuseServer server;
|
||||
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);
|
||||
}
|
||||
@ -29,8 +45,14 @@ TEST(Client, Connect)
|
||||
TEST(Client, ConnectWithTls)
|
||||
{
|
||||
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_set_certpath(config, "client-cert.pem");
|
||||
@ -38,10 +60,15 @@ TEST(Client, ConnectWithTls)
|
||||
wfp_client_config_set_ca_filepath(config, "server-cert.pem");
|
||||
provider.AttachTo(config);
|
||||
|
||||
WebfuseServer server(true);
|
||||
Client client(config, server.GetUrl());
|
||||
{
|
||||
WebfuseServer server(true);
|
||||
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);
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
@ -20,35 +21,76 @@ public:
|
||||
|
||||
~Private()
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
is_shutdown_requested = true;
|
||||
}
|
||||
invoke(shutdown);
|
||||
|
||||
wfp_client_interrupt(client);
|
||||
thread.join();
|
||||
wfp_client_disconnect(client);
|
||||
wfp_client_dispose(client);
|
||||
}
|
||||
|
||||
void Disconnect()
|
||||
{
|
||||
invoke(disconnect);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
bool is_running = true;
|
||||
while (is_running)
|
||||
{
|
||||
wfp_client_service(self->client);
|
||||
switch (self->get_command())
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(self->mutex);
|
||||
is_running = !self->is_shutdown_requested;
|
||||
case run:
|
||||
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;
|
||||
bool is_shutdown_requested;
|
||||
std::thread thread;
|
||||
std::mutex mutex;
|
||||
std::queue<command> commands;
|
||||
};
|
||||
|
||||
Client::Client(wfp_client_config * config, std::string const & url)
|
||||
@ -62,5 +104,10 @@ Client::~Client()
|
||||
delete d;
|
||||
}
|
||||
|
||||
void Client::Disconnect()
|
||||
{
|
||||
d->Disconnect();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,6 +14,7 @@ class Client
|
||||
public:
|
||||
Client(wfp_client_config * config, std::string const & url);
|
||||
~Client();
|
||||
void Disconnect();
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
|
@ -74,8 +74,7 @@ class WebfuseServer::Private: public IServer
|
||||
{
|
||||
public:
|
||||
Private(bool use_tls)
|
||||
: is_connected(false)
|
||||
, is_shutdown_requested(false)
|
||||
: is_shutdown_requested(false)
|
||||
, client(nullptr)
|
||||
{
|
||||
wfp_impl_lwslog_disable();
|
||||
@ -133,17 +132,6 @@ public:
|
||||
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)
|
||||
{
|
||||
throw std::runtime_error("not implemented");
|
||||
@ -183,7 +171,6 @@ public:
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
write_queue.push(response_text);
|
||||
is_connected = true;
|
||||
}
|
||||
free(response_text);
|
||||
json_decref(response);
|
||||
@ -238,7 +225,6 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
bool is_connected;
|
||||
bool is_shutdown_requested;
|
||||
lws * client;
|
||||
std::string url;
|
||||
@ -267,11 +253,6 @@ std::string const & WebfuseServer::GetUrl()
|
||||
return d->GetUrl();
|
||||
}
|
||||
|
||||
void WebfuseServer::AwaitConnection()
|
||||
{
|
||||
d->AwaitConnection();
|
||||
}
|
||||
|
||||
json_t * WebfuseServer::Invoke(std::string method, json_t * params)
|
||||
{
|
||||
return d->Invoke(method, params);
|
||||
|
@ -15,7 +15,6 @@ public:
|
||||
WebfuseServer(bool use_tls = false);
|
||||
~WebfuseServer();
|
||||
std::string const & GetUrl();
|
||||
void AwaitConnection();
|
||||
json_t * Invoke(std::string method, json_t * params);
|
||||
private:
|
||||
class Private;
|
||||
|
Loading…
Reference in New Issue
Block a user