Scala Play Framework - 运行涉及 Akka 的测试时出现"cannot enqueue after timer shutdown"错误



当尝试单元测试我的代码(与Mockito)运行一个Akka调度程序时,我一直得到"定时器关闭后不能排队"。

我代码:

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$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:91)
[error] play.api.Plugins.foreach(Plugins.scala:57)
[error] play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:91)
[error] play.api.Play$$anonfun$start$1.apply(Play.scala:91)
[error] play.api.Play$$anonfun$start$1.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$2.apply$mcI$sp(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun$2.apply(TasksRepositorySpec.scala:88)
[error] repositories.TasksRepositorySpec$$anonfun$2.apply(TasksRepositorySpec.scala:88)

我使用play 2.4

如有任何帮助,不胜感激

对不起,我没有找到解决方案,但只有一个变通办法。它包括使用假的Global对象进行测试,其中onStart()onStop()方法为空。

首先你需要把你的Global对象分成一个trait和一个空对象:

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
  }
}

这对我有用。希望能有所帮助!

相关内容

  • 没有找到相关文章

最新更新