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
}
}