From aa06846c154ba7227f5a866f150ac035b0719a1a Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Thu, 25 Jun 2020 19:21:00 +0200 Subject: [PATCH] fixed tests --- test/webfuse_provider/provider/test_client.cc | 47 +++++++++++--- test/webfuse_provider/test_util/client.cc | 65 ++++++++++++++++--- test/webfuse_provider/test_util/client.hpp | 1 + .../test_util/webfuse_server.cc | 21 +----- .../test_util/webfuse_server.hpp | 1 - 5 files changed, 95 insertions(+), 40 deletions(-) diff --git a/test/webfuse_provider/provider/test_client.cc b/test/webfuse_provider/provider/test_client.cc index 18a13a5..1ec14c0 100644 --- a/test/webfuse_provider/provider/test_client.cc +++ b/test/webfuse_provider/provider/test_client.cc @@ -4,24 +4,40 @@ #include "webfuse_provider/test_util/client.hpp" #include +#include +#include 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 connected; + EXPECT_CALL(provider, OnConnected()).Times(1) + .WillOnce(Invoke([&]() { connected.set_value(); })); + + std::promise 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 connected; + EXPECT_CALL(provider, OnConnected()).Times(1) + .WillOnce(Invoke([&]() { connected.set_value(); })); + + std::promise 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); } \ No newline at end of file diff --git a/test/webfuse_provider/test_util/client.cc b/test/webfuse_provider/test_util/client.cc index 64d6f50..a213f88 100644 --- a/test/webfuse_provider/test_util/client.cc +++ b/test/webfuse_provider/test_util/client.cc @@ -3,6 +3,7 @@ #include #include +#include namespace webfuse_test { @@ -20,35 +21,76 @@ public: ~Private() { - { - std::unique_lock 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 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 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 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 commands; }; Client::Client(wfp_client_config * config, std::string const & url) @@ -62,5 +104,10 @@ Client::~Client() delete d; } +void Client::Disconnect() +{ + d->Disconnect(); +} + } \ No newline at end of file diff --git a/test/webfuse_provider/test_util/client.hpp b/test/webfuse_provider/test_util/client.hpp index 32b2341..9b0412b 100644 --- a/test/webfuse_provider/test_util/client.hpp +++ b/test/webfuse_provider/test_util/client.hpp @@ -14,6 +14,7 @@ class Client public: Client(wfp_client_config * config, std::string const & url); ~Client(); + void Disconnect(); private: class Private; Private * d; diff --git a/test/webfuse_provider/test_util/webfuse_server.cc b/test/webfuse_provider/test_util/webfuse_server.cc index ec6b20b..5c8fa0b 100644 --- a/test/webfuse_provider/test_util/webfuse_server.cc +++ b/test/webfuse_provider/test_util/webfuse_server.cc @@ -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 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 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); diff --git a/test/webfuse_provider/test_util/webfuse_server.hpp b/test/webfuse_provider/test_util/webfuse_server.hpp index e0b3393..35537ce 100644 --- a/test/webfuse_provider/test_util/webfuse_server.hpp +++ b/test/webfuse_provider/test_util/webfuse_server.hpp @@ -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;