From 62929ed01be6ea12e82681eddb277256e83b38f0 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Thu, 12 Nov 2026 21:02:31 -0700 Subject: [PATCH] Test that close() leaves every reason as it was and sends nothing more The README promises disabledReason() is unchanged by close(), but only a client that was reporting had that asserted, and the report made after close() in closeIsPromptAndIdempotent was never checked against what the server received. Assert both, for each disabled reason too. Co-Authored-By: Claude Opus 5.5 --- test/test_trace_client.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 8e79fe8..6707130 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -1267,6 +1267,38 @@ static void closeIsPromptAndIdempotent() { client.report("after-close"); // a no-op, not a crash } +static void closeLeavesTheReasonAsItWasAndSendsNothingMore() { + StubServer server; + trace_client::TraceClient reporting(server.baseUrl(), "MyGame", "1.2.3", "k"); + reporting.report("startup"); + reporting.close(); + CHECK_EQ(1u, server.received().size()); + reporting.report("after-close"); + reporting.report("after-close", 1.0, {{"level", "forest"}}); + reporting.close(); + CHECK(!reporting.isEnabled()); + CHECK(reporting.disabledReason().empty()); + + // A disabled client keeps the reason it was given, whichever it was. + trace_client::TraceClient off(server.baseUrl(), "MyGame", "1.2.3", "k", false); + trace_client::TraceClient keyless(server.baseUrl(), "MyGame", "1.2.3", " "); + trace_client::TraceClient unnamed(server.baseUrl(), " ", "1.2.3", "k"); + trace_client::TraceClient nothing; + setEnv("DO_NOT_TRACK", "1"); + trace_client::TraceClient environment(server.baseUrl(), "MyGame", "1.2.3", "k"); + cleanEnvironment(); // close() must not look again + trace_client::TraceClient *disabled[] = {&off, &keyless, &unnamed, ¬hing, &environment}; + const char *reasons[] = {"config", "no key", "unavailable", "config", "environment"}; + for (std::size_t i = 0; i < sizeof reasons / sizeof reasons[0]; ++i) { + disabled[i]->close(); + disabled[i]->report("after-close"); + CHECK(!disabled[i]->isEnabled()); + CHECK_EQ(std::string(reasons[i]), disabled[i]->disabledReason()); + } + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + CHECK_EQ(1u, server.received().size()); +} + static void closeWithABoundTooLongForTheClockSendsEverythingQueued() { // Infinity, or a bound whose deadline the clock cannot hold, means "as // long as it takes" -- not a deadline that wrapped round into the past. @@ -1857,6 +1889,7 @@ int main() { {"everyAllocationTheConstructorMakesMayFailWithoutAborting", everyAllocationTheConstructorMakesMayFailWithoutAborting}, {"closeReturnsWithinTheTimeoutWhenTheServerHangs", closeReturnsWithinTheTimeoutWhenTheServerHangs}, {"closeIsPromptAndIdempotent", closeIsPromptAndIdempotent}, + {"closeLeavesTheReasonAsItWasAndSendsNothingMore", closeLeavesTheReasonAsItWasAndSendsNothingMore}, {"closeWithABoundTooLongForTheClockSendsEverythingQueued", closeWithABoundTooLongForTheClockSendsEverythingQueued}, {"closeWithANegativeOrNanBoundWaitsForNothing", closeWithANegativeOrNanBoundWaitsForNothing}, {"reportTagsEveryEventWithTheProgramVersionTrimmed", reportTagsEveryEventWithTheProgramVersionTrimmed},