Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestActorRef can not catch exceptions on asynchronous methods #6265

Closed
Nican opened this issue Nov 26, 2022 · 3 comments · Fixed by #6281
Closed

TestActorRef can not catch exceptions on asynchronous methods #6265

Nican opened this issue Nov 26, 2022 · 3 comments · Fixed by #6281
Labels
akka-testkit Akka.NET Testkit issues potential bug
Milestone

Comments

@Nican
Copy link

Nican commented Nov 26, 2022

Version Information
Version of Akka.NET? Akka 1.4.46
Which Akka.NET Modules? Akka.TestKit 1.4.46, xunit 2.4.1

Describe the bug
I am attempting to test the behavior of Actors. I am not very good at it yet, so every once in a while there is an exception, and the test just swallows the exception, and I have to go search why ExpectMsg is timing out.

I have found the best way to test the actor behavior is with TestActorRef.Receive, but it only works for synchronous methods.

To Reproduce
Test actor:

public class ExceptionActor : ReceiveActor
{
    public record GiveError();
    public record GiveErrorAsync();
    public ExceptionActor()
    {
        Receive<GiveError>((b) =>
        {
            throw new Exception("WAT");
        });

        ReceiveAsync<GiveErrorAsync>(async (b) =>
        {
            await Task.Delay(TimeSpan.FromSeconds(0.1));
            throw new Exception("WATASYNC");
        });
    }
}

This test fails (Expected behavior):

    [Fact]
    public void GetException()
    {
        var props = Props.Create<ExceptionActor>();
        var subject = new TestActorRef<ExceptionActor>(Sys, props, null, "testA");
        subject.Receive(new ExceptionActor.GiveError());
    }

This test does not fail (Not expected behavior):

    [Fact]
    public void GetExceptionAsync()
    {
        var props = Props.Create<ExceptionActor>();
        var subject = new TestActorRef<ExceptionActor>(Sys, props, null, "testB");
        subject.Receive(new ExceptionActor.GiveErrorAsync());
    }

Expected behavior
To have a ReceiveAsync method for asynchronous method handling.
OR
Some mechanism that automatically watches for any unhandled exceptions in tests, and have the test failure display the information.

Actual behavior
Unhandled exceptions disappear.

Environment
.NET Core v7 on Windows

Additional context
I am fairly new to akka.net, but I am loving it. I am following the instructions on Testing Actor Systems, and it feels like I am missing something. Are there no better ways to watch that unhandled exceptions exist?

@Nican Nican changed the title TestActorRef can not catch exception on asynchronous methods TestActorRef can not catch exceptions on asynchronous methods Nov 26, 2022
@Aaronontheweb Aaronontheweb added akka-testkit Akka.NET Testkit issues potential bug labels Nov 28, 2022
@Aaronontheweb Aaronontheweb added this to the 1.4.47 milestone Nov 28, 2022
@Aaronontheweb
Copy link
Member

Possibly related: #6279

@Aaronontheweb
Copy link
Member

Actually, #6279 is definitely not related. This looks to me more like a lapse in the TestActorRef test suites itself.

@Arkatufus can you take a look at this for 1.4.47?

@Arkatufus
Copy link
Contributor

Will do, I'll give a stab at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
akka-testkit Akka.NET Testkit issues potential bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants