2013-09-19 08:00:05 +02:00
|
|
|
/**
|
2018-01-04 17:26:29 +00:00
|
|
|
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
2013-09-19 08:00:05 +02:00
|
|
|
*/
|
|
|
|
|
package akka.routing
|
|
|
|
|
|
|
|
|
|
import language.postfixOps
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import scala.concurrent.Await
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import akka.actor.{ Props, Actor }
|
|
|
|
|
import akka.testkit.{ TestLatch, ImplicitSender, DefaultTimeout, AkkaSpec }
|
|
|
|
|
import akka.pattern.ask
|
|
|
|
|
|
|
|
|
|
class RandomSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|
|
|
|
|
|
|
|
|
"random pool" must {
|
|
|
|
|
|
|
|
|
|
"be able to shut down its instance" in {
|
|
|
|
|
val stopLatch = new TestLatch(7)
|
|
|
|
|
|
|
|
|
|
val actor = system.actorOf(RandomPool(7).props(Props(new Actor {
|
|
|
|
|
def receive = {
|
2014-01-16 15:16:35 +01:00
|
|
|
case "hello" ⇒ sender() ! "world"
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
})), "random-shutdown")
|
|
|
|
|
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
actor ! "hello"
|
|
|
|
|
|
|
|
|
|
within(2 seconds) {
|
|
|
|
|
for (i ← 1 to 5) expectMsg("world")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
system.stop(actor)
|
|
|
|
|
Await.ready(stopLatch, 5 seconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver messages in a random fashion" in {
|
|
|
|
|
val connectionCount = 10
|
|
|
|
|
val iterationCount = 100
|
|
|
|
|
val doneLatch = new TestLatch(connectionCount)
|
|
|
|
|
|
|
|
|
|
val counter = new AtomicInteger
|
|
|
|
|
var replies = Map.empty[Int, Int]
|
|
|
|
|
for (i ← 0 until connectionCount) {
|
2016-06-02 14:06:57 +02:00
|
|
|
replies = replies + (i → 0)
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val actor = system.actorOf(RandomPool(connectionCount).props(routeeProps =
|
|
|
|
|
Props(new Actor {
|
|
|
|
|
lazy val id = counter.getAndIncrement()
|
|
|
|
|
def receive = {
|
2014-01-16 15:16:35 +01:00
|
|
|
case "hit" ⇒ sender() ! id
|
2013-09-19 08:00:05 +02:00
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
})), name = "random")
|
|
|
|
|
|
|
|
|
|
for (i ← 0 until iterationCount) {
|
|
|
|
|
for (k ← 0 until connectionCount) {
|
|
|
|
|
val id = Await.result((actor ? "hit").mapTo[Int], timeout.duration)
|
2016-06-02 14:06:57 +02:00
|
|
|
replies = replies + (id → (replies(id) + 1))
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
counter.get should ===(connectionCount)
|
2013-09-19 08:00:05 +02:00
|
|
|
|
|
|
|
|
actor ! akka.routing.Broadcast("end")
|
|
|
|
|
Await.ready(doneLatch, 5 seconds)
|
|
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
replies.values foreach { _ should be > (0) }
|
2015-01-16 11:09:59 +01:00
|
|
|
replies.values.sum should ===(iterationCount * connectionCount)
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"deliver a broadcast message using the !" in {
|
|
|
|
|
val helloLatch = new TestLatch(6)
|
|
|
|
|
val stopLatch = new TestLatch(6)
|
|
|
|
|
|
|
|
|
|
val actor = system.actorOf(RandomPool(6).props(routeeProps = Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "hello" ⇒ helloLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override def postStop() {
|
|
|
|
|
stopLatch.countDown()
|
|
|
|
|
}
|
|
|
|
|
})), "random-broadcast")
|
|
|
|
|
|
|
|
|
|
actor ! akka.routing.Broadcast("hello")
|
|
|
|
|
Await.ready(helloLatch, 5 seconds)
|
|
|
|
|
|
|
|
|
|
system.stop(actor)
|
|
|
|
|
Await.ready(stopLatch, 5 seconds)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|