angelcervera
angelcervera

Reputation: 4179

Testing AKKA 2.6 persistence actor typed (alternatives to Kill and PoisonPill)

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"?

Upvotes: 0

Views: 588

Answers (1)

SourceCodeBot
SourceCodeBot

Reputation: 175

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.

Upvotes: 1

Related Questions