2011-04-11 17:08:10 +02:00
|
|
|
package akka.dispatch
|
|
|
|
|
|
2011-08-26 17:25:18 +02:00
|
|
|
import akka.actor.{ Props, LocalActorRef, Actor }
|
2011-10-11 16:05:48 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
2012-01-18 10:18:51 +01:00
|
|
|
import akka.pattern.ask
|
2011-12-21 20:34:46 +01:00
|
|
|
import akka.util.duration._
|
2011-12-05 20:01:42 +01:00
|
|
|
import akka.testkit.DefaultTimeout
|
2011-12-21 20:34:46 +01:00
|
|
|
import com.typesafe.config.Config
|
2012-02-26 21:26:25 +01:00
|
|
|
import akka.actor.ActorSystem
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
object PriorityDispatcherSpec {
|
|
|
|
|
val config = """
|
|
|
|
|
unbounded-prio-dispatcher {
|
2012-02-09 20:40:09 +01:00
|
|
|
mailbox-type = "akka.dispatch.PriorityDispatcherSpec$Unbounded"
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
|
|
|
|
bounded-prio-dispatcher {
|
2012-02-09 20:40:09 +01:00
|
|
|
mailbox-type = "akka.dispatch.PriorityDispatcherSpec$Bounded"
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
|
|
|
|
"""
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2012-02-26 21:26:25 +01:00
|
|
|
class Unbounded(settings: ActorSystem.Settings, config: Config) extends UnboundedPriorityMailbox(PriorityGenerator({
|
2011-12-21 20:34:46 +01:00
|
|
|
case i: Int ⇒ i //Reverse order
|
|
|
|
|
case 'Result ⇒ Int.MaxValue
|
|
|
|
|
}: Any ⇒ Int))
|
2011-12-20 21:08:27 +01:00
|
|
|
|
2012-02-26 21:26:25 +01:00
|
|
|
class Bounded(settings: ActorSystem.Settings, config: Config) extends BoundedPriorityMailbox(PriorityGenerator({
|
2011-12-21 20:34:46 +01:00
|
|
|
case i: Int ⇒ i //Reverse order
|
|
|
|
|
case 'Result ⇒ Int.MaxValue
|
|
|
|
|
}: Any ⇒ Int), 1000, 10 seconds)
|
2011-12-20 21:08:27 +01:00
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
}
|
2011-12-20 21:08:27 +01:00
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
|
|
|
|
class PriorityDispatcherSpec extends AkkaSpec(PriorityDispatcherSpec.config) with DefaultTimeout {
|
2011-12-20 21:08:27 +01:00
|
|
|
|
2011-12-21 20:34:46 +01:00
|
|
|
"A PriorityDispatcher" must {
|
|
|
|
|
"Order it's messages according to the specified comparator using an unbounded mailbox" in {
|
|
|
|
|
val dispatcherKey = "unbounded-prio-dispatcher"
|
2011-12-20 21:08:27 +01:00
|
|
|
testOrdering(dispatcherKey)
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Order it's messages according to the specified comparator using a bounded mailbox" in {
|
2011-12-20 21:08:27 +01:00
|
|
|
val dispatcherKey = "bounded-prio-dispatcher"
|
|
|
|
|
testOrdering(dispatcherKey)
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 21:08:27 +01:00
|
|
|
def testOrdering(dispatcherKey: String) {
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-12-07 11:10:54 +01:00
|
|
|
val actor = system.actorOf(Props(new Actor {
|
2011-05-18 17:25:30 +02:00
|
|
|
var acc: List[Int] = Nil
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
def receive = {
|
|
|
|
|
case i: Int ⇒ acc = i :: acc
|
2011-10-22 16:06:20 +02:00
|
|
|
case 'Result ⇒ sender.tell(acc)
|
2011-05-18 17:25:30 +02:00
|
|
|
}
|
2011-12-20 21:08:27 +01:00
|
|
|
}).withDispatcher(dispatcherKey)).asInstanceOf[LocalActorRef]
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-09-15 08:12:07 +02:00
|
|
|
actor.suspend //Make sure the actor isn't treating any messages, let it buffer the incoming messages
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
val msgs = (1 to 100).toList
|
|
|
|
|
for (m ← msgs) actor ! m
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-09-15 08:12:07 +02:00
|
|
|
actor.resume //Signal the actor to start treating it's message backlog
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-12-12 22:50:08 +01:00
|
|
|
Await.result(actor.?('Result).mapTo[List[Int]], timeout.duration) must be === msgs.reverse
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|