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

allow null response to Ask<T> operation #5205

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/core/Akka.Tests/Actor/AskSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using Akka.TestKit;
using Xunit;
using Akka.Actor;
using Akka.Actor.Dsl;
using System;
using System.Threading;
using System.Threading.Tasks;
using Akka.Util.Internal;
using FluentAssertions;
using Nito.AsyncEx;

namespace Akka.Tests.Actor
Expand Down Expand Up @@ -180,6 +182,22 @@ public async Task ShouldFailWhenAskExpectsWrongType()
// expect int, but in fact string
await Assert.ThrowsAsync<ArgumentException>(async () => await actor.Ask<int>("answer"));
}

/// <summary>
/// Reproduction for https://github.com/akkadotnet/akka.net/issues/5204
/// </summary>
[Fact]
public async Task Bugfix5204_should_allow_null_response_without_error()
{
var actor = Sys.ActorOf(act => act.ReceiveAny((o, context) =>
{
context.Sender.Tell(null);
}));

// expect a string, but the answer should be `null`
var resp = await actor.Ask<string>(1);
resp.Should().BeNullOrEmpty();
}

[Fact]
public void AskDoesNotDeadlockWhenWaitForResultInGuiApplication()
Expand Down
4 changes: 4 additions & 0 deletions src/core/Akka/Actor/ActorRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ protected override void TellInternal(object message, IActorRef sender)
{
_result.TrySetResult(t);
}
else if (message == null) //special case: https://github.com/akkadotnet/akka.net/issues/5204
{
_result.TrySetResult(default);
}
else if (message is Failure f)
{
_result.TrySetException(f.Exception ?? new TaskCanceledException("Task cancelled by actor via Failure message."));
Expand Down