Akka: how to verify that an actor is stopped

I wonder what is the canonical way of checking if an actor has stopped in an acc. Here is an example of how I am doing it now; I worry that I'm over complicating this.

import akka.actor.{Terminated, Actor, Props, ActorSystem} import akka.testkit.TestProbe class MyActor extends Actor { import MyActor._ override def receive: Receive = { case Stop => context.stop(self) } } object MyActor { def props = Props(new MyActor) case object Stop } object MyActorSpec { val system = ActorSystem() val myActor = system.actorOf(MyActor.props) val testProbe = TestProbe() case object MyActorStopped val watcher = system.actorOf(Props(new Actor { context.watch(myActor) override def receive: Actor.Receive = { case Terminated(`myActor`) => testProbe.ref ! MyActorStopped } })) myActor ! MyActor.Stop testProbe.expectMsg(MyActorStopped) } 
+6
source share
1 answer

You can get rid of a single observer actor and just watch the target actor directly from the testProbe actor:

 val testProbe = TestProbe() testProbe watch myActor myActor ! MyActor.Stop testProbe.expectTerminated(myActor) 

See here for the relevant Akka test docs section.

+18
source

All Articles