Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test an actor is not terminated in Akka

Tags:

akka

I'm trying to test if an actor was terminated or not, I know there is a way to test if it was terminated using TestProbe's expectTerminated

http://doc.akka.io/docs/akka/2.4.16/scala/testing.html#Watching_Other_Actors_from_Probes

But is there a way to test the opposite?

Thanks :)

Update

In my scenario, I have a user actor which has one or more child actors and it will terminate itself when all of its children are disconnected (terminated).

The behavior works fine but my problem is that I can only test the scenario which it has no children left and terminates itself. I can't find the right assertion to check that it's not terminated if it still has children left.

Here's a simplified test version:

"Terminates itself if there are no connected clients" in {
  val userActor = system.ActorOf(UserActor.props())

  userActor ! UserActor.ClientDisconnected()

  val deathWatch = TestProbe()
  deathWatch.watch(userActor)
  deathWatch.expectTerminated(userActor, timeoutDuration)
}

"Not Terminates itself when there are still other clients connected" in {
  val userActor = system.ActorOf(UserActor.props())

  // Connecting a client
  userActor ! UserActor.ClientConnected(sessionId, accessToken)

  userActor ! UserActor.ClientDisconnected()

  // How can I test that userActor is not terminated?
}
like image 812
Soroush Mirzaei Avatar asked Nov 08 '22 02:11

Soroush Mirzaei


1 Answers

Taking inspiration from the testkit, you could do something like:

  import scala.concurrent.duration._
  import akka.testkit._

  def expectNoTerminated(target: ActorRef, max: FiniteDuration) {
    val o = receiveOne(max.dilated)

    if (o ne null)
      assert(o != Terminated(target), s"received Terminated message $o")
  }

This will only check that, if you receive a message, it's not Terminated, hence it might not cover all scenarios..

like image 199
Stefano Bonetti Avatar answered Jan 04 '23 03:01

Stefano Bonetti