2012-06-05 22:16:15 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2012-06-05 22:16:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.cluster
|
|
|
|
|
|
2012-06-25 17:21:06 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
import language.reflectiveCalls
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-06-05 22:16:15 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
2012-07-04 14:39:27 +02:00
|
|
|
import akka.testkit.ImplicitSender
|
2012-06-05 22:16:15 +02:00
|
|
|
import akka.actor.ExtendedActorSystem
|
|
|
|
|
import akka.actor.Address
|
2012-07-06 17:04:04 +02:00
|
|
|
import akka.cluster.InternalClusterAction._
|
2012-07-05 11:56:54 +02:00
|
|
|
import java.lang.management.ManagementFactory
|
|
|
|
|
import javax.management.ObjectName
|
2012-09-12 09:23:02 +02:00
|
|
|
import akka.actor.ActorRef
|
2012-06-05 22:16:15 +02:00
|
|
|
|
|
|
|
|
object ClusterSpec {
|
|
|
|
|
val config = """
|
|
|
|
|
akka.cluster {
|
2013-09-11 16:09:51 +02:00
|
|
|
auto-down-unreachable-after = 0s
|
2012-06-05 22:16:15 +02:00
|
|
|
periodic-tasks-initial-delay = 120 seconds // turn off scheduled tasks
|
2012-08-15 16:47:34 +02:00
|
|
|
publish-stats-interval = 0 s # always, when it happens
|
2012-09-06 21:48:40 +02:00
|
|
|
failure-detector.implementation-class = akka.cluster.FailureDetectorPuppet
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
2012-08-28 08:36:14 +02:00
|
|
|
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
|
2012-09-06 21:48:40 +02:00
|
|
|
akka.remote.log-remote-lifecycle-events = off
|
2013-01-17 16:19:31 +01:00
|
|
|
akka.remote.netty.tcp.port = 0
|
2012-06-07 13:32:12 +02:00
|
|
|
# akka.loglevel = DEBUG
|
2012-06-05 22:16:15 +02:00
|
|
|
"""
|
|
|
|
|
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class GossipTo(address: Address)
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2012-07-04 14:39:27 +02:00
|
|
|
class ClusterSpec extends AkkaSpec(ClusterSpec.config) with ImplicitSender {
|
2012-06-05 22:16:15 +02:00
|
|
|
import ClusterSpec._
|
|
|
|
|
|
2013-03-24 22:01:57 +01:00
|
|
|
val selfAddress = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress
|
2012-06-05 22:16:15 +02:00
|
|
|
|
2012-09-06 21:48:40 +02:00
|
|
|
val cluster = Cluster(system)
|
2012-08-16 18:28:01 +02:00
|
|
|
def clusterView = cluster.readView
|
2012-06-05 22:16:15 +02:00
|
|
|
|
2012-09-12 09:23:02 +02:00
|
|
|
def leaderActions(): Unit =
|
2012-07-04 14:39:27 +02:00
|
|
|
cluster.clusterCore ! LeaderActionsTick
|
2012-06-05 22:16:15 +02:00
|
|
|
|
|
|
|
|
"A Cluster" must {
|
|
|
|
|
|
2012-06-21 10:58:35 +02:00
|
|
|
"use the address of the remote transport" in {
|
2013-12-17 14:25:56 +01:00
|
|
|
cluster.selfAddress should be(selfAddress)
|
2012-06-21 10:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-05 11:56:54 +02:00
|
|
|
"register jmx mbean" in {
|
|
|
|
|
val name = new ObjectName("akka:type=Cluster")
|
|
|
|
|
val info = ManagementFactory.getPlatformMBeanServer.getMBeanInfo(name)
|
2013-12-17 14:25:56 +01:00
|
|
|
info.getAttributes.length should be > (0)
|
|
|
|
|
info.getOperations.length should be > (0)
|
2012-07-05 11:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-25 21:07:44 +02:00
|
|
|
"initially become singleton cluster when joining itself and reach convergence" in {
|
2013-12-17 14:25:56 +01:00
|
|
|
clusterView.members.size should be(0)
|
2012-06-25 21:07:44 +02:00
|
|
|
cluster.join(selfAddress)
|
2013-03-05 21:05:11 +01:00
|
|
|
leaderActions() // Joining -> Up
|
2012-08-16 18:28:01 +02:00
|
|
|
awaitCond(clusterView.isSingletonCluster)
|
2013-12-17 14:25:56 +01:00
|
|
|
clusterView.self.address should be(selfAddress)
|
|
|
|
|
clusterView.members.map(_.address) should be(Set(selfAddress))
|
|
|
|
|
awaitAssert(clusterView.status should be(MemberStatus.Up))
|
2012-09-12 09:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-08 14:14:48 +01:00
|
|
|
"publish inital state as snapshot to subscribers" in {
|
|
|
|
|
try {
|
|
|
|
|
cluster.subscribe(testActor, ClusterEvent.InitialStateAsSnapshot, classOf[ClusterEvent.MemberEvent])
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
|
|
|
|
} finally {
|
|
|
|
|
cluster.unsubscribe(testActor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"publish inital state as events to subscribers" in {
|
|
|
|
|
try {
|
|
|
|
|
cluster.subscribe(testActor, ClusterEvent.InitialStateAsEvents, classOf[ClusterEvent.MemberEvent])
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.MemberUp])
|
|
|
|
|
} finally {
|
|
|
|
|
cluster.unsubscribe(testActor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-12 09:23:02 +02:00
|
|
|
"send CurrentClusterState to one receiver when requested" in {
|
|
|
|
|
cluster.sendCurrentClusterState(testActor)
|
2013-01-14 17:35:56 +01:00
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
// this should be the last test step, since the cluster is shutdown
|
2013-02-11 10:40:01 +01:00
|
|
|
"publish MemberRemoved when shutdown" in {
|
|
|
|
|
cluster.subscribe(testActor, classOf[ClusterEvent.MemberRemoved])
|
|
|
|
|
// first, is in response to the subscription
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
|
|
|
|
|
|
|
|
|
cluster.shutdown()
|
2013-12-17 14:25:56 +01:00
|
|
|
expectMsgType[ClusterEvent.MemberRemoved].member.address should be(selfAddress)
|
2013-02-11 10:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
}
|