From 21bdb95ef8a9ccb0daf17a5a49c483cd2ee2035b Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Wed, 30 Mar 2022 16:09:01 +0100 Subject: [PATCH 1/2] Port `Akka.Tests.IO` tests to `async/await` - `UdpListenerSpec` --- src/core/Akka.Tests/IO/UdpListenerSpec.cs | 35 ++++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/core/Akka.Tests/IO/UdpListenerSpec.cs b/src/core/Akka.Tests/IO/UdpListenerSpec.cs index 27eda4f0a9b..ee0ee751621 100644 --- a/src/core/Akka.Tests/IO/UdpListenerSpec.cs +++ b/src/core/Akka.Tests/IO/UdpListenerSpec.cs @@ -16,6 +16,7 @@ using Xunit.Abstractions; using UdpListener = Akka.IO.UdpListener; using FluentAssertions; +using System.Threading.Tasks; namespace Akka.Tests.IO { @@ -32,7 +33,7 @@ public UdpListenerSpec(ITestOutputHelper output) { } [Fact] - public void UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address() + public async Task UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address() { var probe = CreateTestProbe(); try @@ -40,7 +41,7 @@ public void UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address() var endpoint = new IPEndPoint(IPAddress.Loopback, 12345); var handler = Sys.ActorOf(Props.Create(() => new MockUdpHandler())); Sys.Udp().Tell(new Udp.Bind(handler, endpoint), probe.Ref); - var bound = probe.ExpectMsg(); + var bound = await probe.ExpectMsgAsync(); bound.LocalAddress.Should().BeOfType(); var boundEndpoint = (IPEndPoint)bound.LocalAddress; @@ -56,7 +57,7 @@ public void UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address() } [Fact] - public void UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address() + public async Task UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address() { var probe = CreateTestProbe(); try @@ -64,7 +65,7 @@ public void UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address() var endpoint = new IPEndPoint(IPAddress.IPv6Loopback, 12345); var handler = Sys.ActorOf(Props.Create(() => new MockUdpHandler())); Sys.Udp().Tell(new Udp.Bind(handler, endpoint), probe.Ref); - var bound = probe.ExpectMsg(); + var bound = await probe.ExpectMsgAsync(); bound.LocalAddress.Should().BeOfType(); var boundEndpoint = (IPEndPoint)bound.LocalAddress; @@ -82,9 +83,9 @@ public void UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address() [Fact] public void A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete() { - new TestSetup(this).Run(x => + new TestSetup(this).Run(async x => { - x.BindCommander.ExpectMsg(); + await x.BindCommander.ExpectMsgAsync(); }); } @@ -92,27 +93,27 @@ public void A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_comp public void A_UDP_Listener_must_forward_incoming_packets_to_handler_actor() { const string dgram = "Fly little packet!"; - new TestSetup(this).Run(x => + new TestSetup(this).Run(async x => { - x.BindCommander.ExpectMsg(); + await x.BindCommander.ExpectMsgAsync(); x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram)); - x.Handler.ExpectMsg(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray()))); + await x.Handler.ExpectMsgAsync(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray()))); x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram)); - x.Handler.ExpectMsg(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray()))); + await x.Handler.ExpectMsgAsync(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray()))); }); } [Fact] public void A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away() { - new TestSetup(this).Run(x => + new TestSetup(this).Run(async x => { - x.BindCommander.ExpectMsg(); + await x.BindCommander.ExpectMsgAsync(); // Receive UDP messages from a sender const string requestMessage = "This is my last request!"; var notExistingEndPoint = x.SendDataToLocal(Encoding.UTF8.GetBytes(requestMessage)); - x.Handler.ExpectMsg(_ => + await x.Handler.ExpectMsgAsync(_ => { Assert.Equal(requestMessage, Encoding.UTF8.GetString(_.Data.ToArray())); }); @@ -126,11 +127,11 @@ public void A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_awa localSender.Tell(Udp.Send.Create(ByteString.FromBytes(Encoding.UTF8.GetBytes(response)), notExistingEndPoint)); // Now an ICMP error message "port unreachable" (SocketError.ConnectionReset) is sent to our UDP server port - x.Handler.ExpectNoMsg(TimeSpan.FromSeconds(1)); + await x.Handler.ExpectNoMsgAsync(TimeSpan.FromSeconds(1)); const string followUpMessage = "Back online!"; x.SendDataToLocal(Encoding.UTF8.GetBytes(followUpMessage)); - x.Handler.ExpectMsg(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray()))); + await x.Handler.ExpectMsgAsync(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray()))); }); } @@ -169,9 +170,9 @@ public void Run(Action test) test(this); } - public void BindListener() + public async Task BindListener() { - _bindCommander.ExpectMsg(); + await _bindCommander.ExpectMsgAsync(); } public IPEndPoint SendDataToLocal(byte[] buffer) From ff29061da3af68d43e09d79f95b6c1735acd61de Mon Sep 17 00:00:00 2001 From: Ebere Abanonu Date: Thu, 31 Mar 2022 11:16:36 +0100 Subject: [PATCH 2/2] Created `RunAsync` --- src/core/Akka.Tests/IO/UdpListenerSpec.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/Akka.Tests/IO/UdpListenerSpec.cs b/src/core/Akka.Tests/IO/UdpListenerSpec.cs index ee0ee751621..60e744ebf94 100644 --- a/src/core/Akka.Tests/IO/UdpListenerSpec.cs +++ b/src/core/Akka.Tests/IO/UdpListenerSpec.cs @@ -81,19 +81,19 @@ public async Task UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address() } [Fact] - public void A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete() + public async Task A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete() { - new TestSetup(this).Run(async x => + await new TestSetup(this).RunAsync(async x => { await x.BindCommander.ExpectMsgAsync(); }); } [Fact] - public void A_UDP_Listener_must_forward_incoming_packets_to_handler_actor() + public async Task A_UDP_Listener_must_forward_incoming_packets_to_handler_actor() { const string dgram = "Fly little packet!"; - new TestSetup(this).Run(async x => + await new TestSetup(this).RunAsync(async x => { await x.BindCommander.ExpectMsgAsync(); x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram)); @@ -104,9 +104,9 @@ public void A_UDP_Listener_must_forward_incoming_packets_to_handler_actor() } [Fact] - public void A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away() + public async Task A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away() { - new TestSetup(this).Run(async x => + await new TestSetup(this).RunAsync(async x => { await x.BindCommander.ExpectMsgAsync(); @@ -169,7 +169,10 @@ public void Run(Action test) { test(this); } - + public async Task RunAsync(Func test) + { + await test(this); + } public async Task BindListener() { await _bindCommander.ExpectMsgAsync();