Visitar URL original
ThrowWithinAsync does not work with TaskCanceledException · Issue #2444 · fluentassertions/fluentassertions · GitHub
Skip to content

ThrowWithinAsync does not work with TaskCanceledException #2444

Closed

Description

@Arithmomaniac

Description

ThrowWithinAsync does not work with TaskCanceledException, but only AFAICT on async/await methods.

See the reproduction steps, which endeavor to isolate the cause.

Reproduction Steps

await Invoking(async () => { await Task.Delay(1); throw new TaskCanceledException(); })
    .Should().ThrowAsync<TaskCanceledException>();
await Invoking(async () => { await Task.Delay(1); throw new InvalidOperationException(); })
    .Should().ThrowWithinAsync<InvalidOperationException>(TimeSpan.FromSeconds(1));
await Invoking(() => Task.FromException(new TaskCanceledException()) )
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(0.5));
await Invoking(async () => { await Task.Delay(1); throw new TaskCanceledException(); })
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(1));

Expected behavior

Test passes; all four assertions work

Actual behavior

Test fails with the following exception, which indicates only the last assertion failed:

  Message: 
    Expected a <System.Threading.Tasks.TaskCanceledException> to be thrown within 1s, but no exception was thrown.

  Stack Trace: 
    XUnit2TestFramework.Throw(String message)
    TestFrameworkProvider.Throw(String message)
    DefaultAssertionStrategy.HandleFailure(String message)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(String message)
    AsyncFunctionAssertions`2.AssertThrows[TException](Exception exception, TimeSpan timeSpan, String because, Object[] becauseArgs)
    AsyncFunctionAssertions`2.ThrowWithinAsync[TException](TimeSpan timeSpan, String because, Object[] becauseArgs)
    CollectionExtensionsTests.Foo() line 29
    --- End of stack trace from previous location --

Regression?

No response

Known Workarounds

No response

Configuration

xUnit.net VSTest Adapter v2.5.0.1+5ebf84cd75 (64-bit .NET 7.0.13)
FluentAssertions 6.12.0

Other information

No response

Are you willing to help with a pull-request?

No

Activity

  1. jnyrup commented on Nov 7, 2023

    @jnyrup
    Member

    Thanks for reporting this.

    This is due to Task.WhenAny swallowing OperationCanceledException and returning a canceled exception instead of a faulted exception.

    I have some ideas how to workaround this in Fluent Assertions.

  2. ITaluone commented on Nov 7, 2023

    @ITaluone
    Contributor

    For the record: it seems that the Task.Delay(x) does not have any effect on the outcome.

    await Invoking(async () =>
    {
        await Task.Delay(3000);
        throw new TaskCanceledException();
    })
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(1));

    and

    await Invoking(async () =>
    {
        await Task.Delay(0);
        throw new TaskCanceledException();
    })
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(5));

    both fails with the same exception

  3. jnyrup commented on Nov 8, 2023

    @jnyrup
    Member

    For the record: it seems that the Task.Delay(x) does not have any effect on the outcome.

    That is only need to ensure the exception happens after a call to await, otherwise the exception is thrown before reaching

    _ = await CompletesWithinTimeoutAsync(task, remainingTime);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions