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.Actor tests to async/await - InboxSpec #5780

Merged
merged 5 commits into from
Mar 29, 2022
Merged
Changes from all 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
31 changes: 15 additions & 16 deletions src/core/Akka.Tests/Actor/InboxSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Actor.Internal;
using Akka.Event;
using Akka.TestKit;
using Akka.Tests.Util;
using Xunit;

namespace Akka.Tests.Actor
Expand All @@ -28,13 +29,13 @@ public InboxSpec()
}

[Fact]
public void Inbox_support_watch()
public async Task Inbox_support_watch()
{
_inbox.Watch(TestActor);

// check watch
TestActor.Tell(PoisonPill.Instance);
var received = _inbox.Receive(TimeSpan.FromSeconds(1));
var received = await _inbox.ReceiveAsync(TimeSpan.FromSeconds(1));

received.GetType().ShouldBe(typeof(Terminated));
var terminated = (Terminated)received;
Expand Down Expand Up @@ -82,22 +83,22 @@ public void Inbox_support_selective_receives()
}

[Fact]
public void Inbox_have_maximum_queue_size()
public async Task Inbox_have_maximum_queue_size()
{
try
{
//Fill the inbox (it can hold 1000) messages
foreach (var zero in Enumerable.Repeat(0, 1000))
_inbox.Receiver.Tell(zero);

ExpectNoMsg(TimeSpan.FromSeconds(1));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));

//The inbox is full. Sending another message should result in a Warning message
EventFilter.Warning(start:"Dropping message").ExpectOne(() => _inbox.Receiver.Tell(42));
await EventFilter.Warning(start:"Dropping message").ExpectOneAsync(() => _inbox.Receiver.Tell(42));

//The inbox is still full. But since the warning message has already been sent, no more warnings should be sent
_inbox.Receiver.Tell(42);
ExpectNoMsg(TimeSpan.FromSeconds(1));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));

//Receive all messages from the inbox
var gotit = Enumerable.Repeat(0, 1000).Select(_ => _inbox.Receive());
Expand All @@ -107,7 +108,7 @@ public void Inbox_have_maximum_queue_size()
}

//The inbox should be empty now, so receiving should result in a timeout
Intercept<TimeoutException>(() =>
Assert.Throws<TimeoutException>(() =>
{
var received = _inbox.Receive(TimeSpan.FromSeconds(1));
Log.Error("Received " + received);
Expand All @@ -120,18 +121,17 @@ public void Inbox_have_maximum_queue_size()
}

[Fact]
public void Inbox_have_a_default_and_custom_timeouts()
public async Task Inbox_have_a_default_and_custom_timeouts()
{
Within(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(6), () =>
await WithinAsync(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(6), () =>
{
Intercept<TimeoutException>(() => _inbox.Receive());
Assert.Throws<TimeoutException>(() => _inbox.Receive());
return true;
});

Within(TimeSpan.FromSeconds(1), () =>
await WithinAsync(TimeSpan.FromSeconds(1), () =>
{
Intercept<TimeoutException>(() => _inbox.Receive(TimeSpan.FromMilliseconds(100)));
return true;
Assert.Throws<TimeoutException>(() => _inbox.Receive(TimeSpan.FromMilliseconds(100)));
});
}

Expand All @@ -152,11 +152,10 @@ public void Select_WithClient_should_update_Client_and_copy_the_rest_of_the_prop
}

[Fact]
public void Inbox_Receive_will_timeout_gracefully_if_timeout_is_already_expired()
public async Task Inbox_Receive_will_timeout_gracefully_if_timeout_is_already_expired()
{
var task = _inbox.ReceiveAsync(TimeSpan.FromSeconds(-1));

Assert.True(task.Wait(1000), "Receive did not complete in time.");
Assert.True(await task.AwaitWithTimeout(TimeSpan.FromMilliseconds(1000)), "Receive did not complete in time.");
Assert.IsType<Status.Failure>(task.Result);
}
}
Expand Down