Add test for releasing connections back to the server

This commit is contained in:
2025-08-22 13:10:26 -04:00
parent 7860e720bd
commit 1a85e91113
9 changed files with 168 additions and 42 deletions

View File

@@ -0,0 +1,96 @@
#include "../src/ThreadPipeline.h"
#include "config.hpp"
#include "connection.hpp"
#include "perfetto_categories.hpp"
#include "server.hpp"
#include <doctest/doctest.h>
#include <thread>
// Perfetto static storage for tests
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
struct Message {
std::unique_ptr<Connection> conn;
std::string data;
bool done;
};
struct EchoHandler : public ConnectionHandler {
private:
ThreadPipeline<Message> &pipeline;
public:
explicit EchoHandler(ThreadPipeline<Message> &pipeline)
: pipeline(pipeline) {}
void on_data_arrived(std::string_view data,
std::unique_ptr<Connection> &conn_ptr) override {
assert(conn_ptr);
auto guard = pipeline.push(1, true);
for (auto &message : guard.batch) {
message.conn = std::move(conn_ptr);
message.data = data;
message.done = false;
}
}
};
TEST_CASE("Echo server with connection ownership transfer") {
weaseldb::Config config;
config.server.io_threads = 1;
config.server.epoll_instances = 1;
ThreadPipeline<Message> pipeline{10, {1}};
EchoHandler handler{pipeline};
auto echoThread = std::thread{[&]() {
for (;;) {
auto guard = pipeline.acquire(0, 0);
for (auto &message : guard.batch) {
bool done = message.done;
if (done) {
return;
}
assert(message.conn);
message.conn->appendMessage(message.data);
Server::releaseBackToServer(std::move(message.conn));
}
}
}};
// Create server with NO listen sockets (empty vector)
auto server = Server::create(config, handler, {});
std::thread server_thread([&server]() { server->run(); });
// Create local connection
int client_fd = server->createLocalConnection();
REQUIRE(client_fd > 0);
// Write some test data
const char *test_message = "Hello, World!";
ssize_t bytes_written = write(client_fd, test_message, strlen(test_message));
REQUIRE(bytes_written == strlen(test_message));
// Read the echoed response
char buffer[1024] = {0};
ssize_t bytes_read = read(client_fd, buffer, sizeof(buffer) - 1);
if (bytes_read == -1) {
perror("read failed");
}
REQUIRE(bytes_read == strlen(test_message));
// Verify we got back exactly what we sent
CHECK(std::string(buffer, bytes_read) == std::string(test_message));
// Cleanup
close(client_fd);
server->shutdown();
server_thread.join();
{
auto guard = pipeline.push(1, true);
for (auto &message : guard.batch) {
message.done = true;
}
}
echoThread.join();
}