lundi 17 octobre 2016

How to test Akka actor that spawn child actors at runtime?

Following my other question, I have an UDP server actor as follow:

class Listener(addr: InetSocketAddress) extends Actor {
  import context.system
  IO(Udp) ! Udp.Bind(self, addr)

  def spawnChild(remote): ActorRef = {
    //Check if child already exist
    context.actorOf(Props[Worker])
  }

  def receive = {
    case Udp.Bound(local) =>
      context.become(ready(sender()))
  }

  def ready(socket: ActorRef): Receive = {
    case Udp.Received(data, remote) =>
      val worker = spawnChild(remote)
      worker ! data // forward data directly to child
    case Udp.Unbind  => socket ! Udp.Unbind
    case Udp.Unbound => context.stop(self)
  }
}

I am creating child actors based on where the data is sending from. The reason for this is to keep some internal state by child actor. Internal states include last connection time, total number of packet sent, etc

I want to setup TestProbes to test that

  • data from remoteA is forwarded to TestProbeA
  • data from remoteB is forwarded to TestProbeB
  • data from remoteA is not forwarded to TestProbeB

I have read the section Using multiple probes. However in my situation, it is the parent actor who is responsible of creating children.

How should I write my specs in this case? Or perhaps, how should I refactor my code to be more test-friendly?

Aucun commentaire:

Enregistrer un commentaire