gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:54:24 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com