Skip to content

Commit

Permalink
Port Akka.Tests.Dispatch tests to async/await - MailboxesSpec (#…
Browse files Browse the repository at this point in the history
…5790)

* Port `Akka.Tests.Dispatch` tests to `async/await` - `MailboxesSpec`

* await `AwaitConditionAsync`

* Resolves #5790 (comment)
  • Loading branch information
eaba authored Mar 31, 2022
1 parent ab5071d commit 3a22cd0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
14 changes: 7 additions & 7 deletions src/core/Akka.Tests/Actor/InboxSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public void Inbox_support_queueing_multiple_queries()
Task.Factory.StartNew(() =>
{
Thread.Sleep(100);
return _inbox.ReceiveWhere(x => x.ToString() == "world");
}),
return _inbox.ReceiveWhere(x => x.ToString() == "world");
}),
Task.Factory.StartNew(() =>
{
Thread.Sleep(200);
return _inbox.ReceiveWhere(x => x.ToString() == "hello");
})
return _inbox.ReceiveWhere(x => x.ToString() == "hello");
})
};

_inbox.Receiver.Tell(42);
Expand All @@ -77,7 +77,7 @@ public void Inbox_support_selective_receives()
_inbox.Receiver.Tell("hello");
_inbox.Receiver.Tell("world");

var selection = _inbox.ReceiveWhere(x => x.ToString() == "world");
var selection = _inbox.ReceiveWhere(x => x.ToString() == "world");
selection.ShouldBe("world");
_inbox.Receive().ShouldBe("hello");
}
Expand All @@ -94,7 +94,7 @@ public async Task Inbox_have_maximum_queue_size()
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));

