修改akka testkit中的请求超时

Modifying ask timeout in akka testkit

我尝试了六种不同的方法,但我似乎无法修改演员询问的默认超时值。这意味着它几乎总是在 CI 服务器上失败。

这是我当前的迭代。有几种尝试捆绑在一起。 None 他们中的任何人都会做任何事情来修改 1 秒的超时值。

基础测试class

import akka.actor.ActorSystem
import akka.testkit.TestKit
import com.typesafe.config.ConfigFactory
import org.specs2.mutable.SpecificationLike
import org.specs2.specification.AfterEach
import org.specs2.specification.core.Fragments

abstract class TestActors
    extends TestKit(ActorSystem("testsystem", ConfigFactory.load("test-application.conf")))
    with SpecificationLike
    with AfterEach {
  override def map(fs: => Fragments) = super.map(fs) ^ step(system.terminate, global = true)
  def after = system.terminate()
}

规格

class RepoSpec(implicit ee: ExecutionEnv) extends TestActors {
  isolated

  implicit val timeout = Timeout(5 seconds)

  val repo = system.actorOf(Props(classOf[Repo], data))

  "return None when there's no such record" >> {
    implicit val timeout = Timeout(30 seconds)
    val record = repo ? GetRecord(1, RecordKey(1, 1, 1))
    record must beEqualTo(Option.empty[Record]).await
  }
}

src/test/resources/test-application.conf

akka.test {
  timefactor=10
  single-expect-default=5000
}

规格在我的笔记本电脑上完成,但在 Travis 上失败:

[error] x return None when there's no such record
[error]  Timeout after 1 second (retries = 0, timeout = 1 second), timeFactor = 1 (TestActors.scala:10)

编辑:奇怪的是,错误消息中引用的行是 TestActors.scala:10 - 这是基础测试 class 的 class 定义。

如果我能让系统明白1秒太快了,我会很高兴。

您可以将 timeout 设置覆盖为大于一秒的设置:

record must beEqualTo(Option.empty[Record]).await(timeout = 5.seconds)

不过,recommended practice is to set a higher timeFactor execution environment argument in specs2 when running your tests on a CI server. Await timeout settings are multiplied by timeFactor, whose default value is one。在您的测试中,timeout 的值为一秒,而 timeFactor 为一秒,导致总超时为一秒:1 second * 1。根据您的 CI 服务器更改 timeFactor