Skip to content
Merged
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ public async Task WaitAsync_Generic_WithCancellationToken_RespectsToken()
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Theory]
[InlineData(null)]
[InlineData(0)]
[InlineData(-1)]
public async Task WaitAsync_Generic_NoTimeout_RespectsToken(int? timeoutMilliseconds)
{
// Arrange
var tcs = new TaskCompletionSource<int>();
using var cts = new CancellationTokenSource();
TimeSpan? timeout = timeoutMilliseconds is { } ms ? TimeSpan.FromMilliseconds(ms) : null;

// Act
var waitTask = tcs.Task.WaitBetterAsync(timeout, cts.Token);
cts.Cancel();

// Assert: bounded, so a regression fails instead of hanging
Func<Task> act = () => waitTask.WaitAsync(TimeSpan.FromSeconds(5));
await act.Should().ThrowAsync<OperationCanceledException>();
tcs.Task.IsCompleted.Should().BeFalse("only the wait is cancelled, not the awaited task");
}

[Fact]
public async Task WaitAsync_Generic_NullTimeout_CancelableTokenNotCancelled_ReturnsResult()
{
// Arrange
var tcs = new TaskCompletionSource<int>();
using var cts = new CancellationTokenSource();

// Act
var waitTask = tcs.Task.WaitBetterAsync(null, cts.Token);
tcs.SetResult(42);

// Assert
(await waitTask).Should().Be(42);
}

#endregion

#region WaitAsync (non-generic) tests
Expand Down Expand Up @@ -185,6 +221,43 @@ public async Task WaitAsync_NonGeneric_WithCancellationToken_RespectsToken()
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Theory]
[InlineData(null)]
[InlineData(0)]
[InlineData(-1)]
public async Task WaitAsync_NonGeneric_NoTimeout_RespectsToken(int? timeoutMilliseconds)
{
// Arrange
var tcs = new TaskCompletionSource();
using var cts = new CancellationTokenSource();
TimeSpan? timeout = timeoutMilliseconds is { } ms ? TimeSpan.FromMilliseconds(ms) : null;

// Act
var waitTask = tcs.Task.WaitBetterAsync(timeout, cts.Token);
cts.Cancel();

// Assert: bounded, so a regression fails instead of hanging
Func<Task> act = () => waitTask.WaitAsync(TimeSpan.FromSeconds(5));
await act.Should().ThrowAsync<OperationCanceledException>();
tcs.Task.IsCompleted.Should().BeFalse("only the wait is cancelled, not the awaited task");
}

[Fact]
public async Task WaitAsync_NonGeneric_NullTimeout_CancelableTokenNotCancelled_Completes()
{
// Arrange
var tcs = new TaskCompletionSource();
using var cts = new CancellationTokenSource();

// Act
var waitTask = tcs.Task.WaitBetterAsync(null, cts.Token);
tcs.SetResult();

// Assert
await waitTask.WaitAsync(TimeSpan.FromSeconds(5));
waitTask.IsCompletedSuccessfully.Should().BeTrue();
}

#endregion

#region Integration and edge case tests
Expand Down
28 changes: 16 additions & 12 deletions Plugin.BaseTypeExtensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@ public static class TaskExtensions
/// </summary>
/// <typeparam name="T">The type of the task result.</typeparam>
/// <param name="task">The task to wait for.</param>
/// <param name="timeout">The optional timeout duration. If null, zero, or negative, the method returns the original task (no timeout).</param>
/// <param name="timeout">The optional timeout duration. If null, zero, or negative, no timeout is applied.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>
/// If timeout is null, zero, or negative: returns the original task.
/// Otherwise: returns a task that completes when the original task completes or when the timeout expires.
/// If timeout is null, zero, or negative: returns the original task when <paramref name="cancellationToken"/> cannot be canceled,
/// otherwise a task that completes when the original task completes or when the token is canceled.
/// Otherwise: returns a task that completes when the original task completes, when the timeout expires, or when the token is canceled.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="task"/> is null.</exception>
/// <exception cref="TimeoutException">Thrown when the task doesn't complete within the specified timeout.</exception>
/// <exception cref="OperationCanceledException">Thrown when the operation is canceled via the <paramref name="cancellationToken"/>.</exception>
/// <remarks>
/// This method provides a convenient way to add optional timeouts to any task. When no timeout is needed,
/// simply pass null and the original task is returned unchanged. This avoids the overhead of timeout monitoring
/// when it's not required.
/// simply pass null: the original task is returned unchanged unless the token can be canceled, in which case
/// the wait stays cancelable. Canceling the token only abandons the wait, the original task keeps running.
/// </remarks>
public static Task<T> WaitBetterAsync<T>(this Task<T> task, TimeSpan? timeout, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(task);
if (!timeout.HasValue || timeout.Value <= TimeSpan.Zero)
{
return task;
// No timeout, but the token must still be observed. Only skip WaitAsync when it can never fire.
return cancellationToken.CanBeCanceled ? task.WaitAsync(cancellationToken) : task;
}

// Cap very large timeouts to avoid ArgumentOutOfRangeException from Task.WaitAsync
Expand All @@ -49,26 +51,28 @@ public static Task<T> WaitBetterAsync<T>(this Task<T> task, TimeSpan? timeout, C
/// Waits for the task to complete within an optional timeout period.
/// </summary>
/// <param name="task">The task to wait for.</param>
/// <param name="timeout">The optional timeout duration. If null, zero, or negative, the method returns the original task (no timeout).</param>
/// <param name="timeout">The optional timeout duration. If null, zero, or negative, no timeout is applied.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>
/// If timeout is null, zero, or negative: returns the original task.
/// Otherwise: returns a task that completes when the original task completes or when the timeout expires.
/// If timeout is null, zero, or negative: returns the original task when <paramref name="cancellationToken"/> cannot be canceled,
/// otherwise a task that completes when the original task completes or when the token is canceled.
/// Otherwise: returns a task that completes when the original task completes, when the timeout expires, or when the token is canceled.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="task"/> is null.</exception>
/// <exception cref="TimeoutException">Thrown when the task doesn't complete within the specified timeout.</exception>
/// <exception cref="OperationCanceledException">Thrown when the operation is canceled via the <paramref name="cancellationToken"/>.</exception>
/// <remarks>
/// This method provides a convenient way to add optional timeouts to any task. When no timeout is needed,
/// simply pass null and the original task is returned unchanged. This avoids the overhead of timeout monitoring
/// when it's not required.
/// simply pass null: the original task is returned unchanged unless the token can be canceled, in which case
/// the wait stays cancelable. Canceling the token only abandons the wait, the original task keeps running.
/// </remarks>
public static Task WaitBetterAsync(this Task task, TimeSpan? timeout, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(task);
if (!timeout.HasValue || timeout.Value <= TimeSpan.Zero)
{
return task;
// No timeout, but the token must still be observed. Only skip WaitAsync when it can never fire.
return cancellationToken.CanBeCanceled ? task.WaitAsync(cancellationToken) : task;
}

// Cap very large timeouts to avoid ArgumentOutOfRangeException from Task.WaitAsync
Expand Down
Loading