2015-06-01 19:03:00 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2015 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-12 14:56:28 +02:00
|
|
|
package akka.pattern
|
2015-07-07 16:28:17 +02:00
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import akka.testkit._
|
2015-11-16 17:42:24 +01:00
|
|
|
import scala.util.control.NoStackTrace
|
|
|
|
|
import akka.actor.SupervisorStrategy
|
2015-06-01 19:03:00 +02:00
|
|
|
|
|
|
|
|
object BackoffSupervisorSpec {
|
2015-11-16 17:42:24 +01:00
|
|
|
|
|
|
|
|
class TestException extends RuntimeException with NoStackTrace
|
|
|
|
|
|
2015-06-01 19:03:00 +02:00
|
|
|
object Child {
|
|
|
|
|
def props(probe: ActorRef): Props =
|
|
|
|
|
Props(new Child(probe))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Child(probe: ActorRef) extends Actor {
|
|
|
|
|
def receive = {
|
2015-11-16 17:42:24 +01:00
|
|
|
case "boom" ⇒ throw new TestException
|
|
|
|
|
case msg ⇒ probe ! msg
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BackoffSupervisorSpec extends AkkaSpec with ImplicitSender {
|
|
|
|
|
import BackoffSupervisorSpec._
|
|
|
|
|
|
|
|
|
|
"BackoffSupervisor" must {
|
|
|
|
|
"start child again when it stops" in {
|
|
|
|
|
val supervisor = system.actorOf(
|
|
|
|
|
BackoffSupervisor.props(Child.props(testActor), "c1", 100.millis, 3.seconds, 0.2))
|
|
|
|
|
supervisor ! BackoffSupervisor.GetCurrentChild
|
|
|
|
|
val c1 = expectMsgType[BackoffSupervisor.CurrentChild].ref.get
|
|
|
|
|
watch(c1)
|
|
|
|
|
c1 ! PoisonPill
|
|
|
|
|
expectTerminated(c1)
|
|
|
|
|
awaitAssert {
|
|
|
|
|
supervisor ! BackoffSupervisor.GetCurrentChild
|
|
|
|
|
// new instance
|
|
|
|
|
expectMsgType[BackoffSupervisor.CurrentChild].ref.get should !==(c1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"forward messages to the child" in {
|
|
|
|
|
val supervisor = system.actorOf(
|
|
|
|
|
BackoffSupervisor.props(Child.props(testActor), "c2", 100.millis, 3.seconds, 0.2))
|
|
|
|
|
supervisor ! "hello"
|
|
|
|
|
expectMsg("hello")
|
|
|
|
|
}
|
2015-11-16 17:42:24 +01:00
|
|
|
|
|
|
|
|
"support custom supervision decider" in {
|
|
|
|
|
val supervisor = system.actorOf(
|
|
|
|
|
BackoffSupervisor.propsWithSupervisorStrategy(Child.props(testActor), "c1", 100.millis, 3.seconds, 0.2,
|
|
|
|
|
OneForOneStrategy() {
|
|
|
|
|
case _: TestException ⇒ SupervisorStrategy.Stop
|
|
|
|
|
}))
|
|
|
|
|
supervisor ! BackoffSupervisor.GetCurrentChild
|
|
|
|
|
val c1 = expectMsgType[BackoffSupervisor.CurrentChild].ref.get
|
|
|
|
|
watch(c1)
|
|
|
|
|
c1 ! "boom"
|
|
|
|
|
expectTerminated(c1)
|
|
|
|
|
awaitAssert {
|
|
|
|
|
supervisor ! BackoffSupervisor.GetCurrentChild
|
|
|
|
|
// new instance
|
|
|
|
|
expectMsgType[BackoffSupervisor.CurrentChild].ref.get should !==(c1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-01 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
}
|