Skip to content

Test which thread the log callback runs on - #39

Merged
dmccoystephenson merged 2 commits into
mainfrom
feature/test-log-callback-threads
Nov 16, 2026
Merged

dmccoystephenson merged 2 commits into
mainfrom
feature/test-log-callback-threads

Conversation

@dmccoystephenson

Copy link
Copy Markdown
Member

Summary

Stage B (unit-test expansion) cycle. The README's paragraph on the log callback ("runs on whichever thread hit the problem: the caller's for a report dropped in Report and for an installation ID file that fails in the constructor, and the client's background thread for a failed delivery…") is a promise Unity programs rely on, but no test checked it. Two characterization tests are added; production code is unchanged.

  • Log_RunsOnTheCallersThreadForADropInReportAndOnTheClientsThreadForADelivery (TraceClientTest.cs): records the thread each message is logged on. An overlong name dropped in Report is logged on the calling thread. A 401 answer and an unreachable server are logged on a background, non-pool thread named trace-client/MyGame (the client's own sending thread), never on the caller's.
  • Client_LogsAnIdFileFailureOnTheThreadThatConstructsIt (InstallIdTest.cs): an unreadable installation ID file is logged on the thread running the constructor.

The README's fourth case, the close timed out count, is not re-covered here. It needs a server that hangs past the 5 s timeout, and the existing Close_LogsHowManyReportsItGaveUpOn already waits that out, so adding another 5 s test only to check its thread was judged not worth the CI time. That log line comes from the catch in Drain, which runs on the same thread as the deliveries checked above.

A documentation sweep (Stage A) was also performed against TraceClient.cs, both csproj files and build.yml. No drift was found in the README tables, the wire-format section, the "Building" section or the XML docs.

Test plan

  • Build workflow green on ubuntu-latest (net8.0)
  • Build workflow green on windows-latest (net8.0 + net48)
  • No production code changed; only tests/ files touched

dotnet could not be run in the dispatch sandbox, so local verification is UNVERIFIED. The CI matrix on the PR head is the anchor.

Deferred issues

No tracking issue: the gap was found during triage.

This PR description was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

🤖 Generated with Claude Code


drafted by Claude on behalf of Daniel Stephenson

dmccoystephenson and others added 2 commits November 16, 2026 02:12
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@dmccoystephenson

Copy link
Copy Markdown
Member Author

Self-review rubric (head 5835e41):

  • Scope: PASS. Only tests/TraceClient.Tests/TraceClientTest.cs (+59) and tests/TraceClient.Tests/InstallIdTest.cs (+16) change, both covering the README paragraph on the log callback's thread.
  • Tests-new: PASS (n/a). No new public member; the PR adds tests only.
  • Tests-fix: PASS (n/a). This is not a bug fix. The tests are characterization tests that pin current behaviour, and no production code changes.
  • Sibling structure: PASS. The tests use the established Subject_Behaviour naming, the StubServer(status: () => 401) / FreePort() patterns from Log_NeverContainsTheKeyOnAnyPathThatLogs, and the Directory-in-the-way setup from Client_WithAnUnreadableFileLogsWhyAndLeavesItAlone.
  • Sibling renames: PASS (n/a). Nothing is renamed.
  • Docs: PASS. The tests assert what README.md ("The log callback runs on whichever thread hit the problem…") already says; no doc changes are needed.
  • Issue resolution: PASS (n/a). There is no Closes; the gap was found during triage.
  • CI: PASS. Run 43837927511 on 5835e41: 127/127 on ubuntu net8.0, 127/127 on windows net8.0 and 127/127 on net48. The first push (9e1d846) failed because the test read Thread.IsBackground on the sending thread after it had exited (ThreadStateException). That was a test bug, and it is fixed by snapshotting the thread inside the callback.
  • No dependencies / single file / vendoring floor / environment seam / version agreement / opt-out contract: PASS. src/ is untouched (git ls-files src/TraceClient still lists exactly TraceClient.cs and TraceClient.csproj).
  • Never throws / non-blocking & bounded: PASS (n/a). No production code paths change.

Judgment calls flagged:

  • tests/TraceClient.Tests/TraceClientTest.cs (the new Assert.Equal("trace-client/MyGame", delivery.ThreadName)): this pins the sending thread's name, which no doc promises. It is kept because it is the only assertion that ties the message to the client's own thread rather than just "some non-pool background thread". A future rename of the thread would have to update this line.
  • tests/TraceClient.Tests/TraceClientTest.cs (private sealed class LoggedOn): a nested helper class sits among the test methods, next to the one test that uses it. No sibling precedent exists in this file; the alternative was a tuple, which is less readable on the net48 leg.
  • The README's fourth case, the close timed out count on the background thread, is deliberately not re-covered, to avoid another 5 s test. It is logged from the same Drain thread whose deliveries are checked here.

Local dotnet was unavailable in the dispatch sandbox (UNVERIFIED locally). The CI matrix on the exact head SHA is the anchor and is green.

This review comment was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

@dmccoystephenson
dmccoystephenson merged commit 6bdb841 into main Nov 16, 2026
2 checks passed
@dmccoystephenson
dmccoystephenson deleted the feature/test-log-callback-threads branch November 16, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant