Merge branch 'tmp'
This commit is contained in:
commit
9463c9ff6c
2 changed files with 78 additions and 75 deletions
|
|
@ -6,8 +6,10 @@ package docs.actor
|
||||||
import language.postfixOps
|
import language.postfixOps
|
||||||
|
|
||||||
//#testkit
|
//#testkit
|
||||||
import akka.testkit.{ AkkaSpec, ImplicitSender, EventFilter }
|
import akka.testkit.{ TestKit, ImplicitSender, EventFilter }
|
||||||
import akka.actor.{ ActorRef, Props, Terminated }
|
import akka.actor.{ ActorRef, Props, ActorSystem, Terminated }
|
||||||
|
import org.scalatest._
|
||||||
|
import com.typesafe.config.{ Config, ConfigFactory }
|
||||||
|
|
||||||
//#testkit
|
//#testkit
|
||||||
object FaultHandlingDocSpec {
|
object FaultHandlingDocSpec {
|
||||||
|
|
@ -16,8 +18,6 @@ object FaultHandlingDocSpec {
|
||||||
import akka.actor.Actor
|
import akka.actor.Actor
|
||||||
|
|
||||||
//#child
|
//#child
|
||||||
//#supervisor
|
|
||||||
//#supervisor
|
|
||||||
class Supervisor extends Actor {
|
class Supervisor extends Actor {
|
||||||
//#strategy
|
//#strategy
|
||||||
import akka.actor.OneForOneStrategy
|
import akka.actor.OneForOneStrategy
|
||||||
|
|
@ -79,7 +79,6 @@ object FaultHandlingDocSpec {
|
||||||
|
|
||||||
def receive = Actor.emptyBehavior
|
def receive = Actor.emptyBehavior
|
||||||
}
|
}
|
||||||
//#supervisor
|
|
||||||
|
|
||||||
//#child
|
//#child
|
||||||
class Child extends Actor {
|
class Child extends Actor {
|
||||||
|
|
@ -91,83 +90,87 @@ object FaultHandlingDocSpec {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#child
|
//#child
|
||||||
|
|
||||||
|
val testConf: Config = ConfigFactory.parseString("""
|
||||||
|
akka {
|
||||||
|
loggers = ["akka.testkit.TestEventListener"]
|
||||||
|
}
|
||||||
|
""")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import FaultHandlingDocSpec._
|
||||||
//#testkit
|
//#testkit
|
||||||
class FaultHandlingDocSpec extends AkkaSpec with ImplicitSender {
|
class FaultHandlingDocSpec extends TestKit(ActorSystem("FaultHandlingDocSpec", testConf))
|
||||||
//#testkit
|
with FlatSpecLike with BeforeAndAfterAll with ImplicitSender {
|
||||||
|
|
||||||
import FaultHandlingDocSpec._
|
override def afterAll() {
|
||||||
//#testkit
|
system.terminate()
|
||||||
|
}
|
||||||
|
|
||||||
"A supervisor" must {
|
"A supervisor" must "apply the chosen strategy for its child" in {
|
||||||
|
//#testkit
|
||||||
|
|
||||||
"apply the chosen strategy for its child" in {
|
//#create
|
||||||
//#testkit
|
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
|
||||||
|
|
||||||
//#create
|
supervisor ! Props[Child]
|
||||||
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
|
val child = expectMsgType[ActorRef] // retrieve answer from TestKit’s testActor
|
||||||
|
//#create
|
||||||
|
EventFilter.warning(occurrences = 1) intercept {
|
||||||
|
//#resume
|
||||||
|
child ! 42 // set state to 42
|
||||||
|
child ! "get"
|
||||||
|
expectMsg(42)
|
||||||
|
|
||||||
supervisor ! Props[Child]
|
child ! new ArithmeticException // crash it
|
||||||
val child = expectMsgType[ActorRef] // retrieve answer from TestKit’s testActor
|
child ! "get"
|
||||||
//#create
|
expectMsg(42)
|
||||||
EventFilter.warning(occurrences = 1) intercept {
|
//#resume
|
||||||
//#resume
|
|
||||||
child ! 42 // set state to 42
|
|
||||||
child ! "get"
|
|
||||||
expectMsg(42)
|
|
||||||
|
|
||||||
child ! new ArithmeticException // crash it
|
|
||||||
child ! "get"
|
|
||||||
expectMsg(42)
|
|
||||||
//#resume
|
|
||||||
}
|
|
||||||
EventFilter[NullPointerException](occurrences = 1) intercept {
|
|
||||||
//#restart
|
|
||||||
child ! new NullPointerException // crash it harder
|
|
||||||
child ! "get"
|
|
||||||
expectMsg(0)
|
|
||||||
//#restart
|
|
||||||
}
|
|
||||||
EventFilter[IllegalArgumentException](occurrences = 1) intercept {
|
|
||||||
//#stop
|
|
||||||
watch(child) // have testActor watch “child”
|
|
||||||
child ! new IllegalArgumentException // break it
|
|
||||||
expectMsgPF() { case Terminated(`child`) => () }
|
|
||||||
//#stop
|
|
||||||
}
|
|
||||||
EventFilter[Exception]("CRASH", occurrences = 2) intercept {
|
|
||||||
//#escalate-kill
|
|
||||||
supervisor ! Props[Child] // create new child
|
|
||||||
val child2 = expectMsgType[ActorRef]
|
|
||||||
|
|
||||||
watch(child2)
|
|
||||||
child2 ! "get" // verify it is alive
|
|
||||||
expectMsg(0)
|
|
||||||
|
|
||||||
child2 ! new Exception("CRASH") // escalate failure
|
|
||||||
expectMsgPF() {
|
|
||||||
case t @ Terminated(`child2`) if t.existenceConfirmed => ()
|
|
||||||
}
|
|
||||||
//#escalate-kill
|
|
||||||
//#escalate-restart
|
|
||||||
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
|
|
||||||
|
|
||||||
supervisor2 ! Props[Child]
|
|
||||||
val child3 = expectMsgType[ActorRef]
|
|
||||||
|
|
||||||
child3 ! 23
|
|
||||||
child3 ! "get"
|
|
||||||
expectMsg(23)
|
|
||||||
|
|
||||||
child3 ! new Exception("CRASH")
|
|
||||||
child3 ! "get"
|
|
||||||
expectMsg(0)
|
|
||||||
//#escalate-restart
|
|
||||||
}
|
|
||||||
//#testkit
|
|
||||||
// code here
|
|
||||||
}
|
}
|
||||||
|
EventFilter[NullPointerException](occurrences = 1) intercept {
|
||||||
|
//#restart
|
||||||
|
child ! new NullPointerException // crash it harder
|
||||||
|
child ! "get"
|
||||||
|
expectMsg(0)
|
||||||
|
//#restart
|
||||||
|
}
|
||||||
|
EventFilter[IllegalArgumentException](occurrences = 1) intercept {
|
||||||
|
//#stop
|
||||||
|
watch(child) // have testActor watch “child”
|
||||||
|
child ! new IllegalArgumentException // break it
|
||||||
|
expectMsgPF() { case Terminated(`child`) => () }
|
||||||
|
//#stop
|
||||||
|
}
|
||||||
|
EventFilter[Exception]("CRASH", occurrences = 2) intercept {
|
||||||
|
//#escalate-kill
|
||||||
|
supervisor ! Props[Child] // create new child
|
||||||
|
val child2 = expectMsgType[ActorRef]
|
||||||
|
watch(child2)
|
||||||
|
child2 ! "get" // verify it is alive
|
||||||
|
expectMsg(0)
|
||||||
|
|
||||||
|
child2 ! new Exception("CRASH") // escalate failure
|
||||||
|
expectMsgPF() {
|
||||||
|
case t @ Terminated(`child2`) if t.existenceConfirmed => ()
|
||||||
|
}
|
||||||
|
//#escalate-kill
|
||||||
|
//#escalate-restart
|
||||||
|
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
|
||||||
|
|
||||||
|
supervisor2 ! Props[Child]
|
||||||
|
val child3 = expectMsgType[ActorRef]
|
||||||
|
|
||||||
|
child3 ! 23
|
||||||
|
child3 ! "get"
|
||||||
|
expectMsg(23)
|
||||||
|
|
||||||
|
child3 ! new Exception("CRASH")
|
||||||
|
child3 ! "get"
|
||||||
|
expectMsg(0)
|
||||||
|
//#escalate-restart
|
||||||
|
}
|
||||||
|
//#testkit
|
||||||
|
// code here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#testkit
|
//#testkit
|
||||||
|
|
|
||||||
|
|
@ -127,8 +127,8 @@ This supervisor will be used to create a child, with which we can experiment:
|
||||||
:include: child
|
:include: child
|
||||||
|
|
||||||
The test is easier by using the utilities described in :ref:`akka-testkit`,
|
The test is easier by using the utilities described in :ref:`akka-testkit`,
|
||||||
where ``AkkaSpec`` is a convenient mixture of ``TestKit with WordSpec with
|
where ``AkkaSpec`` is a convenient mixture of
|
||||||
MustMatchers``
|
``TestKit with WordSpec with MustMatchers``
|
||||||
|
|
||||||
.. includecode:: code/docs/actor/FaultHandlingDocSpec.scala
|
.. includecode:: code/docs/actor/FaultHandlingDocSpec.scala
|
||||||
:include: testkit
|
:include: testkit
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue