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
|
2011-11-21 10:48:21 +01:00
|
|
|
import akka.util.Duration
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-10-21 17:01:22 +02:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2011-10-11 16:05:48 +02:00
|
|
|
class PriorityDispatcherSpec extends AkkaSpec {
|
2011-04-11 17:08:10 +02:00
|
|
|
|
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-10-04 20:10:04 +02:00
|
|
|
testOrdering(UnboundedPriorityMailbox(PriorityGenerator({
|
|
|
|
|
case i: Int ⇒ i //Reverse order
|
|
|
|
|
case 'Result ⇒ Int.MaxValue
|
|
|
|
|
}: Any ⇒ Int)))
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Order it's messages according to the specified comparator using a bounded mailbox" in {
|
2011-10-04 20:10:04 +02:00
|
|
|
testOrdering(BoundedPriorityMailbox(PriorityGenerator({
|
|
|
|
|
case i: Int ⇒ i //Reverse order
|
|
|
|
|
case 'Result ⇒ Int.MaxValue
|
2011-11-17 11:51:14 +01:00
|
|
|
}: Any ⇒ Int), 1000, system.settings.MailboxPushTimeout))
|
2011-04-11 17:08:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def testOrdering(mboxType: MailboxType) {
|
2011-11-21 10:48:21 +01:00
|
|
|
val dispatcher = system.dispatcherFactory.newDispatcher("Test", 1, Duration.Zero, mboxType).build
|
2011-04-11 17:08:10 +02:00
|
|
|
|
2011-10-18 17:56:23 +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-10-22 16:06:20 +02:00
|
|
|
case 'Result ⇒ sender.tell(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|