Scala Play Framework - 在涉及 Akka 的 运行 测试中出现 "cannot enqueue after timer shutdown" 错误
Scala Play Framework - getting "cannot enqueue after timer shutdown" error when running tests that involve Akka
当尝试对我的代码(使用 Mockito)进行单元测试时,运行 是一个 Akka 调度程序,我不断得到 "cannot enqueue after timer shutdown"。
我的代码:
Global.scala
override def onStart(app: Application){
Some(Akka.system.scheduler.schedule(23.hours, 24.hours) {
println("I run all the time")
})
}
TasksRepositorySpec.scala
def insertTestData() = {
running(FakeApplication()) {
//some code
}
}
当我运行:
activator test
我得到:
[info] TasksRepositorySpec
[info]
[error] !
[error] cannot enqueue after timer shutdown (Scheduler.scala:270)
[error] akka.actor.LightArrayRevolverScheduler.schedule(Scheduler.scala:270)
[error] akka.actor.Scheduler$class.schedule(Scheduler.scala:79)
[error] akka.actor.LightArrayRevolverScheduler.schedule(Scheduler.scala:182)
[error] Global$.onStart(Global.scala:56)
[error] play.api.GlobalPlugin.onStart(GlobalSettings.scala:272)
[error] play.api.Play$$anonfun$start$$anonfun$apply$mcV$sp.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start$$anonfun$apply$mcV$sp.apply(Play.scala:91)
[error] play.api.Plugins.foreach(Plugins.scala:57)
[error] play.api.Play$$anonfun$start.apply$mcV$sp(Play.scala:91)
[error] play.api.Play$$anonfun$start.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start.apply(Play.scala:91)
[error] play.utils.Threads$.withContextClassLoader(Threads.scala:21)
[error] play.api.Play$.start(Play.scala:90)
[error] play.api.test.PlayRunners$class.running(Helpers.scala:41)
[error] play.api.test.Helpers$.running(Helpers.scala:363)
[error] repositories.TasksRepositorySpec.insertData(TasksRepositorySpec.scala:69)
[error] repositories.TasksRepositorySpec$$anonfun.apply$mcI$sp(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun.apply(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun.apply(TasksRepositorySpec.scala:88)
我用play 2.4
任何帮助将不胜感激
抱歉,我没有找到解决方案,只有一个解决方法。它包括使用伪造的 Global
对象进行测试,其中 onStart()
和 onStop()
方法为空。
首先,您需要将 Global
对象拆分为特征和空对象:
Global.scala
object Global extends Global
trait Global extends GlobalSettings { ... }
然后在你的测试代码中,你可以创建一个假的Global
:
Fake.scala
object FakeGlobal extends Global {
override def onStart(app: Application): Unit = {}
override def onStop(app: Application): Unit = {}
}
object Fake {
// This has to be a method, cannot be a value
def application() = FakeApplication(withGlobal = Some(FakeGlobal))
}
最后在你的测试中使用它:
TasksRepositorySpec.scala
def insertTestData() = {
running(Fake.application()) {
//some code
}
}
对我有用。希望对您有所帮助!
当尝试对我的代码(使用 Mockito)进行单元测试时,运行 是一个 Akka 调度程序,我不断得到 "cannot enqueue after timer shutdown"。
我的代码:
Global.scala
override def onStart(app: Application){
Some(Akka.system.scheduler.schedule(23.hours, 24.hours) {
println("I run all the time")
})
}
TasksRepositorySpec.scala
def insertTestData() = {
running(FakeApplication()) {
//some code
}
}
当我运行:
activator test
我得到:
[info] TasksRepositorySpec
[info]
[error] !
[error] cannot enqueue after timer shutdown (Scheduler.scala:270)
[error] akka.actor.LightArrayRevolverScheduler.schedule(Scheduler.scala:270)
[error] akka.actor.Scheduler$class.schedule(Scheduler.scala:79)
[error] akka.actor.LightArrayRevolverScheduler.schedule(Scheduler.scala:182)
[error] Global$.onStart(Global.scala:56)
[error] play.api.GlobalPlugin.onStart(GlobalSettings.scala:272)
[error] play.api.Play$$anonfun$start$$anonfun$apply$mcV$sp.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start$$anonfun$apply$mcV$sp.apply(Play.scala:91)
[error] play.api.Plugins.foreach(Plugins.scala:57)
[error] play.api.Play$$anonfun$start.apply$mcV$sp(Play.scala:91)
[error] play.api.Play$$anonfun$start.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start.apply(Play.scala:91)
[error] play.utils.Threads$.withContextClassLoader(Threads.scala:21)
[error] play.api.Play$.start(Play.scala:90)
[error] play.api.test.PlayRunners$class.running(Helpers.scala:41)
[error] play.api.test.Helpers$.running(Helpers.scala:363)
[error] repositories.TasksRepositorySpec.insertData(TasksRepositorySpec.scala:69)
[error] repositories.TasksRepositorySpec$$anonfun.apply$mcI$sp(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun.apply(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun.apply(TasksRepositorySpec.scala:88)
我用play 2.4
任何帮助将不胜感激
抱歉,我没有找到解决方案,只有一个解决方法。它包括使用伪造的 Global
对象进行测试,其中 onStart()
和 onStop()
方法为空。
首先,您需要将 Global
对象拆分为特征和空对象:
Global.scala
object Global extends Global
trait Global extends GlobalSettings { ... }
然后在你的测试代码中,你可以创建一个假的Global
:
Fake.scala
object FakeGlobal extends Global {
override def onStart(app: Application): Unit = {}
override def onStop(app: Application): Unit = {}
}
object Fake {
// This has to be a method, cannot be a value
def application() = FakeApplication(withGlobal = Some(FakeGlobal))
}
最后在你的测试中使用它:
TasksRepositorySpec.scala
def insertTestData() = {
running(Fake.application()) {
//some code
}
}
对我有用。希望对您有所帮助!