From 97e326f82cadf804043cf8506fa5be8263f6eaf1 Mon Sep 17 00:00:00 2001 From: Francois Raminosona Date: Fri, 20 Nov 2026 14:06:30 +0100 Subject: [PATCH] test(tasks): make the StartAndForget DoesNotBlockCaller tests deterministic 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. --- .../TaskExtensionsTests.cs | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs b/Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs index 3c472d5..75ba245 100644 --- a/Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs +++ b/Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs @@ -2,6 +2,11 @@ namespace Plugin.BaseTypeExtensions.Tests; public class TaskExtensionsTests { + /// + /// Upper bound for waits that only run out when a test is failing (a hang), never in normal runs. + /// + private static readonly TimeSpan SafetyTimeout = TimeSpan.FromSeconds(10); + [Fact] public void IsDone_ReflectsTaskStatus() { @@ -411,27 +416,29 @@ public async Task StartAndForget_ExceptionInHandler_DoesNotPropagateToCallerCont [Fact] public async Task StartAndForget_LongRunningTask_DoesNotBlockCaller() { - // Arrange - var taskStarted = false; + // Arrange - the task cannot finish until the test releases it, so StartAndForget can + // only return while the task is still running if it does not wait for the task + var taskStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var task = Task.Run(async () => { - taskStarted = true; - await Task.Delay(1000); // Long running + taskStarted.SetResult(); + await release.Task; }); - var stopwatch = System.Diagnostics.Stopwatch.StartNew(); - - // Act - task.StartAndForget(ex => { }); - - stopwatch.Stop(); + try + { + // Act - an implementation that blocks on the task would never return, so bound the call + await Task.Run(() => task.StartAndForget(ex => { })).WaitAsync(SafetyTimeout); - // Assert - method should return immediately - stopwatch.ElapsedMilliseconds.Should().BeLessThan(100); - - // Wait a bit and verify task actually started - await Task.Delay(50); - taskStarted.Should().BeTrue(); + // Assert + await taskStarted.Task.WaitAsync(SafetyTimeout); + task.IsCompleted.Should().BeFalse("the task is held until the test releases it"); + } + finally + { + release.SetResult(); + } } #endregion @@ -602,29 +609,31 @@ public async Task StartAndForget_WithTCS_CancelledTask_SetsException() [Fact] public async Task StartAndForget_WithTCS_DoesNotBlockCaller() { - // Arrange - var taskStarted = false; + // Arrange - the task cannot finish until the test releases it, so StartAndForget can + // only return while the task is still running if it does not wait for the task + var taskStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var task = Task.Run(async () => { - taskStarted = true; - await Task.Delay(1000); // Long running + taskStarted.SetResult(); + await release.Task; }); var tcs = new TaskCompletionSource(); - var stopwatch = System.Diagnostics.Stopwatch.StartNew(); - - // Act - task.StartAndForget(tcs); - - stopwatch.Stop(); + try + { + // Act - an implementation that blocks on the task would never return, so bound the call + await Task.Run(() => task.StartAndForget(tcs)).WaitAsync(SafetyTimeout); - // Assert - method should return immediately - stopwatch.ElapsedMilliseconds.Should().BeLessThan(100); - - // Wait a bit and verify task actually started - await Task.Delay(50); - taskStarted.Should().BeTrue(); - tcs.Task.IsCompleted.Should().BeFalse("task is still running"); + // Assert + await taskStarted.Task.WaitAsync(SafetyTimeout); + task.IsCompleted.Should().BeFalse("the task is held until the test releases it"); + tcs.Task.IsCompleted.Should().BeFalse("task is still running"); + } + finally + { + release.SetResult(); + } } [Fact]