Skip to content

Commit c152158

Browse files
eabaArkatufus
andauthored
Port Akka.Tests.IO tests to async/await - UdpListenerSpec (#5801)
* Port `Akka.Tests.IO` tests to `async/await` - `UdpListenerSpec` * Created `RunAsync` Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent 562997e commit c152158

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/core/Akka.Tests/IO/UdpListenerSpec.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Xunit.Abstractions;
1717
using UdpListener = Akka.IO.UdpListener;
1818
using FluentAssertions;
19+
using System.Threading.Tasks;
1920

2021
namespace Akka.Tests.IO
2122
{
@@ -32,15 +33,15 @@ public UdpListenerSpec(ITestOutputHelper output)
3233
{ }
3334

3435
[Fact]
35-
public void UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address()
36+
public async Task UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address()
3637
{
3738
var probe = CreateTestProbe();
3839
try
3940
{
4041
var endpoint = new IPEndPoint(IPAddress.Loopback, 12345);
4142
var handler = Sys.ActorOf(Props.Create(() => new MockUdpHandler()));
4243
Sys.Udp().Tell(new Udp.Bind(handler, endpoint), probe.Ref);
43-
var bound = probe.ExpectMsg<Udp.Bound>();
44+
var bound = await probe.ExpectMsgAsync<Udp.Bound>();
4445

4546
bound.LocalAddress.Should().BeOfType<IPEndPoint>();
4647
var boundEndpoint = (IPEndPoint)bound.LocalAddress;
@@ -56,15 +57,15 @@ public void UDP_should_return_IPv4_endpoint_if_bound_using_IPv4_address()
5657
}
5758

5859
[Fact]
59-
public void UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address()
60+
public async Task UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address()
6061
{
6162
var probe = CreateTestProbe();
6263
try
6364
{
6465
var endpoint = new IPEndPoint(IPAddress.IPv6Loopback, 12345);
6566
var handler = Sys.ActorOf(Props.Create(() => new MockUdpHandler()));
6667
Sys.Udp().Tell(new Udp.Bind(handler, endpoint), probe.Ref);
67-
var bound = probe.ExpectMsg<Udp.Bound>();
68+
var bound = await probe.ExpectMsgAsync<Udp.Bound>();
6869

6970
bound.LocalAddress.Should().BeOfType<IPEndPoint>();
7071
var boundEndpoint = (IPEndPoint)bound.LocalAddress;
@@ -80,39 +81,39 @@ public void UDP_should_return_IPv6_endpoint_if_bound_using_IPv6_address()
8081
}
8182

8283
[Fact]
83-
public void A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete()
84+
public async Task A_UDP_Listener_must_let_the_bind_commander_know_when_binding_is_complete()
8485
{
85-
new TestSetup(this).Run(x =>
86+
await new TestSetup(this).RunAsync(async x =>
8687
{
87-
x.BindCommander.ExpectMsg<Udp.Bound>();
88+
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();
8889
});
8990
}
9091

9192
[Fact]
92-
public void A_UDP_Listener_must_forward_incoming_packets_to_handler_actor()
93+
public async Task A_UDP_Listener_must_forward_incoming_packets_to_handler_actor()
9394
{
9495
const string dgram = "Fly little packet!";
95-
new TestSetup(this).Run(x =>
96+
await new TestSetup(this).RunAsync(async x =>
9697
{
97-
x.BindCommander.ExpectMsg<Udp.Bound>();
98+
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();
9899
x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram));
99-
x.Handler.ExpectMsg<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
100+
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
100101
x.SendDataToLocal(Encoding.UTF8.GetBytes(dgram));
101-
x.Handler.ExpectMsg<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
102+
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => Assert.Equal(dgram, Encoding.UTF8.GetString(_.Data.ToArray())));
102103
});
103104
}
104105

105106
[Fact]
106-
public void A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away()
107+
public async Task A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_away()
107108
{
108-
new TestSetup(this).Run(x =>
109+
await new TestSetup(this).RunAsync(async x =>
109110
{
110-
x.BindCommander.ExpectMsg<Udp.Bound>();
111+
await x.BindCommander.ExpectMsgAsync<Udp.Bound>();
111112

112113
// Receive UDP messages from a sender
113114
const string requestMessage = "This is my last request!";
114115
var notExistingEndPoint = x.SendDataToLocal(Encoding.UTF8.GetBytes(requestMessage));
115-
x.Handler.ExpectMsg<Udp.Received>(_ =>
116+
await x.Handler.ExpectMsgAsync<Udp.Received>(_ =>
116117
{
117118
Assert.Equal(requestMessage, Encoding.UTF8.GetString(_.Data.ToArray()));
118119
});
@@ -126,11 +127,11 @@ public void A_UDP_Listener_must_be_able_to_send_and_receive_when_server_goes_awa
126127
localSender.Tell(Udp.Send.Create(ByteString.FromBytes(Encoding.UTF8.GetBytes(response)), notExistingEndPoint));
127128

128129
// Now an ICMP error message "port unreachable" (SocketError.ConnectionReset) is sent to our UDP server port
129-
x.Handler.ExpectNoMsg(TimeSpan.FromSeconds(1));
130+
await x.Handler.ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
130131

131132
const string followUpMessage = "Back online!";
132133
x.SendDataToLocal(Encoding.UTF8.GetBytes(followUpMessage));
133-
x.Handler.ExpectMsg<Udp.Received>(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray())));
134+
await x.Handler.ExpectMsgAsync<Udp.Received>(_ => Assert.Equal(followUpMessage, Encoding.UTF8.GetString(_.Data.ToArray())));
134135
});
135136
}
136137

@@ -168,10 +169,13 @@ public void Run(Action<TestSetup> test)
168169
{
169170
test(this);
170171
}
171-
172-
public void BindListener()
172+
public async Task RunAsync(Func<TestSetup, Task> test)
173+
{
174+
await test(this);
175+
}
176+
public async Task BindListener()
173177
{
174-
_bindCommander.ExpectMsg<Udp.Bound>();
178+
await _bindCommander.ExpectMsgAsync<Udp.Bound>();
175179
}
176180

177181
public IPEndPoint SendDataToLocal(byte[] buffer)

0 commit comments

Comments
 (0)