//The inbox is full. Sending another message should result in a Warning message
await EventFilter.Warning(start:"Dropping message").ExpectOneAsync(() => _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);
Expand Down Expand Up @@ -138,7 +138,7 @@ await WithinAsync(TimeSpan.FromSeconds(1), () =>
[Fact]
public void Select_WithClient_should_update_Client_and_copy_the_rest_of_the_properties_BUG_427()
{
var deadline = new TimeSpan(Sys.Scheduler.MonotonicClock.Ticks/2); //Some point in the past
var deadline = new TimeSpan(Sys.Scheduler.MonotonicClock.Ticks / 2); //Some point in the past
Predicate<object> predicate = o => true;
var actorRef = new EmptyLocalActorRef(((ActorSystemImpl)Sys).Provider, new RootActorPath(new Address("akka", "test")), Sys.EventStream);
var select = new Select(deadline, predicate, actorRef);
Expand Down
68 changes: 34 additions & 34 deletions src/core/Akka.Tests/Dispatch/MailboxesSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ public Property UnboundedPriorityQueue_should_sort_items_in_expected_order(int[]
#endif

[Fact]
public void Can_use_unbounded_priority_mailbox()
public async Task Can_use_unbounded_priority_mailbox()
{
var actor = (IInternalActorRef)Sys.ActorOf(EchoActor.Props(this).WithMailbox("string-prio-mailbox"), "echo");

//pause mailbox until all messages have been told
actor.SendSystemMessage(new Suspend());

// wait until we can confirm that the mailbox is suspended before we begin sending messages
AwaitCondition(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
await AwaitConditionAsync(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());

actor.Tell(true);
for (var i = 0; i < 30; i++)
Expand All @@ -222,27 +222,27 @@ public void Can_use_unbounded_priority_mailbox()
//resume mailbox, this prevents the mailbox from running to early
//priority mailbox is best effort only

ExpectMsg("a");
ExpectMsg(true);
await ExpectMsgAsync("a");
await ExpectMsgAsync(true);
for (var i = 0; i < 60; i++)
{
ExpectMsg(1);
await ExpectMsgAsync(1);
}
ExpectMsg(2.0);
await ExpectMsgAsync(2.0);

ExpectNoMsg(TimeSpan.FromSeconds(0.3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(0.3));
}

[Fact]
public void Can_use_unbounded_stable_priority_mailbox()
public async Task Can_use_unbounded_stable_priority_mailbox()
{
var actor = (IInternalActorRef)Sys.ActorOf(EchoActor.Props(this).WithMailbox("stable-prio-mailbox"), "echo");

//pause mailbox until all messages have been told
actor.SendSystemMessage(new Suspend());

// wait until we can confirm that the mailbox is suspended before we begin sending messages
AwaitCondition(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
await AwaitConditionAsync(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());

actor.Tell(true);
for (var i = 0; i < 30; i++)
Expand All @@ -260,26 +260,26 @@ public void Can_use_unbounded_stable_priority_mailbox()
//resume mailbox, this prevents the mailbox from running to early
//priority mailbox is best effort only

ExpectMsg("a");
ExpectMsg(true);
await ExpectMsgAsync("a");
await ExpectMsgAsync(true);
for (var i = 0; i < 60; i++)
{
ExpectMsg(i);
await ExpectMsgAsync(i);
}
ExpectMsg(2.0);
await ExpectMsgAsync(2.0);

ExpectNoMsg(TimeSpan.FromSeconds(0.3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(0.3));
}

[Fact]
public void Priority_mailbox_keeps_ordering_with_many_priority_values()
public async Task Priority_mailbox_keeps_ordering_with_many_priority_values()
{
var actor = (IInternalActorRef)Sys.ActorOf(EchoActor.Props(this).WithMailbox("int-prio-mailbox"), "echo");

//pause mailbox until all messages have been told
actor.SendSystemMessage(new Suspend());

AwaitCondition(()=> (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
await AwaitConditionAsync(()=> (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
// creates 50 messages with values spanning from Int32.MinValue to Int32.MaxValue
var values = new int[50];
var increment = (int)(UInt32.MaxValue / values.Length);
Expand All @@ -301,23 +301,23 @@ public void Priority_mailbox_keeps_ordering_with_many_priority_values()
// expect the messages in the correct order
foreach (var value in values)
{
ExpectMsg(value);
ExpectMsg(value);
ExpectMsg(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
}

ExpectNoMsg(TimeSpan.FromSeconds(0.3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(0.3));
}

[Fact]
public void Unbounded_Priority_Mailbox_Supports_Unbounded_Stashing()
public async Task Unbounded_Priority_Mailbox_Supports_Unbounded_Stashing()
{
var actor = (IInternalActorRef)Sys.ActorOf(StashingActor.Props(this).WithMailbox("int-prio-mailbox"), "echo");

//pause mailbox until all messages have been told
actor.SendSystemMessage(new Suspend());

AwaitCondition(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
await AwaitConditionAsync(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());

var values = new int[10];
var increment = (int)(UInt32.MaxValue / values.Length);
Expand All @@ -338,29 +338,29 @@ public void Unbounded_Priority_Mailbox_Supports_Unbounded_Stashing()
//resume mailbox, this prevents the mailbox from running to early
actor.SendSystemMessage(new Resume(null));

this.Within(5.Seconds(), () =>
await WithinAsync(5.Seconds(), async() =>
{
// expect the messages in the correct order
foreach (var value in values)
{
ExpectMsg(value);
ExpectMsg(value);
ExpectMsg(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
}
});

ExpectNoMsg(TimeSpan.FromSeconds(0.3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(0.3));
}

[Fact]
public void Unbounded_Stable_Priority_Mailbox_Supports_Unbounded_Stashing()
public async Task Unbounded_Stable_Priority_Mailbox_Supports_Unbounded_Stashing()
{
var actor = (IInternalActorRef)Sys.ActorOf(StashingActor.Props(this).WithMailbox("stable-prio-mailbox"), "echo");

//pause mailbox until all messages have been told
actor.SendSystemMessage(new Suspend());

AwaitCondition(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());
await AwaitConditionAsync(() => (((ActorRefWithCell)actor).Underlying is ActorCell) && ((ActorRefWithCell)actor).Underlying.AsInstanceOf<ActorCell>().Mailbox.IsSuspended());

var values = new int[10];
var increment = (int)(UInt32.MaxValue / values.Length);
Expand All @@ -381,18 +381,18 @@ public void Unbounded_Stable_Priority_Mailbox_Supports_Unbounded_Stashing()
//resume mailbox, this prevents the mailbox from running to early
actor.SendSystemMessage(new Resume(null));

this.Within(5.Seconds(), () =>
await WithinAsync(5.Seconds(), async() =>
{
// expect the messages in the original order
foreach (var value in values)
{
ExpectMsg(value);
ExpectMsg(value);
ExpectMsg(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
await ExpectMsgAsync(value);
}
});

ExpectNoMsg(TimeSpan.FromSeconds(0.3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(0.3));
}
}
}
Expand Down

0 comments on commit 3a22cd0

Please sign in to comment.