Skip to content

test(tasks): make the StartAndForget DoesNotBlockCaller tests deterministic - #68

Merged
framinosona merged 1 commit into
mainfrom
framinosona/fix-flaky-startandforget-tcs-test
Nov 22, 2026
Merged

framinosona merged 1 commit into
mainfrom
framinosona/fix-flaky-startandforget-tcs-test

Conversation

@framinosona

Copy link
Copy Markdown
Member

Summary

Rewrites two timing-based tests so they no longer depend on how fast the thread pool picks up work:

  • StartAndForget_WithTCS_DoesNotBlockCaller
  • StartAndForget_LongRunningTask_DoesNotBlockCaller (same defect, Action<Exception> overload)

The task now waits on a gate the test controls, the test waits for a "started" signal, and the call is bounded by a 10 s safety timeout. No library code changes.

Why

Both tests started a Task.Run, called StartAndForget, slept 50 ms and then asserted that the lambda had run (taskStarted.Should().BeTrue()), plus a < 100 ms wall-clock assertion on the call. When the thread pool is busy for more than 50 ms, which happens on a loaded runner with test classes running in parallel, the lambda has not started yet and CI fails with Expected taskStarted to be True, but found False. The failure shows up as an occasional red run that passes on re-run.

StartAndForget can only return while the task is still running if it does not wait for it, so holding the task open makes the elapsed-time assertion unnecessary.

Change Type

  • Feature
  • Bug fix
  • Refactor
  • Docs only
  • CI/build/tooling

Affected Areas

  • Tests

Behavior And Compatibility

  • No externally visible behavior change

Tests

  • Unit tests added/updated

How it was checked (net9.0 target, run on the .NET 10 runtime locally):

  • Reproduced the failure deterministically. A throwaway harness (not committed) occupied every thread-pool worker for 400 ms and then ran a copy of the old bodies: both failed with Expected taskStarted to be True, but found False. The two rewritten tests passed under the same starvation. A plain CPU-load loop (7 runs on 10 cores) did not reproduce it, so that is the only reproduction I have.
  • Still catches the regression they exist for. With StartAndForget temporarily changed to block on the task (GetAwaiter().GetResult()), both tests fail with TimeoutException after 10 s instead of hanging. The change was reverted.
  • Full suite: 476 of 476 passed, three runs in a row.

Documentation

  • Not applicable (tests only)

Notes

  • Other tests in this file still use fixed delays followed by an assertion, for example StartAndForget_WithTCS_MultipleOperations_EachCompletesIndependently (Task.Delay(200)). They have more headroom and were not touched here.
  • Merging runs ci.yml on main, which builds a package and waits on the nuget-org approval like any other push, even though only a test file changed.

Checklist

  • Commit messages follow Conventional Commits (type(scope): subject)
  • CI passes
  • Change is scoped to one logical unit of work

…nistic

StartAndForget_WithTCS_DoesNotBlockCaller and StartAndForget_LongRunningTask_DoesNotBlockCaller
asserted that a Task.Run lambda had started 50 ms after the call, and that the call returned
within 100 ms. Both are wall-clock assumptions: when the thread pool is busy for more than 50 ms
(parallel test classes on a loaded runner) the lambda has not run yet and the assertion fails with
"Expected taskStarted to be True, but found False."

The task now waits on a gate the test controls, and the test waits for a "started" signal instead
of sleeping. StartAndForget can only return while the task is still running if it does not wait for
the task, so the elapsed-time assertion is no longer needed. The call is bounded by a 10 s safety
timeout, so an implementation that blocks the caller fails the test instead of hanging the run.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 13:06
@github-actions

Copy link
Copy Markdown

Test Results

476 tests  ±0   476 ✅ ±0   2s ⏱️ ±0s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 97e326f. ± Comparison against base commit 2263c84.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain in the synchronization changes to the two nonblocking tests.

No open issues 🎉

What changed in this PR

Makes the two StartAndForget nonblocking tests less sensitive to thread-pool scheduling by replacing timing assumptions with explicit coordination. Production library behavior is unchanged.

Changes:

  • Replaces fixed delays and elapsed-time assertions with task-start signals and test-controlled completion gates.
  • Adds a shared 10-second safety timeout to bound calls and waits.
  • Releases each gate in a finally block so failures do not leave tasks waiting on it.
File Description
Plugin.BaseTypeExtensions.Tests/​TaskExtensionsTests.cs Rewrites both nonblocking tests with explicit synchronization, bounded waits, and gate cleanup.

Review effort: Balanced

@framinosona
framinosona merged commit c414357 into main Nov 22, 2026
13 checks passed
@framinosona
framinosona deleted the framinosona/fix-flaky-startandforget-tcs-test branch November 22, 2026 20:45
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.

2 participants