2013-04-14 22:30:09 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2013-04-14 22:30:09 +02:00
|
|
|
*/
|
|
|
|
|
package akka.contrib.pattern
|
|
|
|
|
|
|
|
|
|
import language.postfixOps
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
import akka.cluster.ClusterEvent._
|
|
|
|
|
import akka.remote.testconductor.RoleName
|
|
|
|
|
import akka.remote.testkit.MultiNodeConfig
|
|
|
|
|
import akka.remote.testkit.MultiNodeSpec
|
|
|
|
|
import akka.remote.testkit.STMultiNodeSpec
|
|
|
|
|
import akka.testkit._
|
|
|
|
|
import akka.actor.Address
|
|
|
|
|
|
|
|
|
|
object ClusterClientSpec extends MultiNodeConfig {
|
|
|
|
|
val client = role("client")
|
|
|
|
|
val first = role("first")
|
|
|
|
|
val second = role("second")
|
|
|
|
|
val third = role("third")
|
|
|
|
|
val fourth = role("fourth")
|
|
|
|
|
|
|
|
|
|
commonConfig(ConfigFactory.parseString("""
|
|
|
|
|
akka.loglevel = INFO
|
|
|
|
|
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
|
|
|
|
|
akka.remote.log-remote-lifecycle-events = off
|
2013-09-11 16:09:51 +02:00
|
|
|
akka.cluster.auto-down-unreachable-after = 0s
|
2013-04-14 22:30:09 +02:00
|
|
|
"""))
|
|
|
|
|
|
|
|
|
|
class TestService(testActor: ActorRef) extends Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case msg ⇒
|
|
|
|
|
testActor forward msg
|
2014-01-16 15:16:35 +01:00
|
|
|
sender() ! "ack"
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Service extends Actor {
|
|
|
|
|
def receive = {
|
2014-01-16 15:16:35 +01:00
|
|
|
case msg ⇒ sender() ! msg
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ClusterClientMultiJvmNode1 extends ClusterClientSpec
|
|
|
|
|
class ClusterClientMultiJvmNode2 extends ClusterClientSpec
|
|
|
|
|
class ClusterClientMultiJvmNode3 extends ClusterClientSpec
|
|
|
|
|
class ClusterClientMultiJvmNode4 extends ClusterClientSpec
|
|
|
|
|
class ClusterClientMultiJvmNode5 extends ClusterClientSpec
|
|
|
|
|
|
|
|
|
|
class ClusterClientSpec extends MultiNodeSpec(ClusterClientSpec) with STMultiNodeSpec with ImplicitSender {
|
|
|
|
|
import ClusterClientSpec._
|
|
|
|
|
|
|
|
|
|
override def initialParticipants = roles.size
|
|
|
|
|
|
|
|
|
|
def join(from: RoleName, to: RoleName): Unit = {
|
|
|
|
|
runOn(from) {
|
|
|
|
|
Cluster(system) join node(to).address
|
|
|
|
|
createReceptionist()
|
|
|
|
|
}
|
|
|
|
|
enterBarrier(from.name + "-joined")
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 13:03:29 +02:00
|
|
|
def createReceptionist(): Unit = ClusterReceptionistExtension(system)
|
2013-04-14 22:30:09 +02:00
|
|
|
|
|
|
|
|
def awaitCount(expected: Int): Unit = {
|
|
|
|
|
awaitAssert {
|
2013-04-22 13:03:29 +02:00
|
|
|
DistributedPubSubExtension(system).mediator ! DistributedPubSubMediator.Count
|
2013-12-17 14:25:56 +01:00
|
|
|
expectMsgType[Int] should be(expected)
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def roleName(addr: Address): Option[RoleName] = roles.find(node(_).address == addr)
|
|
|
|
|
|
|
|
|
|
def initialContacts = Set(
|
|
|
|
|
system.actorSelection(node(second) / "user" / "receptionist"),
|
|
|
|
|
system.actorSelection(node(third) / "user" / "receptionist"))
|
|
|
|
|
|
|
|
|
|
"A ClusterClient" must {
|
|
|
|
|
|
|
|
|
|
"startup cluster" in within(30 seconds) {
|
|
|
|
|
join(first, first)
|
|
|
|
|
join(second, first)
|
|
|
|
|
join(third, first)
|
|
|
|
|
join(fourth, first)
|
|
|
|
|
runOn(fourth) {
|
2013-04-17 22:14:19 +02:00
|
|
|
val service = system.actorOf(Props(classOf[TestService], testActor), "testService")
|
2013-08-20 01:43:34 -07:00
|
|
|
ClusterReceptionistExtension(system).registerService(service)
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
runOn(first, second, third, fourth) {
|
|
|
|
|
awaitCount(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enterBarrier("after-1")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"communicate to actor on any node in cluster" in within(10 seconds) {
|
|
|
|
|
runOn(client) {
|
2013-04-17 22:14:19 +02:00
|
|
|
val c = system.actorOf(ClusterClient.props(initialContacts))
|
2013-09-09 14:56:16 +02:00
|
|
|
c ! ClusterClient.Send("/user/testService", "hello", localAffinity = true)
|
|
|
|
|
expectMsg("ack")
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
runOn(fourth) {
|
|
|
|
|
expectMsg("hello")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enterBarrier("after-2")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"demonstrate usage" in within(15 seconds) {
|
|
|
|
|
def host1 = first
|
|
|
|
|
def host2 = second
|
|
|
|
|
def host3 = third
|
|
|
|
|
|
|
|
|
|
//#server
|
|
|
|
|
runOn(host1) {
|
|
|
|
|
val serviceA = system.actorOf(Props[Service], "serviceA")
|
2013-08-20 01:43:34 -07:00
|
|
|
ClusterReceptionistExtension(system).registerService(serviceA)
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runOn(host2, host3) {
|
|
|
|
|
val serviceB = system.actorOf(Props[Service], "serviceB")
|
2013-08-20 01:43:34 -07:00
|
|
|
ClusterReceptionistExtension(system).registerService(serviceB)
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
//#server
|
|
|
|
|
|
2013-09-17 14:52:31 +02:00
|
|
|
runOn(host1, host2, host3, fourth) {
|
2013-09-09 14:56:16 +02:00
|
|
|
awaitCount(4)
|
|
|
|
|
}
|
|
|
|
|
enterBarrier("services-replicated")
|
|
|
|
|
|
2013-04-14 22:30:09 +02:00
|
|
|
//#client
|
|
|
|
|
runOn(client) {
|
2013-04-17 22:14:19 +02:00
|
|
|
val c = system.actorOf(ClusterClient.props(initialContacts))
|
2013-04-22 13:03:29 +02:00
|
|
|
c ! ClusterClient.Send("/user/serviceA", "hello", localAffinity = true)
|
|
|
|
|
c ! ClusterClient.SendToAll("/user/serviceB", "hi")
|
2013-04-14 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
//#client
|
|
|
|
|
|
2013-09-09 14:56:16 +02:00
|
|
|
runOn(client) {
|
|
|
|
|
// note that "hi" was sent to 2 "serviceB"
|
2013-12-17 14:25:56 +01:00
|
|
|
receiveN(3).toSet should be(Set("hello", "hi"))
|
2013-09-09 14:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-14 22:30:09 +02:00
|
|
|
{ //not used, only demo
|
|
|
|
|
//#initialContacts
|
|
|
|
|
val initialContacts = Set(
|
|
|
|
|
system.actorSelection("akka.tcp://OtherSys@host1:2552/user/receptionist"),
|
|
|
|
|
system.actorSelection("akka.tcp://OtherSys@host2:2552/user/receptionist"))
|
|
|
|
|
//#initialContacts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// strange, barriers fail without this sleep
|
|
|
|
|
Thread.sleep(1000)
|
|
|
|
|
enterBarrier("after-3")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"re-establish connection to receptionist when connection is lost" in within(30 seconds) {
|
|
|
|
|
runOn(first, second, third, fourth) {
|
2013-04-17 22:14:19 +02:00
|
|
|
val service2 = system.actorOf(Props(classOf[TestService], testActor), "service2")
|
2013-08-20 01:43:34 -07:00
|
|
|
ClusterReceptionistExtension(system).registerService(service2)
|
2013-04-14 22:30:09 +02:00
|
|
|
awaitCount(8)
|
|
|
|
|
}
|
|
|
|
|
enterBarrier("service2-replicated")
|
|
|
|
|
|
|
|
|
|
runOn(client) {
|
2013-04-17 22:14:19 +02:00
|
|
|
val c = system.actorOf(ClusterClient.props(initialContacts))
|
2013-04-14 22:30:09 +02:00
|
|
|
|
2013-09-09 14:56:16 +02:00
|
|
|
c ! ClusterClient.Send("/user/service2", "bonjour", localAffinity = true)
|
|
|
|
|
expectMsg("ack")
|
2013-04-14 22:30:09 +02:00
|
|
|
val lastSenderAddress = lastSender.path.address
|
|
|
|
|
val receptionistRoleName = roleName(lastSenderAddress) match {
|
|
|
|
|
case Some(r) ⇒ r
|
|
|
|
|
case None ⇒ fail("unexpected missing roleName: " + lastSender.path.address)
|
|
|
|
|
}
|
2013-04-23 16:44:14 +02:00
|
|
|
testConductor.exit(receptionistRoleName, 0).await
|
2013-04-14 22:30:09 +02:00
|
|
|
awaitAssert {
|
2013-04-22 13:03:29 +02:00
|
|
|
c ! ClusterClient.Send("/user/service2", "hi again", localAffinity = true)
|
2013-04-14 22:30:09 +02:00
|
|
|
expectMsg(1 second, "ack")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
enterBarrier("verifed-3")
|
|
|
|
|
receiveWhile(2 seconds) {
|
|
|
|
|
case "hi again" ⇒
|
|
|
|
|
case other ⇒ fail("unexpected message: " + other)
|
|
|
|
|
}
|
|
|
|
|
enterBarrier("after-4")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|