Permalink
Browse files

Mock actor testing

  • Loading branch information...
1 parent 7c218ee commit cfe17be72814f1b5426592959f3fc09952a5b75a @janm399 committed Jul 7, 2012
@@ -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")
+ }
+}
@@ -8,6 +8,7 @@ class BombActor extends Actor {
protected def receive = {
case Bomb() =>
+ Thread.sleep(10)
sender ! Some("boom!")
}
@@ -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
+ }
+}
@@ -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)))
+ }
+}
@@ -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!"))
}
}
@@ -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.