Skip to content

Commit 72b250b

Browse files
committed
Added StepParent example
1 parent 7bc92bf commit 72b250b

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
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.Restart
6+
import akka.testkit.TestProbe
7+
8+
class StepParentSpec extends WordSpec with Matchers with BeforeAndAfterAll {
9+
"An actor that throws an exception" must {
10+
"Result in the supervisor returning a reference to that actor" in {
11+
implicit val system = ActorSystem()
12+
val testProbe = TestProbe()
13+
val parent = system.actorOf(Props(new StepParent(testProbe.ref)), "stepParent")
14+
parent.tell(Props[MyActor], testProbe.ref)
15+
val child = testProbe.expectMsgType[ActorRef]
16+
child ! TestMessage
17+
}
18+
}
19+
}
20+
21+
case object TestMessage
22+
23+
class StepParent(target: ActorRef) extends Actor {
24+
override val supervisorStrategy = OneForOneStrategy() {
25+
case thr: Throwable => target ! thr; Restart
26+
}
27+
def receive = {
28+
case p: Props =>
29+
sender ! context.actorOf(p, "child")
30+
}
31+
}
32+
33+
class MyActor extends Actor {
34+
def receive = {
35+
case _ => throw new NullPointerException
36+
}
37+
}

0 commit comments

Comments
 (0)