2013-09-19 08:00:05 +02:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-09-19 08:00:05 +02:00
|
|
|
*/
|
|
|
|
|
package akka.routing
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import scala.concurrent.Await
|
|
|
|
|
import akka.actor.{ Props, Actor }
|
|
|
|
|
import akka.testkit.{ TestLatch, ImplicitSender, DefaultTimeout, AkkaSpec }
|
|
|
|
|
import akka.pattern.ask
|
|
|
|
|
|
|
|
|
|
object BroadcastSpec {
|
|
|
|
|
class TestActor extends Actor {
|
|
|
|
|
def receive = { case _ ⇒ }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class BroadcastSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|
|
|
|
|
|
|
|
|
"broadcast group" must {
|
|
|
|
|
|
|
|
|
|
"broadcast message using !" in {
|
|
|
|
|
val doneLatch = new TestLatch(2)
|
|
|
|
|
|
|
|
|
|
val counter1 = new AtomicInteger
|
|
|
|
|
val actor1 = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
case msg: Int ⇒ counter1.addAndGet(msg)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
val counter2 = new AtomicInteger
|
|
|
|
|
val actor2 = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
case msg: Int ⇒ counter2.addAndGet(msg)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
val paths = List(actor1, actor2).map(_.path.toString)
|
|
|
|
|
val routedActor = system.actorOf(BroadcastGroup(paths).props())
|
|
|
|
|
routedActor ! 1
|
|
|
|
|
routedActor ! "end"
|
|
|
|
|
|
2014-03-11 11:23:12 +01:00
|
|
|
Await.ready(doneLatch, remainingOrDefault)
|
2013-09-19 08:00:05 +02:00
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
counter1.get should ===(1)
|
|
|
|
|
counter2.get should ===(1)
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"broadcast message using ?" in {
|
|
|
|
|
val doneLatch = new TestLatch(2)
|
|
|
|
|
|
|
|
|
|
val counter1 = new AtomicInteger
|
|
|
|
|
val actor1 = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
case msg: Int ⇒
|
|
|
|
|
counter1.addAndGet(msg)
|
2014-01-16 15:16:35 +01:00
|
|
|
sender() ! "ack"
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
val counter2 = new AtomicInteger
|
|
|
|
|
val actor2 = system.actorOf(Props(new Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case "end" ⇒ doneLatch.countDown()
|
|
|
|
|
case msg: Int ⇒ counter2.addAndGet(msg)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
val paths = List(actor1, actor2).map(_.path.toString)
|
|
|
|
|
val routedActor = system.actorOf(BroadcastGroup(paths).props())
|
|
|
|
|
routedActor ? 1
|
|
|
|
|
routedActor ! "end"
|
|
|
|
|
|
2014-03-11 11:23:12 +01:00
|
|
|
Await.ready(doneLatch, remainingOrDefault)
|
2013-09-19 08:00:05 +02:00
|
|
|
|
2015-01-16 11:09:59 +01:00
|
|
|
counter1.get should ===(1)
|
|
|
|
|
counter2.get should ===(1)
|
2013-09-19 08:00:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|