2011-09-28 18:15:39 +02:00
|
|
|
package akka.routing
|
|
|
|
|
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import akka.routing._
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import java.util.concurrent.{ CountDownLatch, TimeUnit }
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
|
|
|
|
import akka.actor.DeploymentConfig._
|
|
|
|
|
import akka.routing.Routing.Broadcast
|
2011-09-28 18:15:39 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
class ConfiguredLocalRoutingSpec extends AkkaSpec {
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
"round robin router" must {
|
|
|
|
|
|
|
|
|
|
"be able to shut down its instance" in {
|
|
|
|
|
val address = "round-robin-0"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
RoundRobin,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(5),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val helloLatch = new CountDownLatch(5)
|
|
|
|
|
val stopLatch = new CountDownLatch(5)
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒ helloLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
helloLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
|
|
|
|
|
actor.stop()
|
|
|
|
|
stopLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver messages in a round robin fashion" in {
|
|
|
|
|
val address = "round-robin-1"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
RoundRobin,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(10),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val connectionCount = 10
|
|
|
|
|
val iterationCount = 10
|
|
|
|
|
val doneLatch = new CountDownLatch(connectionCount)
|
|
|
|
|
|
|
|
|
|
val counter = new AtomicInteger
|
|
|
|
|
var replies = Map.empty[Int, Int]
|
|
|
|
|
for (i ← 0 until connectionCount) {
|
|
|
|
|
replies = replies + (i -> 0)
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
lazy val id = counter.getAndIncrement()
|
|
|
|
|
def receive = {
|
2011-10-19 16:59:47 +02:00
|
|
|
case "hit" ⇒ channel ! id
|
2011-09-28 18:15:39 +02:00
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
for (i ← 0 until iterationCount) {
|
|
|
|
|
for (k ← 0 until connectionCount) {
|
|
|
|
|
val id = (actor ? "hit").as[Int].getOrElse(fail("No id returned by actor"))
|
|
|
|
|
replies = replies + (id -> (replies(id) + 1))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
counter.get must be(connectionCount)
|
|
|
|
|
|
|
|
|
|
actor ! Broadcast("end")
|
|
|
|
|
doneLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
|
|
|
|
|
replies.values foreach { _ must be(10) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver a broadcast message using the !" in {
|
|
|
|
|
val address = "round-robin-2"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
RoundRobin,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(5),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val helloLatch = new CountDownLatch(5)
|
|
|
|
|
val stopLatch = new CountDownLatch(5)
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒ helloLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
actor ! Broadcast("hello")
|
|
|
|
|
helloLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
|
|
|
|
|
actor.stop()
|
|
|
|
|
stopLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"random router" must {
|
|
|
|
|
|
|
|
|
|
"be able to shut down its instance" in {
|
|
|
|
|
val address = "random-0"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
Random,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(7),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val stopLatch = new CountDownLatch(7)
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒ {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
|
|
|
|
|
actor.stop()
|
|
|
|
|
stopLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver messages in a random fashion" in {
|
|
|
|
|
val address = "random-1"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
Random,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(10),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val connectionCount = 10
|
|
|
|
|
val iterationCount = 10
|
|
|
|
|
val doneLatch = new CountDownLatch(connectionCount)
|
|
|
|
|
|
|
|
|
|
val counter = new AtomicInteger
|
|
|
|
|
var replies = Map.empty[Int, Int]
|
|
|
|
|
for (i ← 0 until connectionCount) {
|
|
|
|
|
replies = replies + (i -> 0)
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
lazy val id = counter.getAndIncrement()
|
|
|
|
|
def receive = {
|
2011-10-19 16:59:47 +02:00
|
|
|
case "hit" ⇒ channel ! id
|
2011-09-28 18:15:39 +02:00
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
for (i ← 0 until iterationCount) {
|
|
|
|
|
for (k ← 0 until connectionCount) {
|
|
|
|
|
val id = (actor ? "hit").as[Int].getOrElse(fail("No id returned by actor"))
|
|
|
|
|
replies = replies + (id -> (replies(id) + 1))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
counter.get must be(connectionCount)
|
|
|
|
|
|
|
|
|
|
actor ! Broadcast("end")
|
|
|
|
|
doneLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
|
|
|
|
|
replies.values foreach { _ must be > (0) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver a broadcast message using the !" in {
|
|
|
|
|
val address = "random-2"
|
|
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
app.deployer.deploy(
|
2011-09-28 18:15:39 +02:00
|
|
|
Deploy(
|
|
|
|
|
address,
|
|
|
|
|
None,
|
|
|
|
|
Random,
|
2011-09-28 19:42:12 +02:00
|
|
|
NrOfInstances(6),
|
2011-10-07 15:42:55 +02:00
|
|
|
NoOpFailureDetector,
|
2011-09-28 18:15:39 +02:00
|
|
|
LocalScope))
|
|
|
|
|
|
|
|
|
|
val helloLatch = new CountDownLatch(6)
|
|
|
|
|
val stopLatch = new CountDownLatch(6)
|
|
|
|
|
|
2011-10-18 17:56:23 +02:00
|
|
|
val actor = app.actorOf(Props(new Actor {
|
2011-09-28 18:15:39 +02:00
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒ helloLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
}), address)
|
2011-09-28 18:15:39 +02:00
|
|
|
|
|
|
|
|
actor ! Broadcast("hello")
|
|
|
|
|
helloLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
|
|
|
|
|
actor.stop()
|
|
|
|
|
stopLatch.await(5, TimeUnit.SECONDS) must be(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|