I am wondering what the canonical way is of testing whether an actor has stopped in akka. Here is an example of how I am currently doing; I'm worried I'm over complicating it.
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)
}
When an actor throws an unexpected exception, a failure, while processing a message or during initialization, the actor will by default be stopped.
An actor can stop itself by returning. Behaviors. stopped as the next behavior. A child actor can be forced to stop after it finishes processing its current message by using the stop method of the ActorContext from the parent actor.
In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor.
In short, there are three ways to stop an actor: Stop() the actor: stops the actor immediately after it finishes processing the current message. Kill the actor: this throws an ActorKilledException which will be logged and handled. The actor will stop immediately after it finishes processing the current message.
You can get rid of the separate watcher 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 section of the Akka testing docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With