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

convert Failure to exception when using Ask<object> #7286

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/core/Akka.Tests/Actor/AskSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ public async Task Bugfix5204_should_allow_null_response_without_error()
resp.Should().BeNullOrEmpty();
}

/// <summary>
/// Reproduction for https://github.com/akkadotnet/akka.net/issues/7254
/// </summary>
[Fact]
public async Task Bugfix7254_should_throw_error_when_expecting_object_type()
{
const string textExceptionMessage = "THIS IS TEST";

var actor = Sys.ActorOf(act => act.ReceiveAny((_, context) =>
{
context.Sender.Tell(new Status.Failure(new Exception(textExceptionMessage)));
}));

var ex = await Assert.ThrowsAsync<Exception>(async () => await actor.Ask("answer"));
ex.Message.ShouldBe(textExceptionMessage);
}

[Fact]
public void AskDoesNotDeadlockWhenWaitForResultInGuiApplication()
{
Expand Down
4 changes: 1 addition & 3 deletions src/core/Akka.Tests/Actor/InboxSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ public void Select_WithClient_should_update_Client_and_copy_the_rest_of_the_prop
public async Task Inbox_Receive_will_timeout_gracefully_if_timeout_is_already_expired()
{
var task = _inbox.ReceiveAsync(TimeSpan.FromSeconds(-1));
Assert.True(await task.AwaitWithTimeout(TimeSpan.FromMilliseconds(1000)), "Receive did not complete in time.");
Assert.IsType<Status.Failure>(task.Result);
await Assert.ThrowsAnyAsync<Exception>(() => task.AwaitWithTimeout(TimeSpan.FromMilliseconds(1000)));
}
}
}

16 changes: 8 additions & 8 deletions src/core/Akka/Actor/ActorRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,23 @@ protected override void TellInternal(object message, IActorRef sender)
case ISystemMessage msg:
handled = _result.TrySetException(new InvalidOperationException($"system message of type '{msg.GetType().Name}' is invalid for {nameof(FutureActorRef<T>)}"));
break;
case T t:
handled = _result.TrySetResult(t);
break;
case null:
handled = _result.TrySetResult(default);
break;
case Status.Failure f:
case Status.Failure f when typeof(T) != typeof(Status.Failure):
handled = _result.TrySetException(f.Cause
?? new TaskCanceledException("Task cancelled by actor via Failure message."));
break;
#pragma warning disable CS0618
// for backwards compatibility
case Failure f:
case Failure f when !typeof(Failure).IsAssignableFrom(typeof(T)):
handled = _result.TrySetException(f.Exception
?? new TaskCanceledException("Task cancelled by actor via Failure message."));
#pragma warning restore CS0618
break;
case T t:
handled = _result.TrySetResult(t);
break;
case null:
handled = _result.TrySetResult(default);
break;
default:
_ = _result.TrySetException(new ArgumentException(
$"Received message of type [{message.GetType()}] - Ask expected message of type [{typeof(T)}]"));
Expand Down
Loading