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.Stash tests to async/await - ActorWithStashSpec #5825

Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/core/Akka.Tests/Actor/Stash/ActorWithStashSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.TestKit;
using Akka.TestKit.TestActors;
using Akka.Tests.TestUtils;
using Akka.Tests.Util;
using Xunit;

namespace Akka.Tests.Actor.Stash
Expand Down Expand Up @@ -89,7 +90,7 @@ await EventFilter.DeadLetter<string>(s=>s=="message", source: stasher.Path.ToStr
}

[Fact]
public void An_actor_Must_process_stashed_messages_after_restart()
public async Task An_actor_Must_process_stashed_messages_after_restart()
{
SupervisorStrategy strategy = new OneForOneStrategy(2, TimeSpan.FromSeconds(1), e => Directive.Restart);
var boss = ActorOf(() => new Supervisor(strategy));
Expand All @@ -98,7 +99,7 @@ public void An_actor_Must_process_stashed_messages_after_restart()
var slaveProps = Props.Create(() => new SlaveActor(restartLatch, hasMsgLatch, "stashme"));

//Send the props to supervisor, which will create an actor and return the ActorRef
var slave = boss.AskAndWait<IActorRef>(slaveProps, TestKitSettings.DefaultTimeout);
var slave = await boss.Ask<IActorRef>(slaveProps).WithTimeout(TestKitSettings.DefaultTimeout);

//send a message that will be stashed
slave.Tell("stashme");
Expand All @@ -121,7 +122,7 @@ public async Task An_actor_that_clears_the_stash_on_preRestart_Must_not_receive_
var slaveProps = Props.Create(() => new ActorsThatClearsStashOnPreRestart(restartLatch));

//Send the props to supervisor, which will create an actor and return the ActorRef
var slave = boss.AskAndWait<IActorRef>(slaveProps, TestKitSettings.DefaultTimeout);
var slave = await boss.Ask<IActorRef>(slaveProps).WithTimeout(TestKitSettings.DefaultTimeout);;

//send messages that will be stashed
slave.Tell("stashme 1");
Expand Down
28 changes: 28 additions & 0 deletions src/core/Akka.Tests/Util/TaskHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,33 @@ public static async Task<bool> AwaitWithTimeout(this Task parentTask, TimeSpan t
}
}
}

public static async Task<T> WithTimeout<T>(this Task<T> parentTask, TimeSpan timeout)
{
using (var cts = new CancellationTokenSource())
{
try
{
var delayed = Task.Delay(timeout, cts.Token);
var returnedTask = await Task.WhenAny(delayed, parentTask);

if (returnedTask != parentTask)
throw new TaskCanceledException($"Task timed out after {timeout.TotalSeconds} seconds");

if(returnedTask == parentTask && returnedTask.Exception != null)
{
var flattened = returnedTask.Exception.Flatten();
ExceptionDispatchInfo.Capture(flattened.InnerException).Throw();
}

return parentTask.Result;
}
finally
{
cts.Cancel();
}
}
}

}
}