Skip to content

Instantly share code, notes, and snippets.

@rogeralsing
Last active August 29, 2015 14:15
Show Gist options
  • Save rogeralsing/9c2201945f799198d52e to your computer and use it in GitHub Desktop.
Save rogeralsing/9c2201945f799198d52e to your computer and use it in GitHub Desktop.
Safe Async Await in Actors
using System;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using Xunit;
namespace Akka.Tests.Dispatch
{
public class AsyncAwaitActor : ReceiveActor
{
public AsyncAwaitActor()
{
Receive<string>(async m =>
{
await Task.Delay(TimeSpan.FromSeconds(1));
Sender.Tell("done");
});
}
}
public class AskerActor : ReceiveActor
{
public AskerActor(ActorRef other)
{
Receive<string>(async m =>
{
var res = await other.Ask(m);
Sender.Tell(res);
});
}
}
public class ActorAsyncAwaitSpec : AkkaSpec
{
[Fact]
public async Task Actors_should_be_able_to_async_await_ask_message_loop()
{
var actor = Sys.ActorOf(Props.Create<AsyncAwaitActor>().WithDispatcher("akka.actor.task-dispatcher"),
"Worker");
var asker = Sys.ActorOf(Props.Create(() => new AskerActor(actor)).WithDispatcher("akka.actor.task-dispatcher"),
"Asker");
var res = await asker.Ask("something");
Assert.Equal("done", res);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment