From 365a38b3736050ade98db1b5c48d88c0610ff3d1 Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Tue, 29 Mar 2022 16:46:30 +0100 Subject: [PATCH] Port `Akka.Tests.Actor` tests to `async/await` - `SupervisorHierarchySpec` --- .../Actor/RepointableActorRefSpecs.cs | 5 +- .../Actor/SupervisorHierarchySpec.cs | 55 ++++++++++--------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/core/Akka.Tests/Actor/RepointableActorRefSpecs.cs b/src/core/Akka.Tests/Actor/RepointableActorRefSpecs.cs index 7878c6ff7c9..dab1523a7c2 100644 --- a/src/core/Akka.Tests/Actor/RepointableActorRefSpecs.cs +++ b/src/core/Akka.Tests/Actor/RepointableActorRefSpecs.cs @@ -5,6 +5,7 @@ // //----------------------------------------------------------------------- +using System.Threading.Tasks; using Akka.Actor; using Akka.Configuration; using Akka.TestKit; @@ -46,11 +47,11 @@ private void Set() /// Fixes https://github.com/akkadotnet/akka.net/pull/2182 /// [Fact] - public void Fix2128_RepointableActorRef_multiple_enumerations() + public async Task Fix2128_RepointableActorRef_multiple_enumerations() { var actor = Sys.ActorOf(Props.Create(() => new Bug2182Actor()).WithDispatcher("akka.test.calling-thread-dispatcher"), "buggy"); actor.Tell("foo"); - ExpectMsg("foo"); + await ExpectMsgAsync("foo"); } } } diff --git a/src/core/Akka.Tests/Actor/SupervisorHierarchySpec.cs b/src/core/Akka.Tests/Actor/SupervisorHierarchySpec.cs index 3548a650d74..0970d66d13a 100644 --- a/src/core/Akka.Tests/Actor/SupervisorHierarchySpec.cs +++ b/src/core/Akka.Tests/Actor/SupervisorHierarchySpec.cs @@ -7,6 +7,7 @@ using System; using System.Threading; +using System.Threading.Tasks; using Akka.Actor; using Akka.Actor.Dsl; using Akka.TestKit; @@ -111,7 +112,7 @@ public SupervisorHierarchySpec() } [Fact] - public void A_supervisor_hierarchy_must_Restart_Manager_And_Workers_In_AllForOne() + public async Task A_supervisor_hierarchy_must_Restart_Manager_And_Workers_In_AllForOne() { var countDown = new CountdownEvent(4); SupervisorStrategy strategy = new OneForOneStrategy(_ => Directive.Restart); @@ -119,14 +120,14 @@ public void A_supervisor_hierarchy_must_Restart_Manager_And_Workers_In_AllForOne Func decider = _ => { return Directive.Escalate; }; var managerProps = new PropsWithName(Props.Create(() => new CountDownActor(countDown, new AllForOneStrategy(decider))), "manager"); - var manager = boss.Ask(managerProps, TestKitSettings.DefaultTimeout).Result; + var manager = await boss.Ask(managerProps, TestKitSettings.DefaultTimeout); var workerProps = Props.Create(() => new CountDownActor(countDown, SupervisorStrategy.DefaultStrategy)); - var worker1 = manager.Ask(new PropsWithName(workerProps, "worker1"), TestKitSettings.DefaultTimeout).Result; - var worker2 = manager.Ask(new PropsWithName(workerProps, "worker2"), TestKitSettings.DefaultTimeout).Result; - var worker3 = manager.Ask(new PropsWithName(workerProps, "worker3"), TestKitSettings.DefaultTimeout).Result; + var worker1 = await manager.Ask(new PropsWithName(workerProps, "worker1"), TestKitSettings.DefaultTimeout); + var worker2 = await manager.Ask(new PropsWithName(workerProps, "worker2"), TestKitSettings.DefaultTimeout); + var worker3 = await manager.Ask(new PropsWithName(workerProps, "worker3"), TestKitSettings.DefaultTimeout); - EventFilter.Exception().ExpectOne(() => + await EventFilter.Exception().ExpectOneAsync(() => { worker1.Tell(Kill.Instance); // manager + all workers should be restarted by only killing a worker @@ -139,7 +140,7 @@ public void A_supervisor_hierarchy_must_Restart_Manager_And_Workers_In_AllForOne } [Fact] - public void A_supervisor_must_send_notifications_to_supervisor_when_permanent_failure() + public async Task A_supervisor_must_send_notifications_to_supervisor_when_permanent_failure() { var countDownMessages = new CountdownEvent(1); var countDownMax = new CountdownEvent(1); @@ -162,7 +163,7 @@ public void A_supervisor_must_send_notifications_to_supervisor_when_permanent_fa //We then send another "killCrasher", which again will send Kill to crasher. It crashes, //decider says it should be restarted but since we specified maximum 1 restart/5seconds it will be //permanently stopped. Boss, which watches crasher, receives Terminated, and counts down countDownMax - EventFilter.Exception().Expect(2, () => + await EventFilter.Exception().ExpectAsync(2, () => { boss.Tell("killCrasher"); boss.Tell("killCrasher"); @@ -171,7 +172,7 @@ public void A_supervisor_must_send_notifications_to_supervisor_when_permanent_fa countDownMax.Wait(TimeSpan.FromSeconds(2)).ShouldBeTrue(); } - private void Helper_A_supervisor_hierarchy_must_resume_children_after_Resume() + private async Task Helper_A_supervisor_hierarchy_must_resume_children_after_Resume() where T : ActorBase, new() { //Build this hierarchy: @@ -183,36 +184,36 @@ private void Helper_A_supervisor_hierarchy_must_resume_children_after_Resume( var name = typeof(T).Name; var boss = ActorOf(name); boss.Tell("spawn:middle"); - var middle = ExpectMsg(); + var middle = await ExpectMsgAsync(); middle.Tell("spawn:worker"); - var worker = ExpectMsg(); + var worker = await ExpectMsgAsync(); //Check everything is in place by sending ping to worker and expect it to respond with pong worker.Tell("ping"); - ExpectMsg("pong"); - EventFilter.Warning("expected").ExpectOne(() => //expected exception is thrown by the boss when it crashes + await ExpectMsgAsync("pong"); + await EventFilter.Warning("expected").ExpectOneAsync(() => //expected exception is thrown by the boss when it crashes { middle.Tell("fail"); //Throws an exception, and then it's resumed }); //verify that middle answers middle.Tell("ping"); - ExpectMsg("pong"); + await ExpectMsgAsync("pong"); //verify worker (child to middle) is up worker.Tell("ping"); - ExpectMsg("pong"); + await ExpectMsgAsync("pong"); } [Fact] - public void A_supervisor_hierarchy_must_resume_children_after_Resume() + public async Task A_supervisor_hierarchy_must_resume_children_after_Resume() { - Helper_A_supervisor_hierarchy_must_resume_children_after_Resume(); - Helper_A_supervisor_hierarchy_must_resume_children_after_Resume(); + await Helper_A_supervisor_hierarchy_must_resume_children_after_Resume(); + await Helper_A_supervisor_hierarchy_must_resume_children_after_Resume(); } [Fact] - public void A_supervisor_hierarchy_must_suspend_children_while_failing() + public async Task A_supervisor_hierarchy_must_suspend_children_while_failing() { var latch = CreateTestLatch(); var slowResumer = ActorOf(c => @@ -231,33 +232,33 @@ public void A_supervisor_hierarchy_must_suspend_children_while_failing() // | // worker slowResumer.Tell("spawn:boss"); - var boss = ExpectMsg(); + var boss = await ExpectMsgAsync(); boss.Tell("spawn:middle"); - var middle = ExpectMsg(); + var middle = await ExpectMsgAsync(); middle.Tell("spawn:worker"); - var worker = ExpectMsg(); + var worker = await ExpectMsgAsync(); //Check everything is in place by sending ping to worker and expect it to respond with pong worker.Tell("ping"); - ExpectMsg("pong"); - EventFilter.Warning("expected").ExpectOne(() => //expected exception is thrown by the boss when it crashes + await ExpectMsgAsync("pong"); + await EventFilter.Warning("expected").ExpectOneAsync(async () => //expected exception is thrown by the boss when it crashes { //Let boss crash, this means any child under boss should be suspended, so we wait for worker to become suspended. boss.Tell("fail"); - AwaitCondition(() => ((LocalActorRef)worker).Cell.Mailbox.IsSuspended()); + await AwaitConditionAsync(() => ((LocalActorRef)worker).Cell.Mailbox.IsSuspended()); //At this time slowresumer is currently handling the failure, in supervisestrategy, waiting for latch to be opened //We verify that no message is handled by worker, by sending it a ping //Normally it would respond with a pong, but since it's suspended nothing will happen. worker.Tell("ping"); - ExpectNoMsg(TimeSpan.FromSeconds(1)); + await ExpectNoMsgAsync(TimeSpan.FromSeconds(1)); //By counting down the latch slowResumer will continue in the supervisorstrategy and will return Resume. latch.CountDown(); }); //Check that all children, and especially worker is resumed. It should receive the ping and respond with a pong - ExpectMsg("pong", TimeSpan.FromMinutes(10)); + await ExpectMsgAsync("pong", TimeSpan.FromMinutes(10)); } [Fact]