alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (WeightedRouteesSpec.scala)

This example Akka source code file (WeightedRouteesSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, actorrefroutee, address, akka, map, route, router, routing, testing, vector, weightedroutees, weightedrouteesspec

The WeightedRouteesSpec.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package akka.cluster.routing

import com.typesafe.config.ConfigFactory
import akka.actor.Address
import akka.actor.RootActorPath
import akka.testkit.AkkaSpec
import akka.routing.ActorSelectionRoutee
import akka.routing.ActorRefRoutee

@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class WeightedRouteesSpec extends AkkaSpec(ConfigFactory.parseString("""
      akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
      akka.remote.netty.tcp.port = 0
      """)) {

  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)

  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)

  "WeightedRoutees" must {

    "allocate weighted routees" in {
      val weights = Map(a1 -> 1, b1 -> 3, c1 -> 10)
      val weighted = new WeightedRoutees(routees, a1, weights)

      weighted(1) should be(routeeA)
      2 to 4 foreach { weighted(_) should be(routeeB) }
      5 to 14 foreach { weighted(_) should be(routeeC) }
      weighted.total should be(14)
    }

    "check boundaries" in {
      val empty = new WeightedRoutees(Vector(), a1, Map.empty)
      empty.isEmpty should be(true)
      intercept[IllegalArgumentException] {
        empty.total
      }

      val empty2 = new WeightedRoutees(Vector(routeeA), a1, Map(a1 -> 0))
      empty2.isEmpty should be(true)
      intercept[IllegalArgumentException] {
        empty2.total
      }
      intercept[IllegalArgumentException] {
        empty2(0)
      }

      val weighted = new WeightedRoutees(routees, a1, Map.empty)
      weighted.total should be(3)
      intercept[IllegalArgumentException] {
        weighted(0)
      }
      intercept[IllegalArgumentException] {
        weighted(4)
      }
    }

    "allocate routees for undefined weight" in {
      val weights = Map(a1 -> 1, b1 -> 7)
      val weighted = new WeightedRoutees(routees, a1, weights)

      weighted(1) should be(routeeA)
      2 to 8 foreach { weighted(_) should be(routeeB) }
      // undefined, uses the mean of the weights, i.e. 4
      9 to 12 foreach { weighted(_) should be(routeeC) }
      weighted.total should be(12)
    }

    "allocate weighted local routees" in {
      val weights = Map(a1 -> 2, b1 -> 1, c1 -> 10)
      val routees2 = Vector(testActorRoutee, routeeB, routeeC)
      val weighted = new WeightedRoutees(routees2, a1, weights)

      1 to 2 foreach { weighted(_) should be(testActorRoutee) }
      3 to weighted.total foreach { weighted(_) should not be (testActorRoutee) }
    }

    "not allocate ref with weight zero" in {
      val weights = Map(a1 -> 0, b1 -> 2, c1 -> 10)
      val weighted = new WeightedRoutees(routees, a1, weights)

      1 to weighted.total foreach { weighted(_) should not be (routeeA) }
    }

  }
}

Other Akka source code examples

Here is a short list of links related to this Akka WeightedRouteesSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.