Capacytron
Capacytron

Reputation: 3729

akka and testkit. Can't get actor children

I have an actor with receive method:

def receive: Actor.Receive = {
    case Tick =>
        val child = context.system.actorOf(...) // create actor
        context.watch(child)
        child ! something

    case AskRunningJobs =>
      log.info(s"My children: ${context.children.toList.length}")
      log.info(s"My children: ${context.children.toList.map(_.path.toSerializationFormat).mkString(" ||| ")}")
      sender ! RunningJobs(context.children.toList.length)

    case unknown =>
      log.warning(s"unknown message: $unknown")
  }

I have detailed log output and I can clearly see that children are created and they are running. But

context.children.toList.length

is always zero. Why? I'm running my actor using TestKit.

Upvotes: 0

Views: 181

Answers (1)

Stefano Bonetti
Stefano Bonetti

Reputation: 9023

By creating children this way

val child = context.system.actorOf(...) // create actor

you make the created actors children of the guardian (i.e. you lose the context). Only your top-level actors are supposed to be created this way.

To make them children of your actor, you need to use

val child = context.actorOf(...) // create actor

instead. More info on actor creation can be found in the docs.

Upvotes: 2

Related Questions