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

Port Akka.Tests.Actor tests to async/await - PipeToSupportSpec #5782

Merged
merged 4 commits into from
Mar 29, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/core/Akka.Tests/Actor/PatternSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
using Akka.Actor;
using Akka.Event;
using Akka.TestKit;
using Akka.Tests.Util;
using Xunit;

namespace Akka.Tests.Actor
{
public class PatternSpec : AkkaSpec
{
[Fact]
public void GracefulStop_must_provide_Task_for_stopping_an_actor()
public async Task GracefulStop_must_provide_Task_for_stopping_an_actor()
{
//arrange
var target = Sys.ActorOf<TargetActor>();

//act
var result = target.GracefulStop(TimeSpan.FromSeconds(5));
result.Wait(TimeSpan.FromSeconds(6));
var result = await target.GracefulStop(TimeSpan.FromSeconds(5))
.AwaitWithTimeout(TimeSpan.FromSeconds(6));;

//assert
Assert.True(result.Result);
Assert.True(result);

}

Expand All @@ -46,7 +47,7 @@ public async Task GracefulStop_must_complete_Task_when_actor_already_terminated(
}

[Fact]
public void GracefulStop_must_complete_Task_with_TaskCanceledException_when_actor_not_terminated_within_timeout()
public async Task GracefulStop_must_complete_Task_with_TaskCanceledException_when_actor_not_terminated_within_timeout()
{
//arrange
var target = Sys.ActorOf<TargetActor>();
Expand All @@ -56,11 +57,9 @@ public void GracefulStop_must_complete_Task_with_TaskCanceledException_when_acto
target.Tell((latch, TimeSpan.FromSeconds(2)));

//assert
XAssert.Throws<TaskCanceledException>(() =>
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
var task = target.GracefulStop(TimeSpan.FromMilliseconds(500));
task.Wait();
var result = task.Result;
await target.GracefulStop(TimeSpan.FromMilliseconds(50));
});
latch.Open();

Expand All @@ -78,7 +77,7 @@ public async Task GracefulStop_must_not_send_unnecessary_Deadletter_bug_2157()

//assert
Assert.True(stopped);
ExpectNoMsg(TimeSpan.Zero);
await ExpectNoMsgAsync(TimeSpan.Zero);
}

#region Actors
Expand Down
28 changes: 14 additions & 14 deletions src/core/Akka.Tests/Actor/PipeToSupportSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,58 @@ public PipeToSupportSpec()
}

[Fact]
public void Should_immediately_PipeTo_completed_Task()
public async Task Should_immediately_PipeTo_completed_Task()
{
var task = Task.FromResult("foo");
task.PipeTo(TestActor);
ExpectMsg("foo");
await ExpectMsgAsync("foo");
}

[Fact]
public void Should_by_default_send_task_result_as_message()
public async Task Should_by_default_send_task_result_as_message()
{
_task.PipeTo(TestActor);
_taskCompletionSource.SetResult("Hello");
ExpectMsg("Hello");
await ExpectMsgAsync("Hello");
}

[Fact]
public void Should_by_default_not_send_a_success_message_if_the_task_does_not_produce_a_result()
public async Task Should_by_default_not_send_a_success_message_if_the_task_does_not_produce_a_result()
{
_taskWithoutResult.PipeTo(TestActor);
_taskCompletionSource.SetResult("Hello");
ExpectNoMsg(TimeSpan.FromMilliseconds(100));
await ExpectNoMsgAsync(TimeSpan.FromMilliseconds(100));
}

[Fact]
public void Should_by_default_send_task_exception_as_status_failure_message()
public async Task Should_by_default_send_task_exception_as_status_failure_message()
{
_task.PipeTo(TestActor);
_taskWithoutResult.PipeTo(TestActor);
_taskCompletionSource.SetException(new Exception("Boom"));
ExpectMsg<Status.Failure>(x => x.Cause.Message == "Boom");
ExpectMsg<Status.Failure>(x => x.Cause.Message == "Boom");
await ExpectMsgAsync<Status.Failure>(x => x.Cause.Message == "Boom");
await ExpectMsgAsync<Status.Failure>(x => x.Cause.Message == "Boom");
}

[Fact]
public void Should_use_success_handling_to_transform_task_result()
public async Task Should_use_success_handling_to_transform_task_result()
{
_task.PipeTo(TestActor, success: x => "Hello " + x);
_taskWithoutResult.PipeTo(TestActor, success: () => "Hello");
_taskCompletionSource.SetResult("World");
var pipeTo = ReceiveN(2).Cast<string>().ToList();
var pipeTo = await ReceiveNAsync(2, default).Cast<string>().ToListAsync();
pipeTo.Should().Contain("Hello");
pipeTo.Should().Contain("Hello World");
}

[Fact]
public void Should_use_failure_handling_to_transform_task_exception()
public async Task Should_use_failure_handling_to_transform_task_exception()
{
_task.PipeTo(TestActor, failure: e => "Such a " + e.Message);
_taskWithoutResult.PipeTo(TestActor, failure: e => "Such a " + e.Message);
_taskCompletionSource.SetException(new Exception("failure..."));
ExpectMsg("Such a failure...");
ExpectMsg("Such a failure...");
await ExpectMsgAsync("Such a failure...");
await ExpectMsgAsync("Such a failure...");
}
}
}