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()