1

在我的单元测试中,有没有办法杀死并重新启动持久性参与者以检查它是否可以正确保留状态(例如,事件序列化/反序列化工作正常)?

4

2 回答 2

3
// Initialize the actor and a probe for the actor 
val probe = TestProbe()
val act = system.actorOf(Props[MyActor], "name")

// send messages to the actor to change state
// Validate that the state has changed

// Send a message to terminate the actor
act ! PoisonPill

// wait for the actor to shutdown 
probe.watch(act)
probe.expectTerminated(act)

// Initialize a new probe and a new instance of the actor, with the assumption 
// that the actor has the same persistence id as the initial one.
// That actor should now be in the same state as before. 

val probeTwo = TestProbe()
val rehydrated = system.actorOf(Props[MyActor], "name")

// Validate the state of the actor. 
// Todo: Put your validation checks here

以下是有关如何测试演员的一些有用信息。

于 2016-02-03T18:55:30.877 回答
0

注意:对于持久演员,不推荐使用 PoisonPill 消息来停止演员。推荐的方式是显式关闭,例如:system.stop(act)

http://doc.akka.io/docs/akka/current/scala/persistence.html#Safely_shutting_down_persistent_actors

于 2016-12-04T08:15:09.533 回答