Manuel Bernhardt
Manuel Bernhardt

Reputation: 3144

Actor addresses and tests

I'm building up an actor hierarchy in which some actors reply to messages they get directly to a well-known actor (fixed name). I.e. these actors (far-removed in the hierarchy) obtain an actorRef via context.actorFor("akka://...").

I.e. for example, I have an "orchestrating" actor:

system.actorOf(Props[OrchestratingActor], name = "orchestrator")

which will then have an address of the kind akka://application/user/orchestrator

and someplace else, a random worker that received a message and wants to talk to the orchestrator:

class RandomWorker extends Actor {
  def theOrchestrator = context.actorFor("akka://application/user/orchestrator")
  def receive = {
    case Foo =>
      theOrchestrator ! "Bar"
  }
}

Now, I'd like to test those actors and am wondering about how to deal with those addresses: when unit-testing an actor (e.g. using TestActorRef), how do I go about checking what's being sent to the remote address? One idea would be to (when possible) provide the address to the well-known actor via the constructor, and pass in the address of a TestActor to see what's being received. However I wonder if there isn't a way to "impersonate" a given address in the test, esp. in situation where the addresses aren't simple.

In other words I'd like to test the behavior of the actor (will it indeed have sent "Bar" to the orchestrator upon receiving a Foo

Upvotes: 0

Views: 105

Answers (2)

Roland Kuhn
Roland Kuhn

Reputation: 15472

My recommendation would be to avoid using look-ups of actors for the purpose you show. It is rather a tool for the setup phase of your application, when wiring it all together. But even then most actors’ supervisors will know the dependencies of their children without using look-ups.

Within a local actor system all ActorRefs can be injected top to bottom (using constructor arguments or introduction messages). Look-ups are most useful when introducing remote systems with each other.

Upvotes: 2

agilesteel
agilesteel

Reputation: 16859

There is nothing wrong with injecting the address via the constructor. Let it me know if you need any details, because at the moment I don't know how to make this more clear, since you basically answered your own question. And btw, I don't know, which Akka version you are using, but actorFor has been recently deprecated in favor of ActorSelection, and there are good reasons for this.

Upvotes: 1

Related Questions