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

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

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

a, actor, akka, controlawaredispatcher, controlawaredispatcherspec, controlmessage, defaulttimeout, dispatch, importantmessage, string, test, testing, testkit

The ControlAwareDispatcherSpec.scala Akka example source code

package akka.dispatch

import akka.testkit.{ DefaultTimeout, AkkaSpec }
import akka.actor.{ Actor, Props }

object ControlAwareDispatcherSpec {
  val config = """
    unbounded-control-dispatcher {
      mailbox-type = "akka.dispatch.UnboundedControlAwareMailbox"
    }
    bounded-control-dispatcher {
      mailbox-type = "akka.dispatch.BoundedControlAwareMailbox"
    }
    """

  case object ImportantMessage extends ControlMessage
}

@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class ControlAwareDispatcherSpec extends AkkaSpec(ControlAwareDispatcherSpec.config) with DefaultTimeout {
  import ControlAwareDispatcherSpec.ImportantMessage

  "A ControlAwareDispatcher" must {
    "deliver control messages first using an unbounded mailbox" in {
      val dispatcherKey = "unbounded-control-dispatcher"
      testControl(dispatcherKey)
    }

    "deliver control messages first using a bounded mailbox" in {
      val dispatcherKey = "bounded-control-dispatcher"
      testControl(dispatcherKey)
    }
  }

  def testControl(dispatcherKey: String) = {
    // It's important that the actor under test is not a top level actor
    // with RepointableActorRef, since messages might be queued in
    // UnstartedCell and the sent to the PriorityQueue and consumed immediately
    // without the ordering taking place.
    val actor = system.actorOf(Props(new Actor {
      context.actorOf(Props(new Actor {

        self ! "test"
        self ! "test2"
        self ! ImportantMessage

        def receive = {
          case x ⇒ testActor ! x
        }
      }).withDispatcher(dispatcherKey))

      def receive = Actor.emptyBehavior

    }))

    expectMsg(ImportantMessage)
    expectMsg("test")
    expectMsg("test2")
  }
}

Other Akka source code examples

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