如果使用TestActorRef.apply()创建了actor,则可能无法通过调用Future中的actorSystem.actorSelection.resolveOne
来解决它。
TestActorRef的文档说它可以在单线程环境中使用,但我想知道以下测试失败的原因是什么。
Akka版本:2.4.16
失败的最小测试,如果运行1000次且错误akka.actor.ActorNotFound: Actor not found for: ActorSelection[Anchor(akka://test-system/), Path(/user/test-actor)]
:
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 _ =>
}
}