2011-04-11 17:08:10 +02:00
|
|
|
package akka.dispatch
|
|
|
|
|
|
|
|
|
|
import akka.actor.Actor._
|
|
|
|
|
import akka.actor.Actor
|
|
|
|
|
import org.scalatest.WordSpec
|
|
|
|
|
import org.scalatest.matchers.MustMatchers
|
|
|
|
|
import java.util.concurrent.CountDownLatch
|
|
|
|
|
|
|
|
|
|
class PriorityDispatcherSpec extends WordSpec with MustMatchers {
|
|
|
|
|
|
|
|
|
|
"A PriorityExecutorBasedEventDrivenDispatcher" must {
|
|
|
|
|
"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) {
|
|
|
|
|
val dispatcher = new PriorityExecutorBasedEventDrivenDispatcher("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-05-18 17:25:30 +02:00
|
|
|
val actor = actorOf(new Actor {
|
|
|
|
|
self.dispatcher = dispatcher
|
|
|
|
|
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
|
|
|
|
|
case 'Result ⇒ self reply_? acc
|
|
|
|
|
}
|
|
|
|
|
}).start()
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
dispatcher.suspend(actor) //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-05-18 17:25:30 +02:00
|
|
|
dispatcher.resume(actor) //Signal the actor to start treating it's message backlog
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
actor.!!![List[Int]]('Result).await.result.get must be === (msgs.reverse)
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|