2012-05-29 08:28:54 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.remote.router
|
|
|
|
|
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
import akka.actor.Props
|
2012-05-29 13:18:46 +02:00
|
|
|
import akka.actor.PoisonPill
|
|
|
|
|
import akka.actor.Address
|
2012-05-29 08:28:54 +02:00
|
|
|
import akka.dispatch.Await
|
|
|
|
|
import akka.pattern.ask
|
|
|
|
|
import akka.remote.testkit.MultiNodeConfig
|
|
|
|
|
import akka.remote.testkit.MultiNodeSpec
|
|
|
|
|
import akka.routing.Broadcast
|
|
|
|
|
import akka.routing.RandomRouter
|
|
|
|
|
import akka.routing.RoutedActorRef
|
|
|
|
|
import akka.testkit._
|
2012-05-29 13:18:46 +02:00
|
|
|
import akka.util.duration._
|
2012-05-29 08:28:54 +02:00
|
|
|
|
|
|
|
|
object RandomRoutedRemoteActorMultiJvmSpec extends MultiNodeConfig {
|
|
|
|
|
|
|
|
|
|
class SomeActor extends Actor with Serializable {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hit" ⇒ sender ! self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val first = role("first")
|
|
|
|
|
val second = role("second")
|
|
|
|
|
val third = role("third")
|
|
|
|
|
val fourth = role("fourth")
|
|
|
|
|
|
|
|
|
|
commonConfig(debugConfig(on = false))
|
|
|
|
|
|
|
|
|
|
deployOnAll("""
|
|
|
|
|
/service-hello.router = "random"
|
|
|
|
|
/service-hello.nr-of-instances = 3
|
|
|
|
|
/service-hello.target.nodes = ["@first@", "@second@", "@third@"]
|
|
|
|
|
""")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RandomRoutedRemoteActorMultiJvmNode1 extends RandomRoutedRemoteActorSpec
|
|
|
|
|
class RandomRoutedRemoteActorMultiJvmNode2 extends RandomRoutedRemoteActorSpec
|
|
|
|
|
class RandomRoutedRemoteActorMultiJvmNode3 extends RandomRoutedRemoteActorSpec
|
|
|
|
|
class RandomRoutedRemoteActorMultiJvmNode4 extends RandomRoutedRemoteActorSpec
|
|
|
|
|
|
|
|
|
|
class RandomRoutedRemoteActorSpec extends MultiNodeSpec(RandomRoutedRemoteActorMultiJvmSpec)
|
|
|
|
|
with ImplicitSender with DefaultTimeout {
|
|
|
|
|
import RandomRoutedRemoteActorMultiJvmSpec._
|
|
|
|
|
|
|
|
|
|
def initialParticipants = 4
|
|
|
|
|
|
|
|
|
|
"A new remote actor configured with a Random router" must {
|
|
|
|
|
"be locally instantiated on a remote node and be able to communicate through its RemoteActorRef" in {
|
|
|
|
|
|
|
|
|
|
runOn(first, second, third) {
|
|
|
|
|
testConductor.enter("start", "broadcast-end", "end", "done")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runOn(fourth) {
|
|
|
|
|
testConductor.enter("start")
|
|
|
|
|
val actor = system.actorOf(Props[SomeActor].withRouter(RandomRouter()), "service-hello")
|
|
|
|
|
actor.isInstanceOf[RoutedActorRef] must be(true)
|
|
|
|
|
|
|
|
|
|
val connectionCount = 3
|
|
|
|
|
val iterationCount = 10
|
|
|
|
|
|
2012-05-29 13:18:46 +02:00
|
|
|
for (i ← 0 until iterationCount; k ← 0 until connectionCount) {
|
|
|
|
|
actor ! "hit"
|
|
|
|
|
}
|
2012-05-29 08:28:54 +02:00
|
|
|
|
2012-05-29 13:18:46 +02:00
|
|
|
val replies: Map[Address, Int] = (receiveWhile(5 seconds, messages = connectionCount * iterationCount) {
|
|
|
|
|
case ref: ActorRef ⇒ ref.path.address
|
|
|
|
|
}).foldLeft(Map(node(first).address -> 0, node(second).address -> 0, node(third).address -> 0)) {
|
|
|
|
|
case (replyMap, address) ⇒ replyMap + (address -> (replyMap(address) + 1))
|
2012-05-29 08:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testConductor.enter("broadcast-end")
|
2012-05-29 13:18:46 +02:00
|
|
|
actor ! Broadcast(PoisonPill)
|
2012-05-29 08:28:54 +02:00
|
|
|
|
|
|
|
|
testConductor.enter("end")
|
|
|
|
|
replies.values foreach { _ must be > (0) }
|
2012-05-29 13:18:46 +02:00
|
|
|
replies.get(node(fourth).address) must be(None)
|
2012-05-29 08:28:54 +02:00
|
|
|
|
|
|
|
|
// shut down the actor before we let the other node(s) shut down so we don't try to send
|
|
|
|
|
// "Terminate" to a shut down node
|
|
|
|
|
system.stop(actor)
|
|
|
|
|
testConductor.enter("done")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|