Skip to content

test: make the mid-flight cancellation test cover every line deterministically - #383

Merged
Chris-Wolfgang merged 1 commit into
mainfrom
test/deterministic-cancel-coverage
Sep 27, 2026
Merged

Chris-Wolfgang merged 1 commit into
mainfrom
test/deterministic-cancel-coverage

Conversation

@Chris-Wolfgang

Copy link
Copy Markdown
Owner

Summary

The first PR run on the new 100% test-assembly gate (#379) was run 36285791752 on #381. It passed Stage 1 (Linux) and Stage 2 (Windows) but failed Stage 3 (macOS ARM64): Wolfgang.TryPattern.Tests.Unit came out at 99%, with RunAsyncActionTests at 97.9%.

Cause: a race in RunAsync_Action_CancellationToken_when_cancellation_is_requested_after_action_started_the_action_is_cancelled. The action signalled actionStarted before entering its loop, and the test cancels as soon as it sees that signal. On a fast runner the cancel lands before the first ThrowIfCancellationRequested(), the action throws immediately, and Task.Delay(10).Wait(); never runs. That's one line of about 48, exactly the 97.9%. So coverage depended on thread timing, and the gate turns that into a flaky failure on every PR.

Fix: the action now signals after a full pass of the loop body (delay → signal → check), so every line runs before the test can cancel. The test still asserts the same thing: the call throws OperationCanceledException, and the action observed the cancellation.

Verification

  • dotnet build -c Release -p:TreatWarningsAsErrors=true: 0 errors.
  • The test ran 50 times on net10.0: 50 passed, 0 failed.
  • Coverage on macOS's framework set (net6.0–net10.0, no net5.0 because the ARM64 runner skips it): Wolfgang.TryPattern.Tests.Unit 100%, no class under 100%.
  • I searched tests/ for the same signal-then-loop pattern and this was the only instance.

Until this merges, Stage 3 will fail intermittently on every PR (it depends on timing). Merge this first.

Refs #310

🤖 Generated with Claude Code

…istically

RunAsync_Action_CancellationToken_when_cancellation_is_requested_after_
action_started signalled "started" before the loop, so a fast runner could
cancel before the first `Task.Delay(10).Wait()`, leaving that line
uncovered on some runs. With the 100% test-assembly gate (#379) that made
Stage 3 (macOS ARM64) fail at 97.9% on PR #381's run 36285791752 while
Linux and Windows passed. The action now signals after a full pass of the
loop body, so every line executes before the test cancels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Chris-Wolfgang
Chris-Wolfgang merged commit 042cd43 into main Sep 27, 2026
24 checks passed
@Chris-Wolfgang
Chris-Wolfgang deleted the test/deterministic-cancel-coverage branch September 27, 2026 13:01
Chris-Wolfgang added a commit that referenced this pull request Sep 27, 2026
…dent

#383's reorder only narrowed the race: on macOS ARM64 the cancel could
still land between TrySetResult and ThrowIfCancellationRequested, so the
`while (true)` loop-back never ran and RunAsyncActionTests measured 98.9%
(PR #391, run 36322172822, Stage 3). The action is now straight-line:
signal started, block on the token's WaitHandle (10 s timeout), record
whether cancellation was seen, then an unconditional `throw` of
OperationCanceledException(token). No loop, and no reachable normal exit
for the compiler to emit a closing-brace sequence point for.

The test still proves the action was running, observed the cancellation
(WaitOne returns true only once the token is cancelled, now asserted),
and that RunAsync propagated the OperationCanceledException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Chris-Wolfgang added a commit that referenced this pull request Sep 27, 2026
…tressTests (#391)

* test(concurrency): clear the seven InspectCode alerts in ConcurrencyStressTests

- AccessToModifiedClosure: the invocation counters are StrongBox<int>, so
  the lambdas capture a reference that never changes; the count is only
  touched through Interlocked / Volatile on .Value.
- AsyncFixer06: Record.ExceptionAsync(async () => await call) instead of
  an implicit Task<Result> -> Task lambda conversion.
- xUnit1031: no blocking call.Result; the completed call is awaited once
  (only when it did not throw) and the assertion reads that value.

Same invariants; both race outcomes still observed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: make the mid-flight cancellation test's coverage timing-independent

#383's reorder only narrowed the race: on macOS ARM64 the cancel could
still land between TrySetResult and ThrowIfCancellationRequested, so the
`while (true)` loop-back never ran and RunAsyncActionTests measured 98.9%
(PR #391, run 36322172822, Stage 3). The action is now straight-line:
signal started, block on the token's WaitHandle (10 s timeout), record
whether cancellation was seen, then an unconditional `throw` of
OperationCanceledException(token). No loop, and no reachable normal exit
for the compiler to emit a closing-brace sequence point for.

The test still proves the action was running, observed the cancellation
(WaitOne returns true only once the token is cancelled, now asserted),
and that RunAsync propagated the OperationCanceledException.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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