-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add ReceiveAsync feature to Akka.TestKit TestActorRef #6281
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ExceptionHandling.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using static FluentAssertions.FluentActions; | ||
|
||
namespace Akka.TestKit.Tests.TestActorRefTests | ||
{ | ||
public class ExceptionHandling: TestKit.Xunit2.TestKit | ||
{ | ||
private class GiveError | ||
{ } | ||
|
||
private class GiveErrorAsync | ||
{ } | ||
|
||
private class ExceptionActor : ReceiveActor | ||
{ | ||
public ExceptionActor() | ||
{ | ||
Receive<GiveError>((b) => throw new Exception("WAT")); | ||
|
||
ReceiveAsync<GiveErrorAsync>(async (b) => | ||
{ | ||
await Task.Delay(TimeSpan.FromSeconds(0.1)); | ||
throw new Exception("WATASYNC"); | ||
}); | ||
} | ||
} | ||
|
||
public ExceptionHandling(ITestOutputHelper helper) : base("akka.loglevel = debug", helper) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void GetException() | ||
{ | ||
var props = Props.Create<ExceptionActor>(); | ||
var subject = new TestActorRef<ExceptionActor>(Sys, props, null, "testA"); | ||
Invoking(() => subject.Receive(new GiveError())) | ||
.Should().Throw<Exception>().WithMessage("WAT"); | ||
} | ||
|
||
[Fact] | ||
public async Task GetExceptionAsync() | ||
{ | ||
var props = Props.Create<ExceptionActor>(); | ||
var subject = new TestActorRef<ExceptionActor>(Sys, props, null, "testB"); | ||
await Awaiting(() => subject.ReceiveAsync(new GiveErrorAsync())) | ||
.Should().ThrowAsync<Exception>().WithMessage("WATASYNC"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ public static void RunTask(Func<Task> asyncAction) | |
//suspend the mailbox | ||
dispatcher.Suspend(context); | ||
|
||
ActorTaskScheduler actorScheduler = context.TaskScheduler; | ||
var actorScheduler = context.TaskScheduler; | ||
actorScheduler.CurrentMessage = context.CurrentMessage; | ||
|
||
actorScheduler.OnBeforeTaskStarted(); | ||
|
@@ -158,18 +158,21 @@ public static void RunTask(Func<Task> asyncAction) | |
.Unwrap() | ||
.ContinueWith(parent => | ||
{ | ||
Exception exception = GetTaskException(parent); | ||
|
||
var exception = GetTaskException(parent); | ||
if (exception == null) | ||
{ | ||
dispatcher.Resume(context); | ||
|
||
context.CheckReceiveTimeout(); | ||
} | ||
else | ||
{ | ||
context.Self.AsInstanceOf<IInternalActorRef>().SendSystemMessage(new ActorTaskSchedulerMessage(exception, actorScheduler.CurrentMessage)); | ||
} | ||
|
||
// Used by TestActorRef to intercept async execution result | ||
if(actorScheduler is IAsyncResultInterceptor interceptor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer not to do this on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, other alternatives would require a more radical change to implement, this is the shortest solution I can find without scrambling codes all over the place |
||
interceptor.OnTaskCompleted(actorScheduler.CurrentMessage, exception); | ||
|
||
//clear the current message field of the scheduler | ||
actorScheduler.CurrentMessage = null; | ||
actorScheduler.OnAfterTaskCompleted(); | ||
|
@@ -203,3 +206,7 @@ private static Exception TryUnwrapAggregateException(AggregateException aggregat | |
} | ||
} | ||
|
||
internal interface IAsyncResultInterceptor | ||
{ | ||
void OnTaskCompleted(object message, Exception exception); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't this require API approvals @Arkatufus ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like it, at least not locally