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

made client protocol test single threaded

This commit is contained in:
Falk Werner 2020-02-23 21:02:01 +01:00
parent 8841ac40f8
commit 555058dbb5
11 changed files with 78 additions and 51 deletions

View File

@ -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

View File

@ -7,6 +7,7 @@
#define WF_ADAPTER_H
#include <webfuse/core/status.h>
#include <webfuse/core/protocol_names.h>
#include <webfuse/adapter/api.h>
#include <webfuse/adapter/server.h>

View File

@ -7,6 +7,7 @@
#define WF_PROVIDER_H
#include <webfuse/core/status.h>
#include <webfuse/core/protocol_names.h>
#include <webfuse/provider/api.h>
#include <webfuse/provider/client.h>

View File

@ -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));

View File

@ -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;

View File

@ -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));

View File

@ -6,7 +6,6 @@
#include <libwebsockets.h>
#include <jansson.h>
#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);

View File

@ -11,8 +11,6 @@ extern "C"
{
#endif
#define WFP_CLIENT_PROTOCOL_NAME ("fs")
struct wfp_client_config;
struct lws_protocols;
struct lws_context;

View File

@ -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<bool> 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);

View File

@ -2,6 +2,7 @@
#include "webfuse/utils/timeout_watcher.hpp"
#include "webfuse/core/util.h"
#include "webfuse/core/protocol_names.h"
#include <libwebsockets.h>
#include <cstring>
#include <vector>
@ -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<void*>(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<std::string> 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();

View File

@ -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();