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
10 changes: 8 additions & 2 deletions Src/FluentAssertions/Specialized/AsyncFunctionAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ private protected async Task<bool> CompletesWithinTimeoutAsync(Task target, Time
{
using var delayCancellationTokenSource = new CancellationTokenSource();

Task completedTask =
await Task.WhenAny(target, Clock.DelayAsync(remainingTime, delayCancellationTokenSource.Token));
Task delayTask = Clock.DelayAsync(remainingTime, delayCancellationTokenSource.Token);
Task completedTask = await Task.WhenAny(target, delayTask);

if (completedTask.IsFaulted)
{
Expand All @@ -303,6 +303,12 @@ private protected async Task<bool> CompletesWithinTimeoutAsync(Task target, Time
return false;
}

if (target.IsCanceled)
{
// Rethrow the exception causing the task be canceled.
await target;
}

// The monitored task is completed, we shall cancel the clock.
delayCancellationTokenSource.Cancel();
return true;
Expand Down
46 changes: 46 additions & 0 deletions Tests/FluentAssertions.Specs/Specialized/TaskAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,52 @@ await action.Should().ThrowAsync<XunitException>().WithMessage(
"Expected a <System.InvalidOperationException> to be thrown within 1s,"
+ " but found <System.NotSupportedException>:*foo*");
}

[Fact]
public async Task When_task_is_canceled_before_timeout_it_succeeds()
{
// Arrange
var timer = new FakeClock();
var taskFactory = new TaskCompletionSource<bool>();

// Act
Func<Task> action = () =>
{
return Awaiting(() => (Task)taskFactory.Task)
.Should(timer).ThrowWithinAsync<TaskCanceledException>(1.Seconds());
};

_ = action.Invoke();

taskFactory.SetCanceled();
timer.Complete();

// Assert
await action.Should().NotThrowAsync<XunitException>();
}

[Fact]
public async Task When_task_is_canceled_after_timeout_it_fails()
{
// Arrange
var timer = new FakeClock();
var taskFactory = new TaskCompletionSource<bool>();

// Act
Func<Task> action = () =>
{
return Awaiting(() => (Task)taskFactory.Task)
.Should(timer).ThrowWithinAsync<TaskCanceledException>(1.Seconds());
};

_ = action.Invoke();

timer.Delay(1.Seconds());
taskFactory.SetCanceled();

// Assert
await action.Should().ThrowAsync<XunitException>();
}
}

public class ThrowExactlyAsync
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ sidebar:
* Fixed incorrect treatment of "\\r\\n" as new line - [#2569](https://github.com/fluentassertions/fluentassertions/pull/2569)
* Ensured that nested calls to `AssertionScope(context)` create a chained context - [#2607](https://github.com/fluentassertions/fluentassertions/pull/2607)
* One overload of the `AssertionScope` constructor would not create an actual scope associated with the thread - [#2607](https://github.com/fluentassertions/fluentassertions/pull/2607)
* Fixed `ThrowWithinAsync` not respecting `OperationCanceledException` - [#2614](https://github.com/fluentassertions/fluentassertions/pull/2614)

### Breaking Changes (for users)
* Moved support for `DataSet`, `DataTable`, `DataRow` and `DataColumn` into a new package `FluentAssertions.DataSet` - [#2267](https://github.com/fluentassertions/fluentassertions/pull/2267)
Expand Down