pekko/akka-actor-tests/src/test/scala/akka/dispatch/StablePriorityDispatcherSpec.scala

99 lines
3.5 KiB
Scala
Raw Normal View History

/*
2021-01-08 17:55:38 +01:00
* Copyright (C) 2015-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.dispatch
import scala.concurrent.duration._
import com.typesafe.config.Config
import language.postfixOps
import akka.actor.{ Actor, ActorSystem, Props }
2019-03-11 10:38:24 +01:00
import akka.testkit.{ AkkaSpec, DefaultTimeout }
import akka.util.unused
object StablePriorityDispatcherSpec {
2019-05-24 08:11:50 +02:00
case object Result
val config = """
unbounded-stable-prio-dispatcher {
mailbox-type = "akka.dispatch.StablePriorityDispatcherSpec$Unbounded"
}
bounded-stable-prio-dispatcher {
mailbox-type = "akka.dispatch.StablePriorityDispatcherSpec$Bounded"
}
"""
class Unbounded(@unused settings: ActorSystem.Settings, @unused config: Config)
2019-03-11 10:38:24 +01:00
extends UnboundedStablePriorityMailbox(PriorityGenerator({
case i: Int if i <= 100 => i // Small integers have high priority
case _: Int => 101 // Don't care for other integers
2019-05-24 08:11:50 +02:00
case Result => Int.MaxValue
2021-03-30 20:57:23 +02:00
case _ => throw new RuntimeException() // compiler exhaustiveness check pleaser
2019-03-11 10:38:24 +01:00
}: Any => Int))
class Bounded(@unused settings: ActorSystem.Settings, @unused config: Config)
2019-03-11 10:38:24 +01:00
extends BoundedStablePriorityMailbox(PriorityGenerator({
case i: Int if i <= 100 => i // Small integers have high priority
case _: Int => 101 // Don't care for other integers
2019-05-24 08:11:50 +02:00
case Result => Int.MaxValue
2021-03-30 20:57:23 +02:00
case _ => throw new RuntimeException() // compiler exhaustiveness check pleaser
2019-03-11 10:38:24 +01:00
}: Any => Int), 1000, 10 seconds)
}
class StablePriorityDispatcherSpec extends AkkaSpec(StablePriorityDispatcherSpec.config) with DefaultTimeout {
2019-05-24 08:11:50 +02:00
import StablePriorityDispatcherSpec._
"A StablePriorityDispatcher" must {
"Order its messages according to the specified comparator while preserving FIFO for equal priority messages, " +
2019-03-11 10:38:24 +01:00
"using an unbounded mailbox" in {
val dispatcherKey = "unbounded-stable-prio-dispatcher"
testOrdering(dispatcherKey)
}
"Order its messages according to the specified comparator while preserving FIFO for equal priority messages, " +
2019-03-11 10:38:24 +01:00
"using a bounded mailbox" in {
val dispatcherKey = "bounded-stable-prio-dispatcher"
testOrdering(dispatcherKey)
}
2018-07-25 20:38:27 +09:00
def testOrdering(dispatcherKey: String): Unit = {
val msgs = (1 to 200) toList
val shuffled = scala.util.Random.shuffle(msgs)
// It's important that the actor under test is not a top level actor
// with RepointableActorRef, since messages might be queued in
// UnstartedCell and then sent to the StablePriorityQueue and consumed immediately
// without the ordering taking place.
system.actorOf(Props(new Actor {
context.actorOf(Props(new Actor {
val acc = scala.collection.mutable.ListBuffer[Int]()
2019-03-11 10:38:24 +01:00
shuffled.foreach { m =>
self ! m
}
2019-05-24 08:11:50 +02:00
self.tell(Result, testActor)
def receive = {
2019-05-24 08:11:50 +02:00
case i: Int => acc += i
case Result => sender() ! acc.toList
}
}).withDispatcher(dispatcherKey))
def receive = Actor.emptyBehavior
}))
// Low messages should come out first, and in priority order. High messages follow - they are equal priority and
// should come out in the same order in which they were sent.
val lo = (1 to 100) toList
2019-03-11 10:38:24 +01:00
val hi = shuffled.filter { _ > 100 }
(expectMsgType[List[Int]]: List[Int]) should ===(lo ++ hi)
}
}
2015-01-16 11:09:59 +01:00
}