2011-12-08 14:44:05 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.remote
|
|
|
|
|
|
|
|
|
|
import akka.testkit._
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import com.typesafe.config._
|
|
|
|
|
|
2011-12-09 00:02:27 +01:00
|
|
|
object RemoteCommunicationSpec {
|
2011-12-09 14:52:11 +01:00
|
|
|
class Echo extends Actor {
|
|
|
|
|
var target: ActorRef = context.system.deadLetters
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2011-12-09 18:07:42 +01:00
|
|
|
case (p: Props, n: String) ⇒ sender ! context.actorOf[Echo](n)
|
2011-12-09 14:52:11 +01:00
|
|
|
case ex: Exception ⇒ throw ex
|
|
|
|
|
case s: String ⇒ sender ! context.actorFor(s)
|
|
|
|
|
case x ⇒ target = sender; sender ! x
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def preStart() {}
|
|
|
|
|
override def preRestart(cause: Throwable, msg: Option[Any]) {
|
|
|
|
|
target ! "preRestart"
|
|
|
|
|
}
|
|
|
|
|
override def postRestart(cause: Throwable) {}
|
|
|
|
|
override def postStop() {
|
|
|
|
|
target ! "postStop"
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-09 00:02:27 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-08 14:44:05 +01:00
|
|
|
class RemoteCommunicationSpec extends AkkaSpec("""
|
|
|
|
|
akka {
|
|
|
|
|
actor.provider = "akka.remote.RemoteActorRefProvider"
|
|
|
|
|
cluster.nodename = Nonsense
|
|
|
|
|
remote.server {
|
|
|
|
|
hostname = localhost
|
|
|
|
|
port = 12345
|
|
|
|
|
}
|
2011-12-09 00:02:27 +01:00
|
|
|
actor.deployment {
|
2011-12-11 20:00:26 +01:00
|
|
|
/blub.remote = "akka://remote_sys@localhost:12346"
|
|
|
|
|
/looker/child.remote = "akka://remote_sys@localhost:12346"
|
|
|
|
|
/looker/child/grandchild.remote = "akka://RemoteCommunicationSpec@localhost:12345"
|
2011-12-09 00:02:27 +01:00
|
|
|
}
|
2011-12-08 14:44:05 +01:00
|
|
|
}
|
|
|
|
|
""") with ImplicitSender {
|
|
|
|
|
|
2011-12-09 00:02:27 +01:00
|
|
|
import RemoteCommunicationSpec._
|
|
|
|
|
|
2011-12-08 14:44:05 +01:00
|
|
|
val conf = ConfigFactory.parseString("akka.remote.server.port=12346").withFallback(system.settings.config)
|
|
|
|
|
val other = ActorSystem("remote_sys", conf)
|
|
|
|
|
|
|
|
|
|
val remote = other.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "ping" ⇒ sender ! (("pong", sender))
|
|
|
|
|
}
|
|
|
|
|
}), "echo")
|
|
|
|
|
|
|
|
|
|
val here = system.actorFor("akka://remote_sys@localhost:12346/user/echo")
|
|
|
|
|
|
|
|
|
|
implicit val timeout = system.settings.ActorTimeout
|
|
|
|
|
|
|
|
|
|
override def atTermination() {
|
|
|
|
|
other.stop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Remoting" must {
|
|
|
|
|
|
|
|
|
|
"support remote look-ups" in {
|
|
|
|
|
here ! "ping"
|
|
|
|
|
expectMsgPF() {
|
|
|
|
|
case ("pong", s: AnyRef) if s eq testActor ⇒ true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"send error message for wrong address" in {
|
|
|
|
|
EventFilter.error(start = "dropping", occurrences = 1).intercept {
|
|
|
|
|
system.actorFor("akka://remotesys@localhost:12346/user/echo") ! "ping"
|
|
|
|
|
}(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"support ask" in {
|
|
|
|
|
(here ? "ping").get match {
|
|
|
|
|
case ("pong", s: AskActorRef) ⇒ // good
|
|
|
|
|
case m ⇒ fail(m + " was not (pong, AskActorRef)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"send dead letters on remote if actor does not exist" in {
|
|
|
|
|
EventFilter.warning(pattern = "dead.*buh", occurrences = 1).intercept {
|
|
|
|
|
system.actorFor("akka://remote_sys@localhost:12346/does/not/exist") ! "buh"
|
|
|
|
|
}(other)
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-09 14:52:11 +01:00
|
|
|
"create and supervise children on remote node" in {
|
|
|
|
|
val r = system.actorOf[Echo]("blub")
|
2011-12-09 00:02:27 +01:00
|
|
|
r.path.toString must be === "akka://remote_sys@localhost:12346/remote/RemoteCommunicationSpec@localhost:12345/user/blub"
|
|
|
|
|
r ! 42
|
|
|
|
|
expectMsg(42)
|
2011-12-09 14:52:11 +01:00
|
|
|
EventFilter[Exception]("crash", occurrences = 1).intercept {
|
|
|
|
|
r ! new Exception("crash")
|
|
|
|
|
}(other)
|
|
|
|
|
expectMsg("preRestart")
|
|
|
|
|
r ! 42
|
|
|
|
|
expectMsg(42)
|
|
|
|
|
r.stop()
|
|
|
|
|
expectMsg("postStop")
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-09 18:07:42 +01:00
|
|
|
"look-up actors across node boundaries" in {
|
2011-12-09 14:52:11 +01:00
|
|
|
val l = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case (p: Props, n: String) ⇒ sender ! context.actorOf(p, n)
|
|
|
|
|
case s: String ⇒ sender ! context.actorFor(s)
|
|
|
|
|
}
|
|
|
|
|
}), "looker")
|
|
|
|
|
l ! (Props[Echo], "child")
|
|
|
|
|
val r = expectMsgType[ActorRef]
|
|
|
|
|
r ! (Props[Echo], "grandchild")
|
2011-12-09 18:07:42 +01:00
|
|
|
val remref = expectMsgType[ActorRef]
|
|
|
|
|
remref.isInstanceOf[LocalActorRef] must be(true)
|
2011-12-09 14:52:11 +01:00
|
|
|
val myref = system.actorFor(system / "looker" / "child" / "grandchild")
|
|
|
|
|
myref.isInstanceOf[RemoteActorRef] must be(true)
|
|
|
|
|
myref ! 43
|
|
|
|
|
expectMsg(43)
|
2011-12-09 18:07:42 +01:00
|
|
|
lastSender must be theSameInstanceAs remref
|
2011-12-09 14:52:11 +01:00
|
|
|
(l ? "child/..").as[ActorRef].get must be theSameInstanceAs l
|
|
|
|
|
(system.actorFor(system / "looker" / "child") ? "..").as[ActorRef].get must be theSameInstanceAs l
|
2011-12-09 00:02:27 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-08 14:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|