2016-03-05 112 views
0

我在编写Actor,应该看另一个Actor;让我们称后者为目标。一旦其目标停止,我的Actor应该停止。对于这个目标,我只有一个ActorSelection。要看它,我显然需要一个ActorRef,所以我想我应该发送ActorSelection一个Identify消息;当它回复与ActorIdentity我会有它的ActorRef。到目前为止这么好,但我无法让它工作。使用ActorSelection识别演员

这里的规格:

// Arrange 
val probe = TestProbe() 
val target = TestProbe().ref 
val sut = system.actorOf(MyActor.props(system.actorSelection(target.path)), "watch-target") 
probe watch sut 

// Act 
target ! PoisonPill 

// Assert 
probe.expectTerminated(sut) 

和实现(一FSM,细节跳过):

log.debug("Asking target selection {} to identify itself; messageId={}", selection.toString(), messageId) 
selection ! Identify(messageId) 

when(Waiting) { 
    case Event(ActorIdentity(`messageId`, Some(ref)), Queue(q)) => 
    log.info("Received identity for remote target: {}", ref) 
    context.watch(ref) 
    goto(NextState) using TargetFound(ref) 
    case Event(ActorIdentity(`messageId`, None), Queue(q)) => 
    log.error("Could not find requested target {}", selection.toString()) 
    stop() 
} 

initialize() 

现在,当我跑我的测试,它是绿色的,因为被测系统确实停止了。但问题在于它会因为无法使用上述步骤找到目标而自行停止。日志文件说:

询问目标选择ActorSelection [Anchor(akka:// default /),Path(/ system/testProbe-3)]以标识其自身; MESSAGEID = 871823258

找不到请求的目标ActorSelection [锚(阿卡://默认/),路径(/系统/ testProbe-3)]

我失去的东西在这里很明显?也许TestProbe不应该透露它的真实身份?我甚至试图通过实例化一个虚拟Actor目标但结果是相同的。任何线索?

回答

0

原来,答案其实很简单:在试运行如此之快,MyActor发送Identify消息给selection之前,选择背后的Actor已经收到了PoisonPill,因而被杀害。

发送该PoisonPill解决了该问题之前,加一点Thread.sleep()