Don't count_down past 0
CI / pre-commit (push) Successful in 59s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64) (push) Successful in 2m22s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64) (push) Successful in 2m57s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64) (push) Successful in 2m44s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64) (push) Successful in 3m10s

This commit is contained in:
2026-07-17 21:15:23 -04:00
parent a427278cc0
commit 3b7cc2a70f
+20 -6
View File
@@ -3,22 +3,36 @@
#include "connection_handler.hpp" #include "connection_handler.hpp"
#include "server.hpp" #include "server.hpp"
#include <atomic>
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <latch> #include <latch>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
struct Event {
void wait() { done_.wait(); }
void set() {
if (!set_.exchange(true, std::memory_order_relaxed)) {
done_.count_down();
}
}
private:
std::latch done_{1};
std::atomic<bool> set_;
};
struct EchoHandler : ConnectionHandler { struct EchoHandler : ConnectionHandler {
Arena arena; Arena arena;
std::span<std::string_view> reply; std::span<std::string_view> reply;
WeakRef<MessageSender> wconn; WeakRef<MessageSender> wconn;
std::latch done{1}; Event done;
void on_data_arrived(std::string_view data, Connection &conn) override { void on_data_arrived(std::string_view data, Connection &conn) override {
reply = arena.allocate_span<std::string_view>(1); reply = arena.allocate_span<std::string_view>(1);
reply[0] = arena.copy_string(data); reply[0] = arena.copy_string(data);
wconn = conn.get_weak_ref(); wconn = conn.get_weak_ref();
CHECK(wconn.lock()); CHECK(wconn.lock());
done.count_down(); done.set();
} }
}; };
@@ -60,8 +74,8 @@ struct ShutdownTestHandler : ConnectionHandler {
Arena arena; Arena arena;
std::span<std::string_view> reply; std::span<std::string_view> reply;
WeakRef<MessageSender> wconn; WeakRef<MessageSender> wconn;
std::latch received_data{1}; Event received_data;
std::latch connection_closed_latch{1}; Event connection_closed_latch;
ConnectionShutdown shutdown_mode = ConnectionShutdown::None; ConnectionShutdown shutdown_mode = ConnectionShutdown::None;
std::atomic<bool> connection_closed{false}; std::atomic<bool> connection_closed{false};
@@ -69,12 +83,12 @@ struct ShutdownTestHandler : ConnectionHandler {
reply = arena.allocate_span<std::string_view>(1); reply = arena.allocate_span<std::string_view>(1);
reply[0] = arena.copy_string(data); reply[0] = arena.copy_string(data);
wconn = conn.get_weak_ref(); wconn = conn.get_weak_ref();
received_data.count_down(); received_data.set();
} }
void on_connection_closed(Connection &) override { void on_connection_closed(Connection &) override {
connection_closed = true; connection_closed = true;
connection_closed_latch.count_down(); connection_closed_latch.set();
} }
}; };