我有一些零星的测试失败,并努力找出原因。我有一堆演员要做我想测试的工作。在测试开始时,我传入了一个参与者引用,它是从TestProbe()中获得的。稍后,参与者组做一些工作,并将结果发送到给定的测试探测器参与者引用。然后我用TestProbe()检查结果:
class MyCaseSpec extends Spec with ShouldMatchers{
describe("The Thingy"){
it("should work"){
val eventListener = TestProbe()
val myStuffUnderTest = Actor.actorOf(new ComplexActor(eventListener.ref)).start();
myStuffUnderTest ! "Start"
val eventMessage = eventListener.receiveOne(10.seconds).asInstanceOf[SomeEventMessage]
eventMessage.data should be ("Result")
}
}
}现在,测试偶尔会失败。当我查看堆栈跟踪时,我发现在向测试探测器参与者发送消息时,我得到了一个'ActorInitializationException‘。然而,在任何时候我都没有停止TestProbe参与者。
下面是一个例外:
[akka:event-driven:dispatcher:global-11] [LocalActorRef] Actor has not been started, you need to invoke 'actor.start()' before using it
akka.actor.ActorInitializationException: Actor has not been started, you need to invoke 'actor.start()' before using it
[Gamlor-Laptop_c15fdca0-219e-11e1-9579-001b7744104e]
at akka.actor.ScalaActorRef$class.$bang(ActorRef.scala:1399)
at akka.actor.LocalActorRef.$bang(ActorRef.scala:605)
at akka.mobile.client.RemoteMessaging$RemoteMessagingSupervision$$anonfun$receive$1.apply(RemoteMessaging.scala:125)
at akka.mobile.client.RemoteMessaging$RemoteMessagingSupervision$$anonfun$receive$1.apply(RemoteMessaging.scala:121)
at akka.actor.Actor$class.apply(Actor.scala:545)
....我想知道我是否遗漏了一些明显的东西,或者我正在犯一个微妙的错误?或者,也许我的代码中真的出了问题,我看不到它?
我用的是Akka 1.2。
Vitor的更新-评论。在125行,我向具有!-operator的参与者发送了一条消息。现在,在测试设置中,这就是TestProbe角色引用。我不明白为什么有时TestProbe演员似乎被阻止了。
protected def receive = {
case msg: MaximumNumberOfRestartsWithinTimeRangeReached => {
val lastException = msg.getLastExceptionCausingRestart
faultHandling ! ConnectionError(lastException, messages.toList, self) // < Line 125. The faultHandling is the TestProbe actor
become({
// Change to failure-state behavior
}
// Snip无论如何,我现在正试图进一步隔离这个问题。谢谢你的任何提示/想法。
发布于 2011-12-09 17:46:43
好的,几乎可以肯定发现了这个问题=)。TestProbes确实有一个超时:当5秒后没有任何反应时,他们会自行停止。
现在不幸的是,测试只需要5秒多一点的时间:在这段时间内,测试探针可能会完全停止,这会导致测试失败。
修复它很容易,增加TestProbe上的超时时间:
val errorHandler = ignoreConnectionMsgProbe()
errorHandler.setTestActorTimeout(20.seconds)发布于 2011-12-09 00:03:05
你不能在这里开始你的演员。我不确定为什么你的测试有时会起作用。上面的代码需要用.start()修改以下行
val myStuffUnderTest = Actor.actorOf(new ComplexActor(eventListener.ref)).start();https://stackoverflow.com/questions/8431912
复制相似问题