2012-09-13 10:54:14 +02:00
|
|
|
package sample.cluster.stats
|
|
|
|
|
|
|
|
|
|
import language.postfixOps
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.RootActorPath
|
|
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
import akka.cluster.Member
|
|
|
|
|
import akka.cluster.MemberStatus
|
|
|
|
|
import akka.cluster.ClusterEvent.CurrentClusterState
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberUp
|
2012-09-21 11:47:50 +02:00
|
|
|
|
|
|
|
|
//#MultiNodeConfig
|
2012-09-13 10:54:14 +02:00
|
|
|
import akka.remote.testkit.MultiNodeConfig
|
2012-09-21 11:47:50 +02:00
|
|
|
import com.typesafe.config.ConfigFactory
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
object StatsSampleSpecConfig extends MultiNodeConfig {
|
2012-09-13 10:54:14 +02:00
|
|
|
// register the named roles (nodes) of the test
|
|
|
|
|
val first = role("first")
|
|
|
|
|
val second = role("second")
|
|
|
|
|
val third = role("thrid")
|
|
|
|
|
|
|
|
|
|
// this configuration will be used for all nodes
|
|
|
|
|
// note that no fixed host names and ports are used
|
|
|
|
|
commonConfig(ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
|
|
|
|
|
akka.remote.log-remote-lifecycle-events = off
|
|
|
|
|
akka.cluster.auto-join = off
|
|
|
|
|
#//#router-lookup-config
|
|
|
|
|
akka.actor.deployment {
|
|
|
|
|
/statsService/workerRouter {
|
2012-09-20 12:58:51 +02:00
|
|
|
router = consistent-hashing
|
2012-09-13 10:54:14 +02:00
|
|
|
nr-of-instances = 100
|
|
|
|
|
cluster {
|
|
|
|
|
enabled = on
|
|
|
|
|
routees-path = "/user/statsWorker"
|
|
|
|
|
allow-local-routees = on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#//#router-lookup-config
|
|
|
|
|
"""))
|
|
|
|
|
|
|
|
|
|
}
|
2012-09-21 11:47:50 +02:00
|
|
|
//#MultiNodeConfig
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
//#concrete-tests
|
2012-09-13 10:54:14 +02:00
|
|
|
// need one concrete test class per node
|
2012-09-21 11:47:50 +02:00
|
|
|
class StatsSampleSpecMultiJvmNode1 extends StatsSampleSpec
|
|
|
|
|
class StatsSampleSpecMultiJvmNode2 extends StatsSampleSpec
|
|
|
|
|
class StatsSampleSpecMultiJvmNode3 extends StatsSampleSpec
|
|
|
|
|
//#concrete-tests
|
|
|
|
|
|
|
|
|
|
//#abstract-test
|
|
|
|
|
import org.scalatest.BeforeAndAfterAll
|
|
|
|
|
import org.scalatest.WordSpec
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import akka.remote.testkit.MultiNodeSpec
|
|
|
|
|
import akka.testkit.ImplicitSender
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
abstract class StatsSampleSpec extends MultiNodeSpec(StatsSampleSpecConfig)
|
|
|
|
|
with WordSpec with MustMatchers with BeforeAndAfterAll
|
|
|
|
|
with ImplicitSender {
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
import StatsSampleSpecConfig._
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
override def initialParticipants = roles.size
|
|
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
override def beforeAll() = multiNodeSpecBeforeAll()
|
|
|
|
|
|
|
|
|
|
override def afterAll() = multiNodeSpecAfterAll()
|
|
|
|
|
|
|
|
|
|
//#abstract-test
|
|
|
|
|
|
2012-09-13 10:54:14 +02:00
|
|
|
"The stats sample" must {
|
2012-09-21 11:47:50 +02:00
|
|
|
|
|
|
|
|
//#startup-cluster
|
2012-09-13 10:54:14 +02:00
|
|
|
"illustrate how to startup cluster" in within(10 seconds) {
|
|
|
|
|
Cluster(system).subscribe(testActor, classOf[MemberUp])
|
|
|
|
|
expectMsgClass(classOf[CurrentClusterState])
|
|
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
//#addresses
|
|
|
|
|
val firstAddress = node(first).address
|
|
|
|
|
val secondAddress = node(second).address
|
|
|
|
|
val thirdAddress = node(third).address
|
|
|
|
|
//#addresses
|
|
|
|
|
|
|
|
|
|
//#join
|
|
|
|
|
Cluster(system) join firstAddress
|
|
|
|
|
//#join
|
|
|
|
|
|
2012-09-13 10:54:14 +02:00
|
|
|
system.actorOf(Props[StatsWorker], "statsWorker")
|
|
|
|
|
system.actorOf(Props[StatsService], "statsService")
|
|
|
|
|
|
|
|
|
|
expectMsgAllOf(
|
2012-09-21 11:47:50 +02:00
|
|
|
MemberUp(Member(firstAddress, MemberStatus.Up)),
|
|
|
|
|
MemberUp(Member(secondAddress, MemberStatus.Up)),
|
|
|
|
|
MemberUp(Member(thirdAddress, MemberStatus.Up)))
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
Cluster(system).unsubscribe(testActor)
|
|
|
|
|
|
|
|
|
|
testConductor.enter("all-up")
|
|
|
|
|
}
|
2012-09-21 11:47:50 +02:00
|
|
|
//#startup-cluster
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
//#test-statsService
|
|
|
|
|
"show usage of the statsService from one node" in within(5 seconds) {
|
|
|
|
|
runOn(second) {
|
|
|
|
|
val service = system.actorFor(node(third) / "user" / "statsService")
|
|
|
|
|
service ! StatsJob("this is the text that will be analyzed")
|
|
|
|
|
val meanWordLength = expectMsgPF() {
|
|
|
|
|
case StatsResult(meanWordLength) ⇒ meanWordLength
|
|
|
|
|
}
|
|
|
|
|
meanWordLength must be(3.875 plusOrMinus 0.001)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testConductor.enter("done-2")
|
|
|
|
|
}
|
|
|
|
|
//#test-statsService
|
|
|
|
|
|
|
|
|
|
"show usage of the statsService from all nodes" in within(5 seconds) {
|
|
|
|
|
val service = system.actorFor(node(third) / "user" / "statsService")
|
2012-09-13 10:54:14 +02:00
|
|
|
service ! StatsJob("this is the text that will be analyzed")
|
|
|
|
|
val meanWordLength = expectMsgPF() {
|
|
|
|
|
case StatsResult(meanWordLength) ⇒ meanWordLength
|
|
|
|
|
}
|
|
|
|
|
meanWordLength must be(3.875 plusOrMinus 0.001)
|
|
|
|
|
|
2012-10-24 17:59:49 +02:00
|
|
|
testConductor.enter("done-3")
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
2012-09-21 11:47:50 +02:00
|
|
|
|
|
|
|
|
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|