From 24724379c50c0046d762351ca9f40719ff035c5c Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Tue, 3 Nov 2026 21:38:36 -0700 Subject: [PATCH 1/2] Test which thread the Logger is called on The README and the Logger typedef both say a failed delivery is mentioned on the client's own thread and a full queue on the thread that called report, and nothing asserted either. Log now records the calling thread with each line. Closes #23 Co-Authored-By: Claude Opus 5.5 --- test/test_trace_client.cpp | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 8c0a329..e937d7b 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -254,10 +254,12 @@ class StubServer { struct Log { std::mutex mutex; std::vector lines; + std::vector threads; // who called the logger, line for line trace_client::Logger logger() { return [this](const std::string &line) { std::lock_guard lock(mutex); lines.push_back(line); + threads.push_back(std::this_thread::get_id()); }; } std::size_t count(const std::string &needle) { @@ -266,6 +268,14 @@ struct Log { for (std::size_t i = 0; i < lines.size(); ++i) if (lines[i].find(needle) != std::string::npos) ++n; return n; } + std::size_t countOn(const std::string &needle, std::thread::id thread) { + std::lock_guard lock(mutex); + std::size_t n = 0; + for (std::size_t i = 0; i < lines.size(); ++i) { + if (threads[i] == thread && lines[i].find(needle) != std::string::npos) ++n; + } + return n; + } }; static void cleanEnvironment() { @@ -450,6 +460,51 @@ static void theLoggerIsNeverCalledOnceCloseReturns() { CHECK_EQ(atClose, log.count("[trace]")); } +static void aFailedDeliveryIsLoggedOnTheClientsOwnThread() { + // Both kinds of failed delivery: the server said no, and nothing answered. + int port; + { + StubServer closed; // bind a port, then let it go + port = std::atoi(closed.baseUrl().substr(closed.baseUrl().rfind(':') + 1).c_str()); + } + StubServer server; + server.status = 500; + Log log; + { + trace_client::TraceClient rejected(server.baseUrl(), "MyGame", "1.2.3", "k", true, log.logger()); + rejected.report("startup"); + rejected.close(); + trace_client::TraceClient unreachable("http://127.0.0.1:" + std::to_string(port), "MyGame", "1.2.3", "k", true, log.logger()); + unreachable.report("startup"); + unreachable.close(); + } + std::thread::id reporter = std::this_thread::get_id(); + CHECK_EQ(1u, log.count("answered 500")); + CHECK_EQ(0u, log.countOn("answered 500", reporter)); + CHECK_EQ(1u, log.count("could not deliver")); + CHECK_EQ(0u, log.countOn("could not deliver", reporter)); +} + +static void aFullQueueIsLoggedOnTheThreadThatCalledReport() { + // Reported from a thread of its own, so "the reporting thread" cannot be + // confused with the one that built the client and will close it. + StubServer server; + server.hold(); // the sender waits on the first report + Log log; + trace_client::TraceClient client(server.baseUrl(), "MyGame", "1.2.3", "k", true, log.logger()); + const std::size_t flood = trace_client::QUEUE_CAPACITY * 3; + std::thread flooder([&client, flood] { + for (std::size_t i = 0; i < flood; ++i) client.report("flood"); + }); + std::thread::id reporter = flooder.get_id(); + flooder.join(); + std::size_t full = log.count("queue full"); + CHECK(full >= flood - trace_client::QUEUE_CAPACITY - 1); + CHECK_EQ(full, log.countOn("queue full", reporter)); + server.release(); + client.close(1.0); +} + static void missingCurlIsAnUnreachableServer() { #if defined(TRACE_CLIENT_POSIX) StubServer server; @@ -1426,6 +1481,8 @@ int main() { {"aLoggerMayItselfReport", aLoggerMayItselfReport}, {"aLoggerMayReportWhileCloseIsRunning", aLoggerMayReportWhileCloseIsRunning}, {"theLoggerIsNeverCalledOnceCloseReturns", theLoggerIsNeverCalledOnceCloseReturns}, + {"aFailedDeliveryIsLoggedOnTheClientsOwnThread", aFailedDeliveryIsLoggedOnTheClientsOwnThread}, + {"aFullQueueIsLoggedOnTheThreadThatCalledReport", aFullQueueIsLoggedOnTheThreadThatCalledReport}, {"missingCurlIsAnUnreachableServer", missingCurlIsAnUnreachableServer}, {"disabledClientSendsNothing", disabledClientSendsNothing}, {"disabledReasonIsEmptyWhenTheClientReports", disabledReasonIsEmptyWhenTheClientReports}, From 191050f2b7d7553aa129d78c6c2287ff8e4ce4f0 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Tue, 3 Nov 2026 21:40:25 -0700 Subject: [PATCH 2/2] Capture by reference in the flood thread's lambda clang rejects the explicit capture of a constant under -Werror (-Wunused-lambda-capture); a default capture builds on every compiler. Co-Authored-By: Claude Opus 5.5 --- test/test_trace_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index e937d7b..2a54d95 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -493,7 +493,7 @@ static void aFullQueueIsLoggedOnTheThreadThatCalledReport() { Log log; trace_client::TraceClient client(server.baseUrl(), "MyGame", "1.2.3", "k", true, log.logger()); const std::size_t flood = trace_client::QUEUE_CAPACITY * 3; - std::thread flooder([&client, flood] { + std::thread flooder([&] { for (std::size_t i = 0; i < flood; ++i) client.report("flood"); }); std::thread::id reporter = flooder.get_id();