From c0867e1308ad49461d095066f7d20803cf724ba7 Mon Sep 17 00:00:00 2001 From: sangche Date: Mon, 22 Dec 2014 19:55:05 -0800 Subject: [PATCH 1/2] =doc #16459 change confusing FaultHandlingDocSpec in Fault Tolerance Document. --- .../docs/actor/FaultHandlingDocSpec.scala | 147 +++++++++--------- 1 file changed, 77 insertions(+), 70 deletions(-) diff --git a/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala b/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala index 1955755ec6..cdd4371854 100644 --- a/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala +++ b/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala @@ -6,8 +6,10 @@ package docs.actor import language.postfixOps //#testkit -import akka.testkit.{ AkkaSpec, ImplicitSender, EventFilter } -import akka.actor.{ ActorRef, Props, Terminated } +import akka.testkit.{ TestKit, ImplicitSender, EventFilter } +import akka.actor.{ ActorRef, Props, ActorSystem, Terminated } +import org.scalatest._ +import com.typesafe.config.{ Config, ConfigFactory } //#testkit object FaultHandlingDocSpec { @@ -91,83 +93,88 @@ object FaultHandlingDocSpec { } } //#child + + val testConf: Config = ConfigFactory.parseString(""" + akka { + loggers = ["akka.testkit.TestEventListener"] + } + """) } +import FaultHandlingDocSpec._ //#testkit -class FaultHandlingDocSpec extends AkkaSpec with ImplicitSender { +class FaultHandlingDocSpec extends TestKit(ActorSystem("FaultHandlingDocSpec", testConf)) + with FlatSpecLike with BeforeAndAfterAll with ImplicitSender { + + override def afterAll() { + system.terminate() + } //#testkit - import FaultHandlingDocSpec._ - //#testkit + "A supervisor" must "apply the chosen strategy for its child" in { + //#testkit - "A supervisor" must { + //#create + val supervisor = system.actorOf(Props[Supervisor], "supervisor") - "apply the chosen strategy for its child" in { - //#testkit + supervisor ! Props[Child] + 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) - //#create - val supervisor = system.actorOf(Props[Supervisor], "supervisor") - - supervisor ! Props[Child] - 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) - - 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 + 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 } } //#testkit From a18c1bbafb0a64b55eae4993820ccba4cbbbb608 Mon Sep 17 00:00:00 2001 From: Roland Kuhn Date: Tue, 27 Jan 2015 16:25:13 +0100 Subject: [PATCH 2/2] fix up wrong includecode comments in FaultHandlingDocSpec --- .../rst/scala/code/docs/actor/FaultHandlingDocSpec.scala | 6 +----- akka-docs/rst/scala/fault-tolerance.rst | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala b/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala index cdd4371854..f8025e9854 100644 --- a/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala +++ b/akka-docs/rst/scala/code/docs/actor/FaultHandlingDocSpec.scala @@ -18,8 +18,6 @@ object FaultHandlingDocSpec { import akka.actor.Actor //#child - //#supervisor - //#supervisor class Supervisor extends Actor { //#strategy import akka.actor.OneForOneStrategy @@ -81,7 +79,6 @@ object FaultHandlingDocSpec { def receive = Actor.emptyBehavior } - //#supervisor //#child class Child extends Actor { @@ -109,10 +106,9 @@ class FaultHandlingDocSpec extends TestKit(ActorSystem("FaultHandlingDocSpec", t override def afterAll() { system.terminate() } - //#testkit "A supervisor" must "apply the chosen strategy for its child" in { - //#testkit + //#testkit //#create val supervisor = system.actorOf(Props[Supervisor], "supervisor") diff --git a/akka-docs/rst/scala/fault-tolerance.rst b/akka-docs/rst/scala/fault-tolerance.rst index 85d6156010..42462b33dc 100644 --- a/akka-docs/rst/scala/fault-tolerance.rst +++ b/akka-docs/rst/scala/fault-tolerance.rst @@ -127,8 +127,8 @@ This supervisor will be used to create a child, with which we can experiment: :include: child The test is easier by using the utilities described in :ref:`akka-testkit`, -where ``AkkaSpec`` is a convenient mixture of ``TestKit with WordSpec with -MustMatchers`` +where ``AkkaSpec`` is a convenient mixture of +``TestKit with WordSpec with MustMatchers`` .. includecode:: code/docs/actor/FaultHandlingDocSpec.scala :include: testkit