Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/TraceClient.Tests/InstallIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Thread>();

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()
{
Expand Down
59 changes: 59 additions & 0 deletions tests/TraceClient.Tests/TraceClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoggedOn>();
Action<string> 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()
{
Expand Down
Loading