Skip to content
Merged
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
75 changes: 42 additions & 33 deletions Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ namespace Plugin.BaseTypeExtensions.Tests;

public class TaskExtensionsTests
{
/// <summary>
/// Upper bound for waits that only run out when a test is failing (a hang), never in normal runs.
/// </summary>
private static readonly TimeSpan SafetyTimeout = TimeSpan.FromSeconds(10);

[Fact]
public void IsDone_ReflectsTaskStatus()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
Loading