How to wait until the child actors are active?

Hi all

I would like to test my supervisor actor, that spawns two children. The problem is, the test does not wait, until the children of the supervisor actor get started.

I am using ActorTestKit , to start the supervisor as follows:

  private val onlineStoreTest = ActorTestKit("StoreTestOnline", withStoreOnlineConfig())

  implicit val system = onlineStoreTest.system.toUntyped
  implicit val materializer = ActorMaterializer()(onlineStoreTest.system)


  private val communicator = onlineStoreTest.createTestProbe[RpcCmd]("Communicator")
  private val trash = onlineStoreTest.createTestProbe[TrashTalk]("TrashTalk")
  private val onlineStore = onlineStoreTest.spawn(PersistenceSupervisor(Some(communicator.ref), trash.ref).create, "OnlineStoreActor")

and the test:

Given("a messages receives from KAFKA")
      val msg = Message("TEST-KAFKA", "TESTED", """{data: payload}""", KAFKA)
      When("write into the store")
      onlineStore ! SavePersistenceMessage(SaveMessage(msg))
      onlineStore ! SavePersistenceMessage(SaveMessage(msg))
      onlineStore ! SavePersistenceMessage(SaveMessage(msg))
      And("read the written messages from store")
      Then("it should redirect the messages to the communicator")
      communicator.receiveMessage(10.seconds) should be(SendMessage(msg))

The problem here is, the children actor in the PersistenceSupervisor are not ready at the time the test starts. So the test just walk through.

The question is, before the test get started, how to wait until the supervisor and children are completely ready.

Thanks

Depends on how the child actors are created and why they are not ready immediately. How would the real application handle that they are not ready?

Possible solution could be that the parent stash all incoming messages until children are ready. Another could be to send back a rejection message and let the requestor (the test in this case) retry.

1 Like

@patriknw Thanks so much for your response. I stash all incoming message and when the child is ready, then unstash it.

Thanks