Skip to content

Commit

Permalink
Port Akka.Tests.IO tests to async/await - TcpListenerSpec (#5797)
Browse files Browse the repository at this point in the history
* Port `Akka.Tests.IO` tests to `async/await` - `TcpListenerSpec`

* Resolve #5797 (comment)

Co-authored-by: Gregorius Soedharmo <[email protected]>
  • Loading branch information
eaba and Arkatufus authored Mar 31, 2022
1 parent 69d72a2 commit 0c809b5
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/core/Akka.Tests/IO/TcpListenerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.IO;
using Akka.TestKit;
Expand All @@ -30,38 +31,38 @@ public TcpListenerSpec()
{ }

[Fact]
public void A_TCP_Listener_must_let_the_bind_commander_know_when_binding_is_complete()
public async Task A_TCP_Listener_must_let_the_bind_commander_know_when_binding_is_complete()
{
new TestSetup(this, pullMode: false).Run(x =>
await new TestSetup(this, pullMode: false).RunAsync(async x =>
{
x.BindCommander.ExpectMsg<Tcp.Bound>();
await x.BindCommander.ExpectMsgAsync<Tcp.Bound>();
});
}

[Fact]
public void A_TCP_Listener_must_continue_to_accept_connections_after_a_previous_accept()
public async Task A_TCP_Listener_must_continue_to_accept_connections_after_a_previous_accept()
{
new TestSetup(this, pullMode: false).Run(x =>
await new TestSetup(this, pullMode: false).RunAsync(async x =>
{
x.BindListener();
await x.BindListener();
x.AttemptConnectionToEndpoint();
x.AttemptConnectionToEndpoint();
await x.AttemptConnectionToEndpoint();
await x.AttemptConnectionToEndpoint();
});
}

[Fact]
public void A_TCP_Listener_must_react_to_unbind_commands_by_replying_with_unbound_and_stopping_itself()
public async Task A_TCP_Listener_must_react_to_unbind_commands_by_replying_with_unbound_and_stopping_itself()
{
new TestSetup(this, pullMode:false).Run(x =>
await new TestSetup(this, pullMode:false).RunAsync(async x =>
{
x.BindListener();
await x.BindListener();
var unbindCommander = CreateTestProbe();
unbindCommander.Send(x.Listener, Tcp.Unbind.Instance);
unbindCommander.ExpectMsg(Tcp.Unbound.Instance);
x.Parent.ExpectTerminated(x.Listener);
await unbindCommander.ExpectMsgAsync(Tcp.Unbound.Instance);
await x.Parent.ExpectTerminatedAsync(x.Listener);
});
}

Expand Down Expand Up @@ -95,17 +96,21 @@ public void Run(Action<TestSetup> test)
{
test(this);
}
public async Task RunAsync(Func<TestSetup, Task> test)
{
await test(this);
}

public void BindListener()
public async Task BindListener()
{
var bound = _bindCommander.ExpectMsg<Tcp.Bound>();
var bound = await _bindCommander.ExpectMsgAsync<Tcp.Bound>();
LocalEndPoint = (IPEndPoint)bound.LocalAddress;
}

public void AttemptConnectionToEndpoint()
public async Task AttemptConnectionToEndpoint()
{
new Socket(LocalEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
.Connect(LocalEndPoint);
await new Socket(LocalEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
.ConnectAsync(LocalEndPoint);
}

public IActorRef Listener { get { return _parentRef.UnderlyingActor.Listener; } }
Expand Down

0 comments on commit 0c809b5

Please sign in to comment.