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

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

This example Akka source code file (DeployerSpec.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

akka, concurrent, defaultresizer, deploy, deployerspec, norouter, noscopegiven, not, randompool, roundrobinpool, route, scattergatherfirstcompletedpool, string, test, testing, time

The DeployerSpec.scala Akka example source code

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

package akka.actor

import language.postfixOps

import akka.testkit.AkkaSpec
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigParseOptions
import akka.routing._
import scala.concurrent.duration._

object DeployerSpec {
  val deployerConf = ConfigFactory.parseString("""
      akka.actor.deployment {
        /service1 {
        }
        /service-direct {
          router = from-code
        }
        /service-direct2 {
          router = from-code
          # nr-of-instances ignored when router = from-code
          nr-of-instances = 2
        }
        /service3 {
          dispatcher = my-dispatcher
        }
        /service4 {
          mailbox = my-mailbox
        }
        /service-round-robin {
          router = round-robin-pool
        }
        /service-random {
          router = random-pool
        }
        /service-scatter-gather {
          router = scatter-gather-pool
          within = 2 seconds
        }
        /service-consistent-hashing {
          router = consistent-hashing-pool
        }
        /service-resizer {
          router = round-robin-pool
          resizer {
            lower-bound = 1
            upper-bound = 10
          }
        }
        /some/random-service {
          router = round-robin-pool
        }
        "/some/*" {
          router = random-pool
        }
        "/*/some" {
          router = scatter-gather-pool
        }
      }
      """, ConfigParseOptions.defaults)

  class RecipeActor extends Actor {
    def receive = { case _ ⇒ }
  }

}

@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class DeployerSpec extends AkkaSpec(DeployerSpec.deployerConf) {
  "A Deployer" must {

    "be able to parse 'akka.actor.deployment._' with all default values" in {
      val service = "/service1"
      val deployment = system.asInstanceOf[ActorSystemImpl].provider.deployer.lookup(service.split("/").drop(1))

      deployment should be(Some(
        Deploy(
          service,
          deployment.get.config,
          NoRouter,
          NoScopeGiven,
          Deploy.NoDispatcherGiven,
          Deploy.NoMailboxGiven)))
    }

    "use None deployment for undefined service" in {
      val service = "/undefined"
      val deployment = system.asInstanceOf[ActorSystemImpl].provider.deployer.lookup(service.split("/").drop(1))
      deployment should be(None)
    }

    "be able to parse 'akka.actor.deployment._' with dispatcher config" in {
      val service = "/service3"
      val deployment = system.asInstanceOf[ActorSystemImpl].provider.deployer.lookup(service.split("/").drop(1))

      deployment should be(Some(
        Deploy(
          service,
          deployment.get.config,
          NoRouter,
          NoScopeGiven,
          dispatcher = "my-dispatcher",
          Deploy.NoMailboxGiven)))
    }

    "be able to parse 'akka.actor.deployment._' with mailbox config" in {
      val service = "/service4"
      val deployment = system.asInstanceOf[ActorSystemImpl].provider.deployer.lookup(service.split("/").drop(1))

      deployment should be(Some(
        Deploy(
          service,
          deployment.get.config,
          NoRouter,
          NoScopeGiven,
          Deploy.NoDispatcherGiven,
          mailbox = "my-mailbox")))
    }

    "detect invalid number-of-instances" in {
      intercept[com.typesafe.config.ConfigException.WrongType] {
        val invalidDeployerConf = ConfigFactory.parseString("""
            akka.actor.deployment {
              /service-invalid-number-of-instances {
                router = round-robin-pool
                nr-of-instances = boom
              }
            }
            """, ConfigParseOptions.defaults).withFallback(AkkaSpec.testConf)

        shutdown(ActorSystem("invalid-number-of-instances", invalidDeployerConf))
      }
    }

    "detect invalid deployment path" in {
      val e = intercept[InvalidActorNameException] {
        val invalidDeployerConf = ConfigFactory.parseString("""
            akka.actor.deployment {
              /gul/ubåt {
                router = round-robin-pool
                nr-of-instances = 2
              }
            }
            """, ConfigParseOptions.defaults).withFallback(AkkaSpec.testConf)

        shutdown(ActorSystem("invalid-path", invalidDeployerConf))
      }
      e.getMessage should include("[ubåt]")
      e.getMessage should include("[/gul/ubåt]")
    }

    "be able to parse 'akka.actor.deployment._' with from-code router" in {
      assertRouting("/service-direct", NoRouter, "/service-direct")
    }

    "ignore nr-of-instances with from-code router" in {
      assertRouting("/service-direct2", NoRouter, "/service-direct2")
    }

    "be able to parse 'akka.actor.deployment._' with round-robin router" in {
      assertRouting("/service-round-robin", RoundRobinPool(1), "/service-round-robin")
    }

    "be able to parse 'akka.actor.deployment._' with random router" in {
      assertRouting("/service-random", RandomPool(1), "/service-random")
    }

    "be able to parse 'akka.actor.deployment._' with scatter-gather router" in {
      assertRouting("/service-scatter-gather", ScatterGatherFirstCompletedPool(nrOfInstances = 1, within = 2 seconds), "/service-scatter-gather")
    }

    "be able to parse 'akka.actor.deployment._' with consistent-hashing router" in {
      assertRouting("/service-consistent-hashing", ConsistentHashingPool(1), "/service-consistent-hashing")
    }

    "be able to parse 'akka.actor.deployment._' with router resizer" in {
      val resizer = DefaultResizer()
      assertRouting("/service-resizer", RoundRobinPool(nrOfInstances = 0, resizer = Some(resizer)), "/service-resizer")
    }

    "be able to use wildcards" in {
      assertRouting("/some/wildcardmatch", RandomPool(1), "/some/*")
      assertRouting("/somewildcardmatch/some", ScatterGatherFirstCompletedPool(nrOfInstances = 1, within = 2 seconds), "/*/some")
    }

    "have correct router mappings" in {
      val mapping = system.asInstanceOf[ActorSystemImpl].provider.deployer.routerTypeMapping
      mapping("from-code") should be(classOf[akka.routing.NoRouter].getName)
      mapping("round-robin-pool") should be(classOf[akka.routing.RoundRobinPool].getName)
      mapping("round-robin-group") should be(classOf[akka.routing.RoundRobinGroup].getName)
      mapping("random-pool") should be(classOf[akka.routing.RandomPool].getName)
      mapping("random-group") should be(classOf[akka.routing.RandomGroup].getName)
      mapping("balancing-pool") should be(classOf[akka.routing.BalancingPool].getName)
      mapping("smallest-mailbox-pool") should be(classOf[akka.routing.SmallestMailboxPool].getName)
      mapping("broadcast-pool") should be(classOf[akka.routing.BroadcastPool].getName)
      mapping("broadcast-group") should be(classOf[akka.routing.BroadcastGroup].getName)
      mapping("scatter-gather-pool") should be(classOf[akka.routing.ScatterGatherFirstCompletedPool].getName)
      mapping("scatter-gather-group") should be(classOf[akka.routing.ScatterGatherFirstCompletedGroup].getName)
      mapping("consistent-hashing-pool") should be(classOf[akka.routing.ConsistentHashingPool].getName)
      mapping("consistent-hashing-group") should be(classOf[akka.routing.ConsistentHashingGroup].getName)
    }

    def assertRouting(service: String, expected: RouterConfig, expectPath: String): Unit = {
      val deployment = system.asInstanceOf[ActorSystemImpl].provider.deployer.lookup(service.split("/").drop(1))
      deployment.map(_.path).getOrElse("NOT FOUND") should be(expectPath)
      deployment.get.routerConfig.getClass should be(expected.getClass)
      deployment.get.scope should be(NoScopeGiven)
      expected match {
        case pool: Pool ⇒ deployment.get.routerConfig.asInstanceOf[Pool].resizer should be(pool.resizer)
        case _          ⇒
      }
    }
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka DeployerSpec.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.