This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentActor extends Actor { | |
def receive: Actor.Receive = { | |
case msg @ _ => { | |
println(s"Received msg, delegating work to a child actor") | |
val childActor = context.actorOf(Props[ChildActor]) | |
childActor ! "Go and do something important!" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentActorTest extends TestKit(ActorSystem("TestSystem")) with FunSuiteLike { | |
test("Should delegate the important work to the client") { | |
val underTest = TestActorRef(new ParentActor) | |
underTest ! "Go do some work" | |
// how do i test this? | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentActor(childFactory: (ActorRefFactory) => ActorRef) extends Actor { | |
def receive: Actor.Receive = { | |
case msg @ _ => { | |
println(s"Received msg, delegating work to a child actor") | |
val childActor = childFactory(context) | |
childActor ! "Go and do something important!" | |
} | |
} | |
} |
childFactory: (ActorRefFactory) => ActorRef
This is then used by the ParentActor as follows:
val childActor = childFactory(context)
You can now inject a TestProbe for testing rather than using a real ActorFactorRef. For example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParentActorTest extends TestKit(ActorSystem("TestSystem")) with FunSuiteLike { | |
test("Should delegate the important work to the client") { | |
val testProbeForChild = TestProbe() | |
val underTest = TestActorRef(new ParentActor(_ => testProbeForChild.ref)) | |
underTest ! "Go do some work" | |
testProbeForChild.expectMsg("Go and do something important!") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test("Using a real child actor") { | |
val underTest = TestActorRef(new ParentActor(actorFactory => actorFactory.actorOf(Props[ChildActor]))) | |
underTest ! "Go do some work" | |
// Can't test this but shows how to crate a ParentActor in production code | |
} |
Full source code here.
1 comment:
Hi, nice job with this scenario. I have a question about parent actor: you can easily create an actor with "new", but how to create a parent actor ref? With actor instance I cannot use ! method.
Thanks
Post a Comment