0

I have a test on an "Akka Classic" actor the is testing the persistence.

The logic is simple:

  1. Create an actor and send a bunch of events.
  2. Stop the actor using akka.actor.Kill or akka.actor.PoisonKill depending on the test.
  3. Start the actor again and test if it has been recovered correctly.

I'm migrating it to AKKA 2.6 and "AKKA Typed", but a Kill and PoisonKill are not available.

From the documentation:

PoisonPill is not supported in Typed. Instead, if you need to request an actor to stop you should define a message that the actor understands and let it return Behaviors.stopped when receiving that message.

But the PoisonPill behavior is easy to reproduce with the TestKit.stop utility.

But how about Kill? It throws an ActorKilledException that will be managed for the supervisor. How to do it in Akka Typed?

So the question is: How to implement this test using "AKKA Typed"?

angelcervera
  • 3,699
  • 1
  • 40
  • 68

1 Answers1

1

in care of stopping actors, the ActorRef can use toClassic when akka.actor.typed.scaladsl.adapter._ was imported and send the Signal akka.actor.typed.internal.PoisonPill.

When define receiveSignal and logging all signals, the pill does the expected work and your actor is stopped.

.receiveSignal {
    case (ctx, signal) =>
        ctx.log.debug("{}", signal)
        Behaviors.stopped
}

first PoisonPill then PostStop was logged in my case.

  • Hi @sourcecodebot. Thanks for your response. Few things about it: 1. Add not necessary runtime code only to support test is not a good practice. 2. It is an internal class so it is not good practice to use it. 3. This implementation stops the actor gracefully similar to classic PoisonPill, but it is different than Kill that throws an ActorKilledException that the supervisor will manage. A new question: Should the Actor returning .toClassic supports classic Kill and PoisonPill? – angelcervera Nov 14 '19 at 08:08
  • hi @angelcervera, to match the typed internal signal api I recommend to not use classic commands. `toClassic` allows you to send a signal, which is not defined in the behavior type information. In care of your edited question, it's recommended to define a `Shutdown` or `Interrupt` extended from your behavior Command type and return in these case `Behaviors.stopped`. [example](https://scastie.scala-lang.org/WdZ7iixZT0aM0BI2xgSf5Q) – SourceCodeBot Nov 14 '19 at 12:49