Akka TestProbe to test context.watch() / Terminate

2019-06-26 03:33发布

I'm testing an akka system using TestKit . One actor of the system I'm testing, upon receiving a certain message type, context.watches the sender, and kills itself when the sender dies:

trait Handler extends Actor {
    override def receive: Receive = {
        case Init => context.watch(sender)
        case Terminated => context.stop(self)
    }
}

In my test I'm sending

val probe = TestProbe(system)
val target = TestActorRef(Props(classOf[Handler]))
probe.send(target, Init)

Now, to test the watch / Terminated behavior - I want to simulate the testprobe being killed.

I can do

probe.send(target, Terminated)

But, this presupposes that target has called context.watch(sender) , else it would not receive a Terminated.

I can do

probe.testActor ! Kill

with doesn't send Terminated unless target has correctly called context.watch(sender) , but I don't actually want the testprobe killed, as it needs to remain responsive to test if (for example) target continues to send messages instead of stopping itself .

I'm come across this a few times now, what's the correct way to test if an actor is handling the above situation correctly?

标签: akka
1条回答
老娘就宠你
2楼-- · 2019-06-26 04:07

You could watch the actor under test for termination with a separate probe instead of trying to do that via the 'sender' probe:

val probe = TestProbe(system)
val deathWatcher = TestProbe(system)

val target = TestActorRef(Props(classOf[Handler]))
deathWatcher.watch(target)

probe.send(target, Init)
// TODO make sure the message is processed.. perhaps ack it?

probe ! Kill

deathWatcher.expectTerminated(target)
查看更多
登录 后发表回答