在最后一个测试用例以外的存根中使用状态变量时,Akka Testkit不起作用

时间:2016-03-30 15:41:07

标签: scala akka akka-testkit

使用以下存根与Akka Testkit时出现问题:

import akka.actor.{Props, Status}
import akka.camel.{CamelExtension, CamelMessage, Consumer}
import akka.testkit.{ImplicitSender, TestProbe}
import org.scalatest._

[import own packages]

import scala.concurrent.duration.DurationInt
import scala.language.postfixOps

class ActorSpec extends AkkaTestBase
  with ImplicitSender with WordSpecLike 
  with Matchers with BeforeAndAfterAll with IOSugars {

  var stubResponse: String = ""
  var doNotRespond = false

  val stubEndpoint = "direct:test"
  val reportProbe = TestProbe()
  val actorUnderTest= TestProbe()

  // create stub consumer
  val stubConsumer = system.actorOf(Props(new Consumer {
    override val endpointUri = stubEndpoint
    override def replyTimeout = 1 second

    override def receive: Receive = {
      case msg: CamelMessage if doNotRespond => ()
      case msg: CamelMessage => sender() ! stubResponse;
      case _ => sender() ! "ack"
    }
  }), "stubActor")

  [...]
}

当我运行以下测试用例时:

"Respond with failure when camel produces a failure" in {
  doNotRespond = true
  actorUnderTest ! // Send relevant message 
                   // (which returns the () above because of doNotRespond set to true)
  doNotRespond = false // Return to original state
  testProbe.expectMsg // Expect relevant message

"Respond with a failure when 'ERROR' status code is returned" in {
  stubResponse = readStringFromResource("/failure.xml")
  actorUnderTest ! // send relevant message
  expectMsgClass(classOf[Status.Failure])
}

不幸的是,这不起作用。我们得到了超时:

  

ERROR o.a.c.processor.DefaultErrorHandler - (ExchangeId:xxx上的MessageId:
xxx)传递失败。交付尝试后用尽:1捕获:
java.util.concurrent.TimeoutException:无法在超时[1秒]内获得来自参与者[ActorEndpointPath(akka:// xxx / stubActor)的响应。< / p>

当更改doNotRespond状态变量的测试用例作为最后一个测试用例时,一切都能正常工作。

为什么会这样,以及如何解决?

0 个答案:

没有答案