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.IO tests to async/await - UdpListenerSpec #5801

Merged
merged 4 commits into from
Mar 31, 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
46 changes: 25 additions & 21 deletions src/core/Akka.Tests/IO/UdpListenerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Xunit.Abstractions;
using UdpListener = Akka.IO.UdpListener;
using FluentAssertions;
using System.Threading.Tasks;

namespace Akka.Tests.IO
{
Expand All @@ -32,15 +33,15 @@ 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
{
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<Udp.Bound>();
var bound = await probe.ExpectMsgAsync<Udp.Bound>();

bound.LocalAddress.Should().BeOfType<IPEndPoint>();
var boundEndpoint = (IPEndPoint)bound.LocalAddress;
Expand All @@ -56,15 +57,15 @@ 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
{
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<Udp.Bound>();
var bound = await probe.ExpectMsgAsync<Udp.Bound>();

bound.LocalAddress.Should().BeOfType<IPEndPoint>();
var boundEndpoint = (IPEndPoint)bound.LocalAddress;
Expand All @@ -80,39 +81,39 @@ 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()
public async Task A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete()
{
new TestSetup(this).Run(x =>
await new TestSetup(this).RunAsync(async x =>
{
x.BindCommander.ExpectMsg<Udp.Bound>();
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();
});
}

[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(x =>
await new TestSetup(this).RunAsync(async x =>
{
x.BindCommander.ExpectMsg<Udp.Bound>();
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();
x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram));
x.Handler.ExpectMsg<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram));
x.Handler.ExpectMsg<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => 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()
public async Task A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away()
{
new TestSetup(this).Run(x =>
await new TestSetup(this).RunAsync(async x =>
{
x.BindCommander.ExpectMsg<Udp.Bound>();
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();

// 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<Udp.Received>(_ =>
await x.Handler.ExpectMsgAsync<Udp.Received>(_ =>
{
Assert.Equal(requestMessage, Encoding.UTF8.GetString(_.Data.ToArray()));
});
Expand All @@ -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<Udp.Received>(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray())));
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray())));
});
}

Expand Down Expand Up @@ -168,10 +169,13 @@ public void Run(Action<TestSetup> test)
{
test(this);
}

public void BindListener()
public async Task RunAsync(Func<TestSetup, Task> test)
{
await test(this);
}
public async Task BindListener()
{
_bindCommander.ExpectMsg<Udp.Bound>();
await _bindCommander.ExpectMsgAsync<Udp.Bound>();
}

public IPEndPoint SendDataToLocal(byte[] buffer)
Expand Down