rewrite FlatSpec tests using WordSpec #24186

This commit is contained in:
Łukasz Drygała 2018-01-03 12:51:27 +01:00 committed by Johan Andrén
parent 6d6e96179d
commit 0c0bf91661
4 changed files with 145 additions and 135 deletions

View file

@ -4,12 +4,13 @@
package docs.actor
import language.postfixOps
import akka.actor.{ ActorSystem, ActorRef, Props, Terminated }
import akka.actor.{ ActorRef, ActorSystem, Props, Terminated }
import FaultHandlingDocSpec._
import org.scalatest.{ WordSpec, WordSpecLike }
//#testkit
import com.typesafe.config.{ Config, ConfigFactory }
import org.scalatest.{ FlatSpecLike, Matchers, BeforeAndAfterAll }
import org.scalatest.{ Matchers, BeforeAndAfterAll }
import akka.testkit.{ TestActors, TestKit, ImplicitSender, EventFilter }
//#testkit
@ -100,7 +101,7 @@ object FaultHandlingDocSpec {
}
//#testkit
class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system)
with ImplicitSender with FlatSpecLike with Matchers with BeforeAndAfterAll {
with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll {
def this() = this(ActorSystem(
"FaultHandlingDocSpec",
@ -115,70 +116,72 @@ class FaultHandlingDocSpec(_system: ActorSystem) extends TestKit(_system)
TestKit.shutdownActorSystem(system)
}
"A supervisor" must "apply the chosen strategy for its child" in {
//#testkit
"A supervisor" must {
"apply the chosen strategy for its child" in {
//#testkit
//#create
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
//#create
val supervisor = system.actorOf(Props[Supervisor], "supervisor")
supervisor ! Props[Child]
val child = expectMsgType[ActorRef] // retrieve answer from TestKits testActor
//#create
EventFilter.warning(occurrences = 1) intercept {
//#resume
child ! 42 // set state to 42
child ! "get"
expectMsg(42)
supervisor ! Props[Child]
val child = expectMsgType[ActorRef] // retrieve answer from TestKits 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 ()
child ! new ArithmeticException // crash it
child ! "get"
expectMsg(42)
//#resume
}
//#escalate-kill
//#escalate-restart
val supervisor2 = system.actorOf(Props[Supervisor2], "supervisor2")
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)
supervisor2 ! Props[Child]
val child3 = expectMsgType[ActorRef]
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")
child3 ! 23
child3 ! "get"
expectMsg(23)
supervisor2 ! Props[Child]
val child3 = expectMsgType[ActorRef]
child3 ! new Exception("CRASH")
child3 ! "get"
expectMsg(0)
//#escalate-restart
child3 ! 23
child3 ! "get"
expectMsg(23)
child3 ! new Exception("CRASH")
child3 ! "get"
expectMsg(0)
//#escalate-restart
}
//#testkit
// code here
}
//#testkit
// code here
}
}
//#testkit