Scala Akka测试如何使用多个ExecutionContext
我的actor在默认的akka调度程序上运行,该调度程序随后调用一个返回未来的方法。我已经为所有未来配置了不同的ExecutionContext来运行(因为它们是阻塞的(由于db调用),并使actors dispatcher仅用于非阻塞的actors。不知道是否可以测试此代码(继续使用两个执行上下文等)使用Akka Testkit?如果是这样的话,配置测试的方法是什么,以便Actor在默认dispatcher上运行,futures也可以找到“自定义dispatcher”供其运行?显然,当前测试抛出了以下问题Scala Akka测试如何使用多个ExecutionContext,scala,akka,Scala,Akka,我的actor在默认的akka调度程序上运行,该调度程序随后调用一个返回未来的方法。我已经为所有未来配置了不同的ExecutionContext来运行(因为它们是阻塞的(由于db调用),并使actors dispatcher仅用于非阻塞的actors。不知道是否可以测试此代码(继续使用两个执行上下文等)使用Akka Testkit?如果是这样的话,配置测试的方法是什么,以便Actor在默认dispatcher上运行,futures也可以找到“自定义dispatcher”供其运行?显然,当前测试抛
Caused by: akka.ConfigurationException: Dispatcher [custom-dispatcher] not configured
当您为参与者创建Akka Testkit的TestActorRef时,它将使用PinnedDispatcher,除非您在参与者的道具中指定了一个不同的道具,并在创建TestActorRef时传递了该道具
异常“Dispatcher[custom Dispatcher]not configured”(调度程序[custom Dispatcher]未配置)可能意味着您正在为测试使用不同的Akka配置,其中没有配置名为[custom Dispatcher]的调度程序。在测试/资源目录中创建文件application.conf
my-custom-dispatcher {
executor = "thread-pool-executor"
type = PinnedDispatcher
}
然后在测试中创建参与者时
val boothWorker = system.actorOf(Props(classOf[WorkerTest]))
.withDispatcher("my-custom-dispatcher"))
阻塞{…}
construct确保将代码正确分派到可用线程?否则,我就不需要回答您的问题了。@BobDalgleish:所以,如果我继续使用context.dispatcher,即使是阻止未来,它也会在akka的分派器上产生新的工作线程,并且在这个特定场景中永远不需要单独的ExecutionContext。是这样吗当然,这个评论是关于在akka的context.dispatcher中使用blocking{..}