2012-11-08 18:49:54 +01:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
|
2012-11-08 18:49:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.cluster.routing
|
|
|
|
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import akka.actor.Address
|
|
|
|
|
import akka.actor.RootActorPath
|
|
|
|
|
import akka.testkit.AkkaSpec
|
2013-09-19 08:00:05 +02:00
|
|
|
import akka.routing.ActorSelectionRoutee
|
|
|
|
|
import akka.routing.ActorRefRoutee
|
2012-11-08 18:49:54 +01:00
|
|
|
|
|
|
|
|
class WeightedRouteesSpec extends AkkaSpec(ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
|
2013-01-17 16:19:31 +01:00
|
|
|
akka.remote.netty.tcp.port = 0
|
2012-11-08 18:49:54 +01:00
|
|
|
""")) {
|
|
|
|
|
|
2013-01-24 04:28:21 -08:00
|
|
|
val a1 = Address("akka.tcp", "sys", "a1", 2551)
|
|
|
|
|
val b1 = Address("akka.tcp", "sys", "b1", 2551)
|
|
|
|
|
val c1 = Address("akka.tcp", "sys", "c1", 2551)
|
|
|
|
|
val d1 = Address("akka.tcp", "sys", "d1", 2551)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
val routeeA = ActorSelectionRoutee(system.actorSelection(RootActorPath(a1) / "user" / "a"))
|
|
|
|
|
val routeeB = ActorSelectionRoutee(system.actorSelection(RootActorPath(b1) / "user" / "b"))
|
|
|
|
|
val routeeC = ActorSelectionRoutee(system.actorSelection(RootActorPath(c1) / "user" / "c"))
|
|
|
|
|
val routees = Vector(routeeA, routeeB, routeeC)
|
|
|
|
|
val testActorRoutee = ActorRefRoutee(testActor)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
|
|
|
|
"WeightedRoutees" must {
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
"allocate weighted routees" in {
|
2012-11-08 18:49:54 +01:00
|
|
|
val weights = Map(a1 -> 1, b1 -> 3, c1 -> 10)
|
2013-09-19 08:00:05 +02:00
|
|
|
val weighted = new WeightedRoutees(routees, a1, weights)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
weighted(1) should ===(routeeA)
|
|
|
|
|
2 to 4 foreach { weighted(_) should ===(routeeB) }
|
|
|
|
|
5 to 14 foreach { weighted(_) should ===(routeeC) }
|
|
|
|
|
weighted.total should ===(14)
|
2012-11-08 18:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"check boundaries" in {
|
|
|
|
|
val empty = new WeightedRoutees(Vector(), a1, Map.empty)
|
2015-01-16 11:09:59 +01:00
|
|
|
empty.isEmpty should ===(true)
|
2012-11-16 13:35:10 +01:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
empty.total
|
|
|
|
|
}
|
2013-08-16 12:04:36 +02:00
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
val empty2 = new WeightedRoutees(Vector(routeeA), a1, Map(a1 -> 0))
|
2015-01-16 11:09:59 +01:00
|
|
|
empty2.isEmpty should ===(true)
|
2013-08-16 12:04:36 +02:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
empty2.total
|
|
|
|
|
}
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
empty2(0)
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
val weighted = new WeightedRoutees(routees, a1, Map.empty)
|
2015-01-16 11:09:59 +01:00
|
|
|
weighted.total should ===(3)
|
2012-11-08 18:49:54 +01:00
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
weighted(0)
|
|
|
|
|
}
|
|
|
|
|
intercept[IllegalArgumentException] {
|
|
|
|
|
weighted(4)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
"allocate routees for undefined weight" in {
|
2012-11-08 18:49:54 +01:00
|
|
|
val weights = Map(a1 -> 1, b1 -> 7)
|
2013-09-19 08:00:05 +02:00
|
|
|
val weighted = new WeightedRoutees(routees, a1, weights)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
weighted(1) should ===(routeeA)
|
|
|
|
|
2 to 8 foreach { weighted(_) should ===(routeeB) }
|
2012-11-08 18:49:54 +01:00
|
|
|
// undefined, uses the mean of the weights, i.e. 4
|
2015-01-16 11:09:59 +01:00
|
|
|
9 to 12 foreach { weighted(_) should ===(routeeC) }
|
|
|
|
|
weighted.total should ===(12)
|
2012-11-08 18:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-19 08:00:05 +02:00
|
|
|
"allocate weighted local routees" in {
|
2012-11-08 18:49:54 +01:00
|
|
|
val weights = Map(a1 -> 2, b1 -> 1, c1 -> 10)
|
2013-09-19 08:00:05 +02:00
|
|
|
val routees2 = Vector(testActorRoutee, routeeB, routeeC)
|
|
|
|
|
val weighted = new WeightedRoutees(routees2, a1, weights)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
1 to 2 foreach { weighted(_) should ===(testActorRoutee) }
|
2013-12-17 14:25:56 +01:00
|
|
|
3 to weighted.total foreach { weighted(_) should not be (testActorRoutee) }
|
2012-11-08 18:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not allocate ref with weight zero" in {
|
|
|
|
|
val weights = Map(a1 -> 0, b1 -> 2, c1 -> 10)
|
2013-09-19 08:00:05 +02:00
|
|
|
val weighted = new WeightedRoutees(routees, a1, weights)
|
2012-11-08 18:49:54 +01:00
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
1 to weighted.total foreach { weighted(_) should not be (routeeA) }
|
2012-11-08 18:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|