Skip to content

Instantly share code, notes, and snippets.

View chbatey's full-sized avatar
🐯
WFH

Christopher Batey chbatey

🐯
WFH
View GitHub Profile
@chbatey
chbatey / ParentActor.scala
Created February 5, 2014 22:25
Akka: Example parent sending message to child
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!"
}
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:29
Akka: How do I test parent sending msg to child?
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?
}
}
@chbatey
chbatey / ParentActor.scala
Created February 5, 2014 22:34
Akka: Using a ActorRefFactory to make it easier to test
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!"
}
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:47
Testing a message sent to a child
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!")
}
}
@chbatey
chbatey / ParentActorTest.scala
Created February 5, 2014 22:51
Akka: Using actor factory ref in real code
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
}
@chbatey
chbatey / ClassThatReturnsFutures.scala
Created February 6, 2014 22:34
Scala: Example class that returns a future
class ClassThatReturnsFutures() {
def goDoSomething() : Future[String] = {
future {
println("I best do this asynchronously")
"The result"
}
}
}
@chbatey
chbatey / ClassThatReturnsFuturesTest.scala
Created February 6, 2014 22:36
Scala: Not a good way to test asynchronous code
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)
}
}
@chbatey
chbatey / ClassThatReturnsFuturesTest.scala
Created February 6, 2014 22:40
ScalaTest: Using whenReady to test Futures
test("Test a asynchronous method synchronously - whenReady") {
val underTest = new ClassThatReturnsFutures()
val futureResult = underTest.goDoSomething()
whenReady(futureResult) { result =>
result should equal("The result")
}
}
@chbatey
chbatey / ChildActor.scala
Created February 11, 2014 02:39
Akka: Example actor that sends a message to its parent
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"
}
}
}
@chbatey
chbatey / ChildActorTest.scala
Created February 11, 2014 02:40
Akka: Testing a message sent to an actor's parent
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")
}
}