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

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

This example Akka source code file (DispatcherActorsSpec.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, akkaspec, countdownlatch, dispatch, dispatcheractorsspec, fastactor, int, slowactor, test, testing, testkit

The DispatcherActorsSpec.scala Akka example source code

package akka.actor.dispatch

import java.util.concurrent.CountDownLatch
import akka.actor._
import akka.testkit.AkkaSpec

/**
 * Tests the behavior of the executor based event driven dispatcher when multiple actors are being dispatched on it.
 */
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class DispatcherActorsSpec extends AkkaSpec {
  class SlowActor(finishedCounter: CountDownLatch) extends Actor {

    def receive = {
      case x: Int ⇒ {
        Thread.sleep(50) // slow actor
        finishedCounter.countDown()
      }
    }
  }

  class FastActor(finishedCounter: CountDownLatch) extends Actor {
    def receive = {
      case x: Int ⇒ {
        finishedCounter.countDown()
      }
    }
  }

  "A dispatcher and two actors" must {
    "not block fast actors by slow actors" in {
      val sFinished = new CountDownLatch(50)
      val fFinished = new CountDownLatch(10)
      val s = system.actorOf(Props(new SlowActor(sFinished)))
      val f = system.actorOf(Props(new FastActor(fFinished)))

      // send a lot of stuff to s
      for (i ← 1 to 50) {
        s ! i
      }

      // send some messages to f
      for (i ← 1 to 10) {
        f ! i
      }

      // now assert that f is finished while s is still busy
      fFinished.await
      assert(sFinished.getCount > 0)
      sFinished.await
      assert(sFinished.getCount === 0)
      system.stop(f)
      system.stop(s)
    }
  }
}

Other Akka source code examples

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