Skip to content

Cancelling ExpectMsgAsync (Xunit) throws excessively nested exception type #7743

@quixoticaxis

Description

@quixoticaxis

Version Information
Akka.TestKit.Xunit: 1.5.46

Describe the bug
When async message expectation operation is cancelled, the exception has one more level of nesting than expected.

To Reproduce

  1. Create a test class:
using System;
using System.Threading;
using System.Threading.Tasks;

using Akka.TestKit.Xunit;

using Shouldly;

using Xunit;

namespace Actors.Routers.Tests;

[Trait("Category", "Example")]
public class ExampleTests : TestKit
{
    [Fact]
    public async Task SynchronousExample()
    {
        var probe = CreateTestProbe();

        using var stopper = new CancellationTokenSource();
        stopper.Cancel();

        var task = probe.ExpectMsgAsync<int>(
            cancellationToken: stopper.Token).AsTask();

        await Task.Delay(TimeSpan.FromSeconds(1)); // default timeout is 3 seconds

        var inner = task.Exception.ShouldBeOfType<AggregateException>();
        var superInner = inner.InnerException.ShouldBeOfType<AggregateException>();
        superInner.InnerException.ShouldBeOfType<TaskCanceledException>();
    }

    [Fact]
    public async Task AsynchronousExample()
    {
        var probe = CreateTestProbe();

        using var stopper = new CancellationTokenSource();
        stopper.Cancel();

        try
        {
            await probe.ExpectMsgAsync<int>(
                cancellationToken: stopper.Token);
        }
        catch (Exception caught)
        {
            var superInner = caught.ShouldBeOfType<AggregateException>();
            superInner.InnerException.ShouldBeOfType<TaskCanceledException>();
        }
    }
}
  1. Run the tests.

Expected behavior
I expected the tests to fail. I expected the operation to complete with some OperationCanceledException wrapped in AggregateException as with the most of .Net APIs.

Actual behavior
The tests pass. For some reason the method completes with TaskCanceledException wrapped in AggregateException wrapped in yet another AggregateException.

Environment
On Windows, dotnet 9.0.303, but I believe it does not matter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions