From 9e1d8468c4db937f62cc8bb018a7b5d37f004c87 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Mon, 16 Nov 2026 02:12:28 -0700 Subject: [PATCH 1/2] Test which thread the log callback runs on The README tells programs which thread the log callback runs on, so a Unity program knows when it may touch a main-thread-only API, but no test checked it. Pin the current behaviour: a report dropped in Report and an installation ID file that fails in the constructor are logged on the caller's thread; a rejected or undelivered report is logged on the client's own named background thread, not the caller's and not a pool thread. Co-Authored-By: Claude Opus 5.5 --- tests/TraceClient.Tests/InstallIdTest.cs | 16 +++++++++ tests/TraceClient.Tests/TraceClientTest.cs | 38 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/tests/TraceClient.Tests/InstallIdTest.cs b/tests/TraceClient.Tests/InstallIdTest.cs index 56c0b01..07d1f03 100644 --- a/tests/TraceClient.Tests/InstallIdTest.cs +++ b/tests/TraceClient.Tests/InstallIdTest.cs @@ -219,6 +219,22 @@ public void Client_WithAnUnreadableFileLogsWhyAndLeavesItAlone() Assert.Contains("\"install\":\"" + client.InstallId + "\"", _server.Received.Single().Body); } + [Fact] + public void Client_LogsAnIdFileFailureOnTheThreadThatConstructsIt() + { + // The README: an installation ID file that fails in the constructor is + // logged on the caller's thread, not the client's background thread. + string path = Path.Combine(_directory, "install-id"); + Directory.CreateDirectory(path); + var threads = new ConcurrentQueue(); + + var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k", + log: m => threads.Enqueue(Thread.CurrentThread), installIdFile: path); + client.Close(); + + Assert.Same(Thread.CurrentThread, Assert.Single(threads)); + } + [Fact] public void Client_WhoseIdFileFailsStillConstructsWhenTheLoggerThrows() { diff --git a/tests/TraceClient.Tests/TraceClientTest.cs b/tests/TraceClient.Tests/TraceClientTest.cs index d1e7668..91467f3 100644 --- a/tests/TraceClient.Tests/TraceClientTest.cs +++ b/tests/TraceClient.Tests/TraceClientTest.cs @@ -1377,6 +1377,44 @@ public void Log_NeverContainsTheKeyOnAnyPathThatLogs() Assert.All(log, m => Assert.DoesNotContain(key, m)); } + [Fact] + public void Log_RunsOnTheCallersThreadForADropInReportAndOnTheClientsThreadForADelivery() + { + // The README tells programs which thread the callback runs on, so a + // Unity program knows when it may touch a main-thread-only API. + // Record the thread with each message: a drop decided in Report is + // logged on the caller's thread, a failed delivery on the client's + // own background thread -- not the caller's, and not a pool thread. + var log = new ConcurrentQueue>(); + Action record = m => log.Enqueue(new KeyValuePair(Thread.CurrentThread, m)); + Thread caller = Thread.CurrentThread; + + using (var rejecting = new StubServer(status: () => 401)) + { + var client = new TraceClient(rejecting.BaseUrl, "MyGame", "1.2.3", key: "k", log: record); + client.Report(new string('n', TraceClient.MaxLength + 1)); // dropped in Report + client.Report("startup"); // answered 401 + client.Close(); + } + var unreachable = new TraceClient("http://127.0.0.1:" + StubServer.FreePort(), "MyGame", "1.2.3", + key: "k", log: record); + unreachable.Report("startup"); // could not deliver + unreachable.Close(); + + string all = string.Join(" | ", log.Select(e => e.Value)); + Assert.Equal(3, log.Count); + KeyValuePair dropped = Assert.Single(log, e => e.Value.StartsWith("[trace] dropped nnn", StringComparison.Ordinal)); + Assert.Same(caller, dropped.Key); + foreach (string kind in new[] { "[trace] trace server answered 401 for ", "[trace] could not deliver " }) + { + KeyValuePair delivery = Assert.Single(log, e => e.Value.StartsWith(kind, StringComparison.Ordinal)); + Assert.NotSame(caller, delivery.Key); + Assert.True(delivery.Key.IsBackground, kind + "logged on a foreground thread; logged: " + all); + Assert.False(delivery.Key.IsThreadPoolThread, kind + "logged on a pool thread; logged: " + all); + Assert.Equal("trace-client/MyGame", delivery.Key.Name); + } + } + [Fact] public void Close_LogsNoDropWhenEverythingWasDelivered() { From 5835e41b72eaaca801c2f9d6ff5b69bf4d30b34a Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Mon, 16 Nov 2026 02:14:16 -0700 Subject: [PATCH 2/2] Describe the logging thread while the callback runs The sending thread has exited by the time the asserts run, and reading IsBackground on a dead thread throws ThreadStateException. Record the thread's ID, name and flags inside the callback instead. Co-Authored-By: Claude Opus 5.5 --- tests/TraceClient.Tests/TraceClientTest.cs | 43 ++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/tests/TraceClient.Tests/TraceClientTest.cs b/tests/TraceClient.Tests/TraceClientTest.cs index 91467f3..f61bdba 100644 --- a/tests/TraceClient.Tests/TraceClientTest.cs +++ b/tests/TraceClient.Tests/TraceClientTest.cs @@ -1385,9 +1385,12 @@ public void Log_RunsOnTheCallersThreadForADropInReportAndOnTheClientsThreadForAD // Record the thread with each message: a drop decided in Report is // logged on the caller's thread, a failed delivery on the client's // own background thread -- not the caller's, and not a pool thread. - var log = new ConcurrentQueue>(); - Action record = m => log.Enqueue(new KeyValuePair(Thread.CurrentThread, m)); - Thread caller = Thread.CurrentThread; + // The thread is described while the callback runs: the sending thread + // has exited by the time the asserts run, and a dead thread's + // IsBackground throws. + var log = new ConcurrentQueue(); + Action record = m => log.Enqueue(new LoggedOn(Thread.CurrentThread, m)); + int caller = Thread.CurrentThread.ManagedThreadId; using (var rejecting = new StubServer(status: () => 401)) { @@ -1401,20 +1404,38 @@ public void Log_RunsOnTheCallersThreadForADropInReportAndOnTheClientsThreadForAD unreachable.Report("startup"); // could not deliver unreachable.Close(); - string all = string.Join(" | ", log.Select(e => e.Value)); + string all = string.Join(" | ", log.Select(e => e.Message)); Assert.Equal(3, log.Count); - KeyValuePair dropped = Assert.Single(log, e => e.Value.StartsWith("[trace] dropped nnn", StringComparison.Ordinal)); - Assert.Same(caller, dropped.Key); + LoggedOn dropped = Assert.Single(log, e => e.Message.StartsWith("[trace] dropped nnn", StringComparison.Ordinal)); + Assert.Equal(caller, dropped.ThreadId); foreach (string kind in new[] { "[trace] trace server answered 401 for ", "[trace] could not deliver " }) { - KeyValuePair delivery = Assert.Single(log, e => e.Value.StartsWith(kind, StringComparison.Ordinal)); - Assert.NotSame(caller, delivery.Key); - Assert.True(delivery.Key.IsBackground, kind + "logged on a foreground thread; logged: " + all); - Assert.False(delivery.Key.IsThreadPoolThread, kind + "logged on a pool thread; logged: " + all); - Assert.Equal("trace-client/MyGame", delivery.Key.Name); + LoggedOn delivery = Assert.Single(log, e => e.Message.StartsWith(kind, StringComparison.Ordinal)); + Assert.NotEqual(caller, delivery.ThreadId); + Assert.True(delivery.IsBackground, kind + "logged on a foreground thread; logged: " + all); + Assert.False(delivery.IsThreadPoolThread, kind + "logged on a pool thread; logged: " + all); + Assert.Equal("trace-client/MyGame", delivery.ThreadName); } } + private sealed class LoggedOn + { + public LoggedOn(Thread thread, string message) + { + ThreadId = thread.ManagedThreadId; + ThreadName = thread.Name; + IsBackground = thread.IsBackground; + IsThreadPoolThread = thread.IsThreadPoolThread; + Message = message; + } + + public int ThreadId { get; } + public string ThreadName { get; } + public bool IsBackground { get; } + public bool IsThreadPoolThread { get; } + public string Message { get; } + } + [Fact] public void Close_LogsNoDropWhenEverythingWasDelivered() {