Skip to content

Commit ace0aab

Browse files
committed
Added FailureParent example
1 parent 72b250b commit ace0aab

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.reactivedesignpatterns.chapter4
2+
3+
import org.scalatest._
4+
import akka.actor._
5+
import akka.actor.SupervisorStrategy.Stop
6+
import akka.testkit.TestProbe
7+
8+
class FailureParentSpec extends WordSpec with Matchers with BeforeAndAfterAll {
9+
"Using a FailureParent" must {
10+
"Result in failures being collected and returned" in {
11+
implicit val system = ActorSystem()
12+
val failures = TestProbe()
13+
val failureParent = system.actorOf(Props(new FailureParent(failures.ref)))
14+
failureParent ! TestFailureParentMessage
15+
failures.expectMsgType[NullPointerException]
16+
}
17+
}
18+
}
19+
20+
case object TestFailureParentMessage
21+
22+
class FailureParent(failures: ActorRef) extends Actor {
23+
val props = Props[MyFailureParentActor]
24+
val child = context.actorOf(props, "child")
25+
override val supervisorStrategy = OneForOneStrategy() {
26+
case f => failures ! f; Stop
27+
}
28+
def receive = {
29+
case msg => child forward msg
30+
}
31+
}
32+
33+
class MyFailureParentActor extends Actor {
34+
def receive = {
35+
case _ => throw new NullPointerException
36+
}
37+
}

0 commit comments

Comments
 (0)