scala - TestActorRef 死锁可能性是否仍然相关?

标签 scala testing akka

在 akka 2.3.9 documentation有一个警告:

Any message send from a TestProbe to another actor which runs on the CallingThreadDispatcher runs the risk of dead-lock, if that other actor might also send to this probe. The implementation of TestProbe.watch and TestProbe.unwatch will also send a message to the watchee, which means that it is dangerous to try watching e.g. TestActorRef from a TestProbe.

我尝试使用 TestProbeTestActorRef 在测试中重现所描述的死锁情况,因为两者都使用 CallingThreadDispatcher

class DeadlockTest extends TestKit(ActorSystem()) with FunSuiteLike {
  test("deadlock attempt") {
    val b = TestProbe()
    val a = TestActorRef(new Actor {
      override def preStart() = b.ref ! "pre"
      def receive = {
        case msg =>
          sender ! msg
          context.stop(self)
      }
    })
    b.watch(a)
    b.send(a, "foo")
    b.expectMsg("pre")
    b.expectMsg("foo")
    b.expectTerminated(a)
    b.unwatch(a)
  }
}

我预计测试会挂起,但它成功通过了。

警告是否仍然相关并且死锁的可能性很高还是已过时?

最佳答案

这个警告所说的竞争条件是 TestProbe 设计中固有的,但是遇到死锁的概率相当小,监视必须与向另一个方向发送消息的时间完全同时发生,并且两者都必须发生在不同的线程。由于顶级参与者的创建方式,在极少数情况下可以观察到后者。

所有这些都相当棘手,并且都不适用于为 Akka Typed 编写和执行测试的方式,我期待着何时成为唯一使用的模式。

关于scala - TestActorRef 死锁可能性是否仍然相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806011/

相关文章:

scala - 递归调用不在尾部位置

scala - 如何使用actorSelection选择akka Actor ?

akka - MaximumNumberOfRestartsWithinTimeRangeReached 相当于 Akka 2.0

java - 模拟 : mock method parameter

Scala/Akka websocket 服务器库

添加 swagger-play2 依赖项后 Scala Play 项目构建失败 - sbt 找不到播放?

scala - 将Spark插入Java堆空间

scala 中的 Java AWT : (AWT-EventQueue-0) java. lang.NoClassDefFoundError

api - 如何使用 SonarQube API 获取测试信息?

Java 断言错误