1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +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

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