gpt4 book ai didi

scala - “Actor not found” on context.actorSelection where actorFor works

转载 作者:行者123 更新时间:2023-12-05 07:58:53 24 4
gpt4 key购买 nike

代码是:

 //      pilot = Await.result(context.actorSelection(s"../$pilotName").resolveOne, 5.seconds)
pilot = context.actorFor("../" + pilotName)

actorFor 在应用和测试中运行良好。
评论的代码在应用程序中工作但在运行测试时因 akka.actor.ActorNotFound(它查找的 Actor 存在并且我认为超时足够)而失败,这真的很奇怪。

测试是:

class PilotsSpec extends TestKit(ActorSystem("PilotsSpec",
ConfigFactory.parseString(PilotsSpec.configStr)))
with ImplicitSender with WordSpecLike with MustMatchers {
import PilotsSpec._
import plane.Plane._

def nilActor: ActorRef = TestProbe().ref

val pilotPath = s"/user/TestPilots/$pilotName"
val copilotPath = s"/user/TestPilots/$copilotName"

def pilotsReadyToGo(): ActorRef = {
implicit val timeout = Timeout(5.seconds)

val a = system.actorOf(Props(
new IsolatedStopSupervisor with OneForOneStrategyFactory {
def childrenStart() = {
context.actorOf(Props[FakePilot], pilotName)
context.actorOf(Props(new CoPilot(testActor, nilActor, nilActor)), copilotName)
}
}), "TestPilots")
Await.result(a ? IsolatedLifeCycleSupervisor.WaitForStart, 5.seconds)
system.actorFor(copilotPath) ! Pilots.ReadyToGo
a
}

"CoPilot" should {
"takecontrol when the Pilotdies" in {
pilotsReadyToGo()
// Kill the Pilot
system.actorFor(pilotPath) ! PoisonPill
// Sincethe test classis the "Plane" we can
// expect to see this request
expectMsg(GiveMeControl)
// The girl who sent it had better be Mary
lastSender must be (system.actorFor(copilotPath))
}
}

}

不知道是不是我的ActorSelection做错了。
我尝试使用 onComplete 但它仍然不起作用并在测试中抛出 ActorNotFound 异常。

  val f = context.actorSelection("../" + pilotName).resolveOne
f onComplete {
case Success(v) => { pilot = v; context.watch(pilot); println("pilot get") }
case Failure(e) => throw e
}

有谁知道 actorFor 有效但 actorSelection 失败的原因(在测试中)?

然后我将下面的代码添加到测试代码中:

system.actorSelection(pilotPath).resolveOne map {v => println("------pilot:"+v)}
system.actorSelection(copilotPath).resolveOne map {v => println("------copilot:"+v)}
Thread.sleep(1000)

它可以工作并打印这些 actorRef然后我尝试用常量字符串 pilotPath 替换 ../+pilotName 但它再次失败(无论 context.actorSelection 还是 context .system.actorSelection)
这是异常(片段):

test-only cc.akka.avionics.PilotsSpec
[info] Compiling 1 Scala source to G:\scala_workspace\akka_test_u7\target\scala-2.10\test-classes...
[warn] there were 2 deprecation warning(s); re-run with -deprecation for details
[warn] one warning found
------copilot:Actor[akka://PilotsSpec/user/TestPilots/Mary#962346268]
------pilot:Actor[akka://PilotsSpec/user/TestPilots/Mark#-320295209]
ActorSelection[Anchor(akka://PilotsSpec/user/TestPilots/Mary#962346268), Path(/../Mark)]
[ERROR] [04/29/2014 15:13:16.080] [PilotsSpec-akka.actor.default-dispatcher-4] [akka.dispatch.Dispatcher] Actor not found for: ActorSelection[Ancho r(akka://PilotsSpec/user/TestPilots/Mary#962346268), Path(/../Mark)]
akka.actor.ActorNotFound: Actor not found for: ActorSelection[Anchor(akka://PilotsSpec/user/TestPilots/Mary#962346268), Path(/../Mark)]
at akka.actor.ActorSelection$$anonfun$resolveOne$1.apply(ActorSelection.scala:65)
at akka.actor.ActorSelection$$anonfun$resolveOne$1.apply(ActorSelection.scala:63)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:67)
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply$mcV$sp(BatchingExecutor.scala:82)
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.apply(BatchingExecutor.scala:59)
at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:72)
at akka.dispatch.BatchingExecutor$Batch.run(BatchingExecutor.scala:58)
at akka.dispatch.ExecutionContexts$sameThreadExecutionContext$.unbatchedExecute(Future.scala:74)
at akka.dispatch.BatchingExecutor$class.execute(BatchingExecutor.scala:110)

只有actorFor在测试中有效,其他使用actorSelection注释的代码在应用中有效但在测试中失败(很奇怪):

class CoPilot(plane: ActorRef,
var controls: ActorRef,
altimeter: ActorRef) extends Actor {
implicit val timeout = Timeout(1.second)
implicit val ct = context.dispatcher
var pilot: ActorRef = context.system.deadLetters
val pilotName: String = context.system.settings.config.getString("cc.akka.avionics.flightcrew.pilotName")
val pilotId : Int = 200

def receive = {
case ReadyToGo =>
// fails in test
// pilot = Await.result(context.actorSelection(s"../$pilotName").resolveOne, 3.seconds)
// println("get pilot:" + pilot.path + " dead:" + pilot.isTerminated)

// actorFor works
// pilot = context.actorFor("../" + pilotName)
// context.watch(pilot)
// autopilot = Await.result(context.actorSelection("../AutoPilot").resolveOne, 100.millis)

// fails in test
// val f = context.actorSelection("../" + pilotName).resolveOne
// f onComplete {
// case Success(v) => { pilot = v; context.watch(pilot); println("pilot get") }
// case Failure(e) => throw e
// }
println("-----------"+pilotName)

// fails in test
context.actorSelection("../" + pilotName) ! Identify(pilotId)

case Terminated(_) =>
plane ! GiveMeControl
case Controls(controlSurfaces) =>
controls = controlSurfaces

case ActorIdentity(pilotId, Some(ref)) =>
pilot = ref
context.watch(pilot)
println("find copilot:"+pilot)
case ActorIdentity(pilotId, None) =>
println("failed to find pilot")
}
}

最佳答案

actorFor 创建新的 Actors,而 actorSelection 寻找已经存在的 actors。

我怀疑你的 actorSelection 不起作用而你的 actorFor 起作用的原因是 Actor 目前不存在。

关于scala - “Actor not found” on context.actorSelection where actorFor works,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356540/

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