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!" | |
} | |
} | |
} |
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 | |
} |
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 ClassThatReturnsFutures() { | |
def goDoSomething() : Future[String] = { | |
future { | |
println("I best do this asynchronously") | |
"The result" | |
} | |
} | |
} |
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("Test a asynchronous method synchronously") { | |
val underTest = new ClassThatReturnsFutures() | |
val futureResult = underTest.goDoSomething() | |
futureResult onComplete { | |
case Success(value) => value should equal("Something") | |
case Failure(exp) => fail(exp) | |
} | |
} |
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("Test a asynchronous method synchronously - whenReady") { | |
val underTest = new ClassThatReturnsFutures() | |
val futureResult = underTest.goDoSomething() | |
whenReady(futureResult) { result => | |
result should equal("The result") | |
} | |
} |
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 ChildActor extends Actor { | |
def receive: Actor.Receive = { | |
case "Do something" => { | |
// Do something important | |
context.parent ! "I was told to do something and I did it" | |
} | |
} | |
} |
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 ChildActorTest extends TestKit(ActorSystem("ChildActorTest")) with FunSuiteLike with ShouldMatchers { | |
test("Should inform parent when told to do something") { | |
val parent = TestProbe() | |
val underTest = TestActorRef(Props[ChildActor], parent.ref, "ChildActor") | |
underTest ! "Do something" | |
parent.expectMsg("I was told to do something and I did it") | |
} | |
} |
OlderNewer