|
Akka/Scala example source code file (DispatcherActorsSpec.scala)
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 examplesHere 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 |
Copyright 1998-2024 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.