Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving an ActorPath for a testProbe

I have some code that sends a message to an actor using it's ActorPath not ActorRef

system.actorSelection("/user/myActor") ! "a message"

I need to make sure that this message is really sent. So I need in my tests to create a testProbe (or something similar) that lives in this actor path, so that it receives those message.

val probe = TestProbe()
val system.actorOf(probe.ref, "myActor") // ??? I want to give my probe name and path
like image 864
tabdulradi Avatar asked Jan 22 '14 14:01

tabdulradi


3 Answers

I figured out a non elegant solution.
I defined a helper actor called "ForwardActor"

class ForwardActor(to: ActorRef) extends Actor {
    def receive = {
        case x => to.forward(x)
    }
}

then in my test case

 val probe = TestProbe() 
 system.actorOf(Props(new ForwardActor(probe.ref)), "myActor")
 probe.expectMsg(...)
like image 198
tabdulradi Avatar answered Oct 12 '22 21:10

tabdulradi


Hard coding actor paths is likely to make you unhappy later, wherefore you should take the difficulty of testing it as a design hint. I would recommend using ActorRef as a first choice, and only if that cannot be made to work fall back to ActorPath, but then keep it flexible by telling the actor which path to use (in a message or via the constructor).

like image 6
Roland Kuhn Avatar answered Oct 12 '22 21:10

Roland Kuhn


I have developed a small library/DSL for dealing with such cases (it also allows to create more complex hierarchies and listen for messages for certain elements of path) https://github.com/stanislav-chetvertkov/spy-tree. The above case can be dealt like the following:

import com.github.spytree.ActorListenersDSL._

val myActorProbe = TestProbe()
("myActor" replyTo myActorProbe.ref).materialize

context.actorSelection("/user/myActor") ! "a message"

val response = myActorProbe.expectResponse[String]
response.message shouldBe "a message"
like image 1
stanislav.chetvertkov Avatar answered Oct 12 '22 19:10

stanislav.chetvertkov