From 555058dbb578908576315b559a9c764325743814 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 23 Feb 2020 21:02:01 +0100 Subject: [PATCH] made client protocol test single threaded --- include/webfuse/core/protocol_names.h | 21 ++++++++ include/webfuse_adapter.h | 1 + include/webfuse_provider.h | 1 + lib/webfuse/adapter/impl/server.c | 1 - lib/webfuse/adapter/impl/server_protocol.c | 2 + lib/webfuse/provider/impl/client.c | 1 - lib/webfuse/provider/impl/client_protocol.c | 8 +-- lib/webfuse/provider/impl/client_protocol.h | 2 - .../tests/provider/test_client_protocol.cc | 53 ++++++------------- test/webfuse/utils/ws_server.cc | 37 ++++++++++--- test/webfuse/utils/ws_server.hpp | 2 + 11 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 include/webfuse/core/protocol_names.h diff --git a/include/webfuse/core/protocol_names.h b/include/webfuse/core/protocol_names.h new file mode 100644 index 0000000..4de8c4a --- /dev/null +++ b/include/webfuse/core/protocol_names.h @@ -0,0 +1,21 @@ +//////////////////////////////////////////////////////////////////////////////// +/// \file protocol_names.h +/// \brief Names of websocket protocol. +//////////////////////////////////////////////////////////////////////////////// +#ifndef WF_PROTOCOL_NAMES_H +#define WF_PROTOCOL_NAMES_H + + +//------------------------------------------------------------------------------ +/// \def WF_PROTOCOL_NAME_ADAPTER_SERVER +/// \brief Name of the websocket protocol an adapter server is running. +//------------------------------------------------------------------------------ +#define WF_PROTOCOL_NAME_ADAPTER_SERVER ("webfuse-adapter-server") + +//------------------------------------------------------------------------------ +/// \def WF_PROTOCOL_NAME_PROVIDER_CLIENT +/// \brief Name of the websocket protocol an provider client is running. +//------------------------------------------------------------------------------ +#define WF_PROTOCOL_NAME_PROVIDER_CLIENT ("webfuse-provider-client") + +#endif diff --git a/include/webfuse_adapter.h b/include/webfuse_adapter.h index af37c14..1ff048f 100644 --- a/include/webfuse_adapter.h +++ b/include/webfuse_adapter.h @@ -7,6 +7,7 @@ #define WF_ADAPTER_H #include +#include #include #include diff --git a/include/webfuse_provider.h b/include/webfuse_provider.h index bd119ac..7af3934 100644 --- a/include/webfuse_provider.h +++ b/include/webfuse_provider.h @@ -7,6 +7,7 @@ #define WF_PROVIDER_H #include +#include #include #include diff --git a/lib/webfuse/adapter/impl/server.c b/lib/webfuse/adapter/impl/server.c index ad3e5c2..bf32b85 100644 --- a/lib/webfuse/adapter/impl/server.c +++ b/lib/webfuse/adapter/impl/server.c @@ -38,7 +38,6 @@ static struct lws_context * wf_impl_server_context_create( memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT); server->ws_protocols[0].name = "http"; server->ws_protocols[0].callback = lws_callback_http_dummy; - server->ws_protocols[1].name = "fs"; wf_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]); memset(&server->mount, 0, sizeof(struct lws_http_mount)); diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 02efd80..74d4631 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -6,6 +6,7 @@ #include "webfuse/core/message.h" #include "webfuse/core/util.h" +#include "webfuse/core/protocol_names.h" #include "webfuse/adapter/impl/credentials.h" #include "webfuse/adapter/impl/jsonrpc/request.h" @@ -121,6 +122,7 @@ void wf_impl_server_protocol_init_lws( struct wf_server_protocol * protocol, struct lws_protocols * lws_protocol) { + lws_protocol->name = WF_PROTOCOL_NAME_ADAPTER_SERVER; lws_protocol->callback = &wf_impl_server_protocol_callback; lws_protocol->per_session_data_size = 0; lws_protocol->user = protocol; diff --git a/lib/webfuse/provider/impl/client.c b/lib/webfuse/provider/impl/client.c index 8b3c06b..4ddae92 100644 --- a/lib/webfuse/provider/impl/client.c +++ b/lib/webfuse/provider/impl/client.c @@ -35,7 +35,6 @@ struct wfp_client * wfp_impl_client_create( wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data); memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT); - client->protocols[0].name = WFP_CLIENT_PROTOCOL_NAME; wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]); memset(&client->info, 0, sizeof(struct lws_context_creation_info)); diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 6d5ba85..59945bd 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -6,7 +6,6 @@ #include #include - #include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/provider.h" #include "webfuse/core/util.h" @@ -14,6 +13,7 @@ #include "webfuse/core/message_queue.h" #include "webfuse/core/container_of.h" #include "webfuse/provider/impl/url.h" +#include "webfuse/core/protocol_names.h" static void wfp_impl_client_protocol_respond( json_t * response, @@ -112,7 +112,7 @@ static int wfp_impl_client_protocol_callback( // fall-through case LWS_CALLBACK_CLIENT_WRITEABLE: if ((wsi == protocol->wsi) && (!wf_slist_empty(&protocol->messages))) - { + { struct wf_slist_item * item = wf_slist_remove_first(&protocol->messages); struct wf_message * message = wf_container_of(item, struct wf_message, item); lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT); @@ -180,6 +180,7 @@ void wfp_impl_client_protocol_init_lws( struct wfp_client_protocol * protocol, struct lws_protocols * lws_protocol) { + lws_protocol->name = WF_PROTOCOL_NAME_PROVIDER_CLIENT; lws_protocol->callback = &wfp_impl_client_protocol_callback; lws_protocol->per_session_data_size = 0; lws_protocol->user = protocol; @@ -203,7 +204,8 @@ void wfp_impl_client_protocol_connect( info.host = info.address; info.origin = info.address; info.ssl_connection = (url_data.use_tls) ? LCCSCF_USE_SSL : 0; - info.protocol = WFP_CLIENT_PROTOCOL_NAME; + info.protocol = WF_PROTOCOL_NAME_ADAPTER_SERVER; + info.local_protocol_name = WF_PROTOCOL_NAME_PROVIDER_CLIENT; info.pwsi = &protocol->wsi; lws_client_connect_via_info(&info); diff --git a/lib/webfuse/provider/impl/client_protocol.h b/lib/webfuse/provider/impl/client_protocol.h index d197559..d6f537d 100644 --- a/lib/webfuse/provider/impl/client_protocol.h +++ b/lib/webfuse/provider/impl/client_protocol.h @@ -11,8 +11,6 @@ extern "C" { #endif -#define WFP_CLIENT_PROTOCOL_NAME ("fs") - struct wfp_client_config; struct lws_protocols; struct lws_context; diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index beeaf52..b602e58 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -22,7 +22,6 @@ public: : server(nullptr) , config(nullptr) , protocol(nullptr) - , context(nullptr) { // empty } @@ -30,41 +29,29 @@ public: protected: void SetUp() { - server = new WebsocketServer(54321); - config = wfp_client_config_create(); protocol = wfp_client_protocol_create(config); - struct lws_protocols protocols[2]; - memset(protocols, 0, sizeof(struct lws_protocols) * 2); - protocols[0].name = "fs"; - wfp_client_protocol_init_lws(protocol, &protocols[0]); + struct lws_protocols client_protocol; + memset(&client_protocol, 0, sizeof(struct lws_protocols)); + wfp_client_protocol_init_lws(protocol, &client_protocol); - struct lws_context_creation_info info; - memset(&info, 0, sizeof(struct lws_context_creation_info)); - info.port = CONTEXT_PORT_NO_LISTEN; - info.protocols = protocols; - info.uid = -1; - info.gid = -1; - - context = lws_create_context(&info); - wfp_client_protocol_connect(protocol, context, "ws://localhost:54321/"); - - isShutdownRequested = false; - thread = std::thread(run, this); + server = new WebsocketServer(54321, &client_protocol, 1); } void TearDown() { - isShutdownRequested = true; - thread.join(); - - lws_context_destroy(context); + delete server; wfp_client_protocol_dispose(protocol); wfp_client_config_dispose(config); - delete server; + } + + void connect() + { + wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/"); + server->waitForConnection(); } void awaitAddFilesystem(std::string& filesystemName) @@ -103,20 +90,8 @@ protected: WebsocketServer * server; private: - static void run(ClientProtocolTest * self) - { - while (!self->isShutdownRequested) - { - lws_service(self->context, 100); - } - } - wfp_client_config * config; wfp_client_protocol * protocol; - std::thread thread; - std::atomic isShutdownRequested; - lws_context * context; - }; @@ -126,12 +101,14 @@ private: TEST_F(ClientProtocolTest, connect) { - server->waitForConnection(); + connect(); + if (HasFatalFailure()) { return; } } TEST_F(ClientProtocolTest, getattr) { - server->waitForConnection(); + connect(); + if (HasFatalFailure()) { return; } std::string filesystem; awaitAddFilesystem(filesystem); diff --git a/test/webfuse/utils/ws_server.cc b/test/webfuse/utils/ws_server.cc index 995d0b8..48e2588 100644 --- a/test/webfuse/utils/ws_server.cc +++ b/test/webfuse/utils/ws_server.cc @@ -2,6 +2,7 @@ #include "webfuse/utils/timeout_watcher.hpp" #include "webfuse/core/util.h" +#include "webfuse/core/protocol_names.h" #include #include #include @@ -76,15 +77,22 @@ namespace webfuse_test class WebsocketServer::Private: public IServer { public: - explicit Private(int port) + Private(int port, struct lws_protocols * additionalProtocols, size_t additionalProtocolsCount) : client_wsi(nullptr) { - memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2); - ws_protocols[0].name = "fs"; + ws_protocols = new struct lws_protocols[2 + additionalProtocolsCount]; + memset(ws_protocols, 0, sizeof(struct lws_protocols) * (2 + additionalProtocolsCount)); + + ws_protocols[0].name = WF_PROTOCOL_NAME_ADAPTER_SERVER; ws_protocols[0].callback = &wf_test_utils_ws_server_callback; ws_protocols[0].per_session_data_size = 0; ws_protocols[0].user = reinterpret_cast(this); + if (0 < additionalProtocolsCount) + { + memcpy(&ws_protocols[additionalProtocolsCount], additionalProtocols, sizeof(struct lws_protocols) * additionalProtocolsCount); + } + memset(&info, 0, sizeof(struct lws_context_creation_info)); info.port = port; info.mounts = NULL; @@ -94,11 +102,18 @@ public: info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; context = lws_create_context(&info); + } virtual ~Private() { lws_context_destroy(context); + delete[] ws_protocols; + } + + struct lws_context * getContext() + { + return context; } void waitForConnection() @@ -130,7 +145,6 @@ public: lws_service(context, 100); } } - } json_t * receiveMessage() @@ -203,7 +217,7 @@ private: struct lws * client_wsi; - struct lws_protocols ws_protocols[2]; + struct lws_protocols * ws_protocols; struct lws_context_creation_info info; struct lws_context * context; std::queue writeQueue; @@ -212,7 +226,13 @@ private: }; WebsocketServer::WebsocketServer(int port) -: d(new Private(port)) +: d(new Private(port, nullptr, 0)) +{ + +} + +WebsocketServer::WebsocketServer(int port, struct lws_protocols * additionalProtocols, std::size_t additionalProtocolsCount) +: d(new Private(port, additionalProtocols, additionalProtocolsCount)) { } @@ -222,6 +242,11 @@ WebsocketServer::~WebsocketServer() delete d; } +struct lws_context * WebsocketServer::getContext() +{ + return d->getContext(); +} + void WebsocketServer::waitForConnection() { d->waitForConnection(); diff --git a/test/webfuse/utils/ws_server.hpp b/test/webfuse/utils/ws_server.hpp index bacab51..e9d825a 100644 --- a/test/webfuse/utils/ws_server.hpp +++ b/test/webfuse/utils/ws_server.hpp @@ -13,7 +13,9 @@ class WebsocketServer WebsocketServer & operator=(WebsocketServer const &) = delete; public: explicit WebsocketServer(int port); + WebsocketServer(int port, struct lws_protocols * additionalProtocols, std::size_t additionalProtocolsCount); ~WebsocketServer(); + struct lws_context * getContext(); void waitForConnection(); void sendMessage(json_t * message); json_t * receiveMessage();