vendredi 4 décembre 2015

akka: how to test that an actor was stopped

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)
}

Aucun commentaire:

Enregistrer un commentaire