如何测试 As persistence actor

How to test Akka Persistence actor

我正在从事一个基于 Akka Persistent FSM 的项目。特别是:

  1. 我想知道为独立性构建测试用例的最佳方法是什么?由于状态更改是持久化的(这在文档中没有很好地解释,但可以看到 here),确保我的持久性 actor 始终以干净状态开始可能很棘手。是否需要手动将重置构建到我的 actor FSM 协议中?如果是这样,这似乎并不理想,因为它是需要自行测试的新行为。

  2. 在测试中管理期刊本身的最佳方式是什么?有没有一种简单的方法来配置使用不同的日志进行测试,而不必在 actor 设计本身中明确选择?测试中的 Plugin TCK section of the docs mentions deleting the whole journal file manually. This seems reasonable for testing plugins themselves, but seems like an unnecessarily low-level solution for application code. Perhaps I need to explicitly call through to the journal's asyncDeleteMessagesTo 拆掉了吗?这看起来仍然很低级,但也许它只是尚未构建到库中的基础结构。

在单元测试持久性 actor 时,您可以使用内存中持久性插件,例如https://github.com/dnvriend/akka-persistence-inmemory

无需更改代码,您只需在 src/test/resources 中使用与 src/main/resources 中不同的 application.conf

为了单元测试之间的独立性,您可以将 persistenceId 设置为注入到 actor 中的东西。为每个测试创建一个具有不同持久性 ID 的 actor 的新实例。那么,即使之前测试的事件仍然存在于内存中,它们也不会影响后续测试。

如果你使用Akka testkit,每次测试都会创建一个新的actor系统运行,并且在运行结束时系统关闭时,内存日志的内容是已销毁,因此无需手动清理。