Skip to content

Commit

Permalink
Mock actor testing
Browse files Browse the repository at this point in the history
  • Loading branch information
janm399 committed Jul 7, 2012
1 parent 7c218ee commit cfe17be
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.cakesolutions.akkapatterns.core.application

import akka.actor.Actor

case class GetAddresses(person: String)

class AddressBookActor extends Actor {
protected def receive = {
case GetAddresses(person) =>
sender ! List(person + "@cakesolutions.net", person + "@gmail.com", person + "@hotmail.com")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BombActor extends Actor {

protected def receive = {
case Bomb() =>
Thread.sleep(10)
sender ! Some("boom!")

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.cakesolutions.akkapatterns.core.application

import akka.actor.Actor

case class Message(address: String, body: String)

class MessageSenderActor extends Actor {
protected def receive = {
case Message(address, body) =>
// do something
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.cakesolutions.akkapatterns.core.application

import akka.actor.Actor
import akka.pattern.ask
import akka.util.Timeout

case class Notify(person: String)

class NotificationActor extends Actor {
implicit val timeout = Timeout(1000)
def messageSender = context.actorFor("/user/application/messageSender")
def addressBook = context.actorFor("/user/application/addressBook")

protected def receive = {
case Notify(person) =>
val addrs = (addressBook ? GetAddresses(person)).mapTo[List[String]]
// do some work to construct the body and then
val body = "Dummy body"

// notify all
addrs.foreach(_.foreach(messageSender ! Message(_, body)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class BombActorSpec extends TestKit(ActorSystem()) with Specification with Impli
"flooding the actor's queue" in {
val bombActor = system actorFor "user/application/bomb"

within(Duration("2s")) {
within(Duration("3s")) {
// 10 * 10ms = 1000msgs/s
for (i <- 0 to 100) {
bombActor ! Bomb()
expectMsg(Some("boom!"))
expectMsg(Duration("20ms"), Some("boom!"))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cakesolutions.akkapatterns.core.application

import org.specs2.mutable.Specification
import akka.actor.ActorSystem
import akka.testkit.{TestActorRef, TestKit, ImplicitSender}

class NotificationActorSpec extends TestKit(ActorSystem()) with Specification with ImplicitSender {

"x" in {
val n = TestActorRef[NotificationActor]

val person = "Jan Machacek"
n ! Notify(person)

// D_expectAndReply(GetAddress(person), List("A"))
// D_expectMsgAllOf(Message("A", "Dummy body"))

success
}

}

0 comments on commit cfe17be

Please sign in to comment.