我在使用 PersistAll 和 PersistAllAsync 方法的 ReceivePersistentActor 实现的某些单元测试中遇到问题。
问题是当使用 TestKit.NUnit 进行单元测试时,PersistAll/Async 调用永远不会调用它们的完成回调。然而,Persist/Async 调用工作正常,并且 PersistAll/Async 调用在单元测试之外工作正常。
这与 TestKit 以及它尝试在 CallingThreadDispatcher 上运行的方式有关吗?
我不确定如何为使用 PersistAll/Async 的代码编写单元测试 - 由于从未调用过回调(我有我的 Sender.Tell 代码),测试总是因超时而失败。
我没有做任何奇怪的事情 - 我正在使用内存日志和快照存储,它们在默认调度程序上运行(我尝试对这些使用调用线程调度程序,但在单元测试中根本没有任何工作) .
公共接口 IEvent;
公共类测试: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
}
}