2017-09-28 70 views
2

如果使用TestActorRef.apply()創建參與者,則可能無法通過在Future中調用actorSystem.actorSelection.resolveOne來解決。使用TestActorRef創建的參與者有時無法解析

TestActorRef的文檔說它可以在單線程環境中使用,但我想知道下面的測試失敗的原因是什麼。

阿卡版本:2.4.16

最少的測試失敗,如果有錯誤akka.actor.ActorNotFound: Actor not found for: ActorSelection[Anchor(akka://test-system/), Path(/user/test-actor)]運行1000次:

import akka.actor.{Actor, ActorSystem, Props} 
import akka.testkit.TestActorRef 
import akka.util.Timeout 
import org.junit.runner.RunWith 
import org.scalatest._ 
import org.scalatest.junit.JUnitRunner 

import scala.concurrent.Await 
import scala.concurrent.duration._ 

@RunWith(classOf[JUnitRunner]) 
class TestActorRefTest extends FunSuite with Matchers with BeforeAndAfterAll { 

    implicit val actorSystem = ActorSystem("test-system") 
    implicit val timeout = Timeout.durationToTimeout(3.seconds) 

    override def afterAll(): Unit = actorSystem.terminate() 

    test("find just created actors") { 

    val actorRef = TestActorRef(Props(new TestActor()), "test-actor") 
    val timeout = Timeout.durationToTimeout(3.seconds) 

    val findFuture = actorSystem.actorSelection(actorRef.path).resolveOne()(timeout) 
    Await.result(findFuture, 10.seconds) 
    } 
} 

private class TestActor extends Actor { 
    override def receive: Receive = { 
    case _ => 
    } 
} 

回答

0

如文檔here一個大的警告框指出測試演員裁判是僅用於同步測試,並不能安全地用於任何異步測試(例如顯示的正在執行actor選擇的測試)。

+0

在此示例中,不會使用TestActorRef來代替錯誤。 – Oleg

+0

您正在嘗試使用actorSelection來查看testActorRef,因此它實際上用於出錯的地方。 – johanandren

+0

正在搜索doest並不意味着被使用。 – Oleg

相關問題