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..f61bdba 100644 --- a/tests/TraceClient.Tests/TraceClientTest.cs +++ b/tests/TraceClient.Tests/TraceClientTest.cs @@ -1377,6 +1377,65 @@ 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. + // 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)) + { + 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.Message)); + Assert.Equal(3, log.Count); + 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 " }) + { + 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() {