2011-04-11 17:08:10 +02:00
|
|
|
package akka.dispatch
|
|
|
|
|
|
|
|
|
|
import akka.actor.Actor._
|
|
|
|
|
import org.scalatest.WordSpec
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
2011-08-26 17:25:18 +02:00
|
|
|
import akka.actor.{ Props, LocalActorRef, Actor }
|
2011-04-11 17:08:10 +02:00
|
|
|
|
|
|
|
|
class PriorityDispatcherSpec extends WordSpec with MustMatchers {
|
|
|
|
|
|
2011-05-20 22:41:41 +02:00
|
|
|
"A PriorityDispatcher" must {
|
2011-04-11 17:08:10 +02:00
|
|
|
"Order it's messages according to the specified comparator using an unbounded mailbox" in {
|
2011-04-27 12:21:19 +02:00
|
|
|
testOrdering(UnboundedMailbox())
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Order it's messages according to the specified comparator using a bounded mailbox" in {
|
2011-04-27 12:21:19 +02:00
|
|
|
testOrdering(BoundedMailbox(1000))
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def testOrdering(mboxType: MailboxType) {
|
2011-05-20 22:41:41 +02:00
|
|
|
val dispatcher = new PriorityDispatcher("Test",
|
2011-05-18 17:25:30 +02:00
|
|
|
PriorityGenerator({
|
|
|
|
|
case i: Int ⇒ i //Reverse order
|
|
|
|
|
case 'Result ⇒ Int.MaxValue
|
|
|
|
|
}: Any ⇒ Int),
|
|
|
|
|
throughput = 1,
|
|
|
|
|
mailboxType = mboxType)
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-08-26 17:25:18 +02:00
|
|
|
val actor = 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-07-17 09:02:36 +03:00
|
|
|
case 'Result ⇒ self tryReply acc
|
2011-05-18 17:25:30 +02:00
|
|
|
}
|
2011-08-26 17:25:18 +02:00
|
|
|
}).withDispatcher(dispatcher)).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-06-14 00:19:54 +02:00
|
|
|
actor.?('Result).as[List[Int]].get must be === (msgs.reverse)
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|