Separate Connection and Request lifetimes
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include "server.hpp"
|
||||
|
||||
#include <doctest/doctest.h>
|
||||
#include <future>
|
||||
#include <latch>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
@@ -19,19 +19,16 @@ static std::string_view arena_copy_string(std::string_view str, Arena &arena) {
|
||||
}
|
||||
|
||||
struct EchoHandler : ConnectionHandler {
|
||||
std::future<void> f;
|
||||
Arena arena;
|
||||
std::span<std::string_view> reply;
|
||||
WeakRef<MessageSender> wconn;
|
||||
std::latch done{1};
|
||||
void on_data_arrived(std::string_view data, Connection &conn) override {
|
||||
Arena arena;
|
||||
auto reply = std::span{arena.allocate<std::string_view>(1), 1};
|
||||
reply = std::span{arena.allocate<std::string_view>(1), 1};
|
||||
reply[0] = arena_copy_string(data, arena);
|
||||
f = std::async(std::launch::async, [wconn = conn.get_weak_ref(), reply,
|
||||
arena = std::move(arena)]() mutable {
|
||||
if (auto conn = wconn.lock()) {
|
||||
conn->append_message(reply, std::move(arena));
|
||||
} else {
|
||||
REQUIRE(false);
|
||||
}
|
||||
});
|
||||
wconn = conn.get_weak_ref();
|
||||
CHECK(wconn.lock());
|
||||
done.count_down();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -44,16 +41,23 @@ TEST_CASE("Echo test") {
|
||||
|
||||
auto runThread = std::thread{[&]() { server->run(); }};
|
||||
|
||||
SUBCASE("writes hello back") {
|
||||
int w = write(fd, "hello", 5);
|
||||
REQUIRE(w == 5);
|
||||
char buf[6];
|
||||
buf[5] = 0;
|
||||
int r = read(fd, buf, 5);
|
||||
REQUIRE(r == 5);
|
||||
CHECK(std::string(buf) == "hello");
|
||||
int w = write(fd, "hello", 5);
|
||||
REQUIRE(w == 5);
|
||||
|
||||
handler.done.wait();
|
||||
if (auto conn = handler.wconn.lock()) {
|
||||
conn->append_message(std::exchange(handler.reply, {}),
|
||||
std::move(handler.arena));
|
||||
} else {
|
||||
REQUIRE(false);
|
||||
}
|
||||
|
||||
char buf[6];
|
||||
buf[5] = 0;
|
||||
int r = read(fd, buf, 5);
|
||||
REQUIRE(r == 5);
|
||||
CHECK(std::string(buf) == "hello");
|
||||
|
||||
close(fd);
|
||||
|
||||
server->shutdown();
|
||||
|
||||
Reference in New Issue
Block a user