jaksky
jaksky

Reputation: 3405

Is there a way to restart an akka actor

is there a way to restart an actor? I am writing a tests to test my recovery strategy in my actor system structure and I cannot find a way how to restart an actor to test my customized preRestart/postRestart life cycle hooks. Using default supervisorStrategy - OneForOne with default decider. I tried to use Killed but in default strategy that is translated to stop an actor. And overriding strategy would mean that I would be actually testing something different than it is actually running in production code.

Did I missed something important here as I consider having tested not just happy day scenarios as an integral part of the tests.

Thanks for clarification

Tried following but wasn't able to make:

trait FailActor extends Actor {

  abstract override def receive = LoggingReceive {
     fail.orElse(super.receive)
  }
  def fail:Receive = {
    case "fail" => throw new RuntimeException("Test")
  }
}

class AddressTranslatorFailActor(storage: ActorRef) extends AddressTranslatorActor(storage) with FailActor

And in the test passing this failing actor:

val probe = TestProbe()
  val addressServiceProps = Props {
    new AddressServiceActor {
      override def translateAddressProps = classOf[AddressTranslatorFailActor]
    }
  }

where AddressService acctor is defined as follows:

class AddressServiceActor extends Actor with ActorLogging {

  def translateAddressProps: Class[_<:AddressTranslatorActor] = classOf[AddressTranslatorActor]
...

But still getting the "fail" message un-handeled

Upvotes: 1

Views: 5940

Answers (1)

LynxZh
LynxZh

Reputation: 835

Unless you set the supervisor stratergy to restart. Otherwise, you can't restart an actor with a poison pill or kill. If the actor is terminated somehow without supervisor strategy set to restart, the only way is to create a new one.

Upvotes: 3

Related Questions