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 - FSMTransitionSpec #5776

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Changes from 3 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
61 changes: 31 additions & 30 deletions src/core/Akka.Tests/Actor/FSMTransitionSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using FluentAssertions;
Expand All @@ -18,100 +19,100 @@ namespace Akka.Tests.Actor
public class FSMTransitionSpec : AkkaSpec
{
[Fact]
public void FSMTransitionNotifier_must_not_trigger_onTransition_for_stay()
public async Task FSMTransitionNotifier_must_not_trigger_onTransition_for_stay()
{
var fsm = Sys.ActorOf(Props.Create(() => new SendAnyTransitionFSM(TestActor)));
ExpectMsg((0, 0)); // caused by initialize(), OK.
await ExpectMsgAsync((0, 0)); // caused by initialize(), OK.
fsm.Tell("stay"); // no transition event
ExpectNoMsg(500.Milliseconds());
await ExpectNoMsgAsync(500.Milliseconds());
fsm.Tell("goto"); // goto(current state)
ExpectMsg((0, 0));
await ExpectMsgAsync((0, 0));
}

[Fact]
public void FSMTransitionNotifier_must_notify_listeners()
public async Task FSMTransitionNotifier_must_notify_listeners()
{
var fsm = Sys.ActorOf(Props.Create(() => new MyFSM(TestActor)));

Within(1.Seconds(), () =>
await WithinAsync(1.Seconds(), async() =>
{
fsm.Tell(new SubscribeTransitionCallBack(TestActor));
ExpectMsg(new CurrentState<int>(fsm, 0));
await ExpectMsgAsync(new CurrentState<int>(fsm, 0));
fsm.Tell("tick");
ExpectMsg(new Transition<int>(fsm, 0, 1));
await ExpectMsgAsync(new Transition<int>(fsm, 0, 1));
fsm.Tell("tick");
ExpectMsg(new Transition<int>(fsm, 1, 0));
await ExpectMsgAsync(new Transition<int>(fsm, 1, 0));
});
}

[Fact]
public void FSMTransitionNotifier_must_not_fail_when_listener_goes_away()
public async Task FSMTransitionNotifier_must_not_fail_when_listener_goes_away()
{
var forward = Sys.ActorOf(Props.Create(() => new Forwarder(TestActor)));
var fsm = Sys.ActorOf(Props.Create(() => new MyFSM(TestActor)));

Within(1.Seconds(), () =>
await WithinAsync(1.Seconds(), async() =>
{
fsm.Tell(new SubscribeTransitionCallBack(forward));
ExpectMsg(new CurrentState<int>(fsm, 0));
forward.GracefulStop(5.Seconds()).Wait();
await ExpectMsgAsync(new CurrentState<int>(fsm, 0));
await forward.GracefulStop(5.Seconds());
fsm.Tell("tick");
ExpectNoMsg(200.Milliseconds());
await ExpectNoMsgAsync(200.Milliseconds());
});
}

[Fact]
public void FSM_must_make_previous_and_next_state_data_available_in_OnTransition()
public async Task FSM_must_make_previous_and_next_state_data_available_in_OnTransition()
{
var fsm = Sys.ActorOf(Props.Create(() => new OtherFSM(TestActor)));

Within(1.Seconds(), () =>
await WithinAsync(1.Seconds(), async() =>
{
fsm.Tell("tick");
ExpectMsg((0, 1));
await ExpectMsgAsync((0, 1));
});
}

[Fact]
public void FSM_must_trigger_transition_event_when_goto_the_same_state()
public async Task FSM_must_trigger_transition_event_when_goto_the_same_state()
{
var forward = Sys.ActorOf(Props.Create(() => new Forwarder(TestActor)));
var fsm = Sys.ActorOf(Props.Create(() => new OtherFSM(TestActor)));

Within(1.Seconds(), () =>
await WithinAsync(1.Seconds(), async() =>
{
fsm.Tell(new SubscribeTransitionCallBack(forward));
ExpectMsg(new CurrentState<int>(fsm, 0));
await ExpectMsgAsync(new CurrentState<int>(fsm, 0));
fsm.Tell("tick");
ExpectMsg((0, 1));
ExpectMsg(new Transition<int>(fsm, 0, 1));
await ExpectMsgAsync((0, 1));
await ExpectMsgAsync(new Transition<int>(fsm, 0, 1));
fsm.Tell("tick");
ExpectMsg((1, 1));
ExpectMsg(new Transition<int>(fsm, 1, 1));
await ExpectMsgAsync((1, 1));
await ExpectMsgAsync(new Transition<int>(fsm, 1, 1));
});
}

[Fact]
public void FSM_must_not_trigger_transition_event_on_stay()
public async Task FSM_must_not_trigger_transition_event_on_stay()
{
var forward = Sys.ActorOf(Props.Create(() => new Forwarder(TestActor)));
var fsm = Sys.ActorOf(Props.Create(() => new OtherFSM(TestActor)));

fsm.Tell(new SubscribeTransitionCallBack(forward));
ExpectMsg(new CurrentState<int>(fsm, 0));
await ExpectMsgAsync(new CurrentState<int>(fsm, 0));
fsm.Tell("stay");
ExpectNoMsg(500.Milliseconds());
await ExpectNoMsgAsync(500.Milliseconds());
}

[Fact]
public void FSM_must_not_leak_memory_in_nextState()
public async Task FSM_must_not_leak_memory_in_nextState()
{
var fsmref = Sys.ActorOf<LeakyFSM>();

fsmref.Tell("switch");
ExpectMsg((0, 1));
await ExpectMsgAsync((0, 1));
fsmref.Tell("test");
ExpectMsg("ok");
await ExpectMsgAsync("ok");
}

#region Test actors
Expand Down