pygumby
pygumby

Reputation: 6790

How to stop all actors and wait for them to terminate?

I am implementing unit tests for my Akka project. To avoid InvalidActorNameExceptions and the like, I want all actors that were created within one unit test to be stopped before the next unit test is being run. So, for each actor created within a unit test, I call _system.stop(someActorRef) at the end of it. However, it takes some time for an actor to actually be stopped, and unfortunately, the next unit test usually starts running before the actors that were created within the previous one are actually gone. And since there is neither a Future that is being returned by the stop method, nor an awaitStop method available, I really don't know how to solve this. Currently I call Thread.sleep(1000) at the end of each unit test and hope all actors are dead by then, but, obviously, I cannot stay this way. :D

I'd appreciate any hint!

Upvotes: 0

Views: 332

Answers (1)

kardapoltsev
kardapoltsev

Reputation: 1088

You could try this at the end of your test:

val probe = TestProbe()
probe.watch(someActorRef)
system.stop(someActorRef)
probe.expectMsgType[Terminated]
//another way
//probe.expectMsgPF() {
//  case Terminated(someActorRef) =>
//}

Upvotes: 1

Related Questions