0
votes

my actor runs on default akka dispatcher, which then calls a method which returns a future. I have configured different executioncontexts for all futures to run (since they are blocking(due to db calls) and to keep actors dispatcher dedicated to non blocking actors only. Wondering if this code can be tested (continue using two execution contexts etc) using Akka Testkit? If so what would be the way to configure a test so Actor runs on default dispatcher and futures can find "custom-dispatcher" as well for them to run? Obviously currently test throws following.

Caused by: akka.ConfigurationException: Dispatcher [custom-dispatcher] not configured
2
Does not the blocking { ... } construct ensure that code is properly dispatched to available threads? Otherwise, I miss the need for your question.Bob Dalgleish
@BobDalgleish : So if I continue using context.dispatcher even for blocking futures it should spawn new workers on that akka's dispatcher and never needing a separate exectionContext in this particular scenario. Is that right?Vikas Pandya
of course that comment was in regard to using blocking {..} with akka's context.dispatcherVikas Pandya

2 Answers

0
votes

When you create an Akka Testkit's TestActorRef for an Actor it will use PinnedDispatcher except you've specified a different one in Actor's Props and passed that Props when creating the TestActorRef.

The exception "Dispatcher [custom-dispatcher] not configured" may mean that you are using different Akka config for your tests in which no dispatcher with name [custom-dispatcher] configured.

0
votes

create a file application.conf in your test/resources directory

my-custom-dispatcher {
  executor = "thread-pool-executor"
  type = PinnedDispatcher
}

then in your test when you create the actor

val boothWorker = system.actorOf(Props(classOf[WorkerTest]))
   .withDispatcher("my-custom-dispatcher"))