I have a problem in some unit tests of a ReceivePersistentActor implementation using PersistAll and PersistAllAsync methods.
The problem is that when unit testing with TestKit.NUnit, the PersistAll/Async calls never call their completion callbacks. Persist/Async calls work fine however and the PersistAll/Async calls work fine outside of unit testing.
Has this something to do with TestKit and the way it tries to run things on the CallingThreadDispatcher?
I'm not sure how to write unit tests for code where PersistAll/Async is used - the tests always fail by timing out due to the callbacks never being invoked (where I have my Sender.Tell code).
I'm not doing anything weird - i'm using the in-memory journal and snapshot-store and these are running on the default dispatcher (I tried using calling-thread dispatcher for these but then nothing worked at all in the unit tests).
public interface IEvent;
public class Test : ReceivePersistentActor {
public class StringReceivedEvent:IEvent {}
public Test()
{
Console.WriteLine("Started"); // <-- This is seen, but the next console output is not - callback is never fired
Command<string>(message => {
Console.WriteLine($"Received {message}");
PersistAll(new List<IEvent>(){new StringReceivedEvent()}, @event => {
Console.WriteLine("Persisted event"); //<-- never see this
Sender.Tell(true); //<-- this never gets called
});
});
}
public override string PersistenceId => "test-id";
}
[TestFixture]
public class Tests : Akka.TestKit.NUnit.TestKit
{
IActorRef subject;
public PublishingTests() : base(ConfigurationFactory.Load().WithFallback(Akka.TestKit.Configs.TestConfigs.DefaultConfig))
{
}
[SetUp]
public void Setup() {
subject = ActorOf(Props.Create<Test>(), "My-Test-Actor");
}
[Test]
public void Can_Persist_Event_And_Send_Completion_Reply() {
subject.Tell("hello");
ExpectMsg<bool>(TimeSpan.FromSeconds(15)); //<---- This times out
}
}
I just tried your code in a new .NET Core 2.0 project (with some slight modifications to get it to compile), and it works fine.
Here is the complete replication steps. First on commandline/PowerShell:
I now have a .csproj file that looks like this:
Then, for the sake of just reproducing, I put all code in one file like this:
Then you should be able to test your code from command line again:
Giving output:
I had a similar problem with the UntypedPersistentActor and xUnit, but it suddenly disappeared after I changed it to a ReceivePersistentActor, and restarted Visual Studio, and other things, but there were too many things going on at once, so unfortunately I am not able to provide you with a good answer to exactly what I did to solve it (also the reason which I wanted to try to figure this out and reproduce it here, but I wasn't able to). I hope a set of complete reproduction steps with exact package versions for a working project helps you out instead...