I ended up changing my DoWorkActor like this:

class DoWorkActor(managerRef: Option[ActorRef] = None) extends Actor {
    lazy val managerSel = context.system.actorSelection("user/manager")

   def receive = {
       case "go" => {
              // do some work
             tellManager("success")
       }
   }

   def tellManager(msg: Any) = {
        managerRef match {
              case None => managerSel ! msg
              case _ => managerRef.get ! msg
        }
   }

Then I changed my test to extend TestKit and now when I create an instance 
of DoWorkActor, I pass in the "testActor" provided by the TestKit

reference: 
http://blog.matthieuguillermin.fr/2013/06/akka-testing-your-actors/

On Thursday, March 20, 2014 8:10:06 AM UTC-4, Craig Wickesser wrote:
>
> I am using Scala 2.9.2 and Akka 2.0.x. I have two actors:
>
> class DoWorkActor extends Actor {
>    val manager = context.system.ActorSelection("user/manager")
>
>    def receive = {
>        case "go" => {
>               // do some work
>              manager ! "success"
>        }
>    }
> }
>
> class ManagerActor extends Actor {
>   var successCount = 0
>
>   def receive = {
>        case "success" => {
>             successCount += 1
>        }
>        case "getSuccessCount" => {
>             sender ! successCount
>        }
>    }
> }
>
> I want to verify that the DoWorkActor sends a "success" to the 
> ManagerActor. I'm trying with the following test:
>
> class DoWorkActorSpec extends FunSpec {
>
>    val config = ConfigFactory.load()
>    implicit val actorSystem = ActorSystem("test", config)
>    implicit val timeout = Timeout(500)
>
>    val managerRef = TestActorRef(new ManagerActor)
>    actorSystem.actorOf(Props(managerRef.underlyingActor), name = "manager")
>
>    describe("#receive") {
>         it("should send a success message to the manager") {
>                 val doWorkActor = TestActorRef(new DoWorkActor)
>                 doWorkActor ! "go"
>
>                 val count = Await.result((managerRef ? "getSuccessCount"), 
> 5 seconds)
>                 assert(count === 1)
>         }
>    }
>
> Somtimes this test passes, but usually it fails. I put "println" 
> statements in the Manger actor and I can see that it usually gets the 
> "getSuccessCount" message before the "success" message. If I put in a 
> "Thread.sleep(100)" after sending the "go" message to the DoWorkActor, the 
> test passes.
>
> I thought by using TestActorRef's, things would be asynchronous but it 
> seems like they're not. Am I doing something wrong? Any suggestions?
>
> Thanks.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to