2011-10-11 17:41:25 +02:00
|
|
|
/**
|
2014-02-02 19:05:45 -06:00
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
2011-10-11 17:41:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.event
|
|
|
|
|
|
2012-06-21 16:09:14 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2011-10-13 13:41:44 +02:00
|
|
|
import org.scalatest.BeforeAndAfterEach
|
2011-10-11 17:41:25 +02:00
|
|
|
import akka.testkit._
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2011-10-11 17:41:25 +02:00
|
|
|
import java.util.concurrent.atomic._
|
2011-12-14 00:06:36 +01:00
|
|
|
import akka.actor.{ Props, Actor, ActorRef, ActorSystem }
|
2011-10-12 11:46:49 +02:00
|
|
|
import java.util.Comparator
|
|
|
|
|
import akka.japi.{ Procedure, Function }
|
2011-10-11 17:41:25 +02:00
|
|
|
|
|
|
|
|
object EventBusSpec {
|
2011-10-12 11:46:49 +02:00
|
|
|
class TestActorWrapperActor(testActor: ActorRef) extends Actor {
|
|
|
|
|
def receive = {
|
|
|
|
|
case x ⇒ testActor forward x
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-21 17:01:22 +02:00
|
|
|
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
2011-10-13 13:41:44 +02:00
|
|
|
abstract class EventBusSpec(busName: String) extends AkkaSpec with BeforeAndAfterEach {
|
2011-10-11 17:41:25 +02:00
|
|
|
import EventBusSpec._
|
|
|
|
|
type BusType <: EventBus
|
|
|
|
|
|
|
|
|
|
def createNewEventBus(): BusType
|
|
|
|
|
|
|
|
|
|
def createEvents(numberOfEvents: Int): Iterable[BusType#Event]
|
|
|
|
|
|
|
|
|
|
def createSubscriber(pipeTo: ActorRef): BusType#Subscriber
|
|
|
|
|
|
|
|
|
|
def classifierFor(event: BusType#Event): BusType#Classifier
|
|
|
|
|
|
2011-12-14 00:06:36 +01:00
|
|
|
def disposeSubscriber(system: ActorSystem, subscriber: BusType#Subscriber): Unit
|
2011-10-11 17:41:25 +02:00
|
|
|
|
|
|
|
|
busName must {
|
|
|
|
|
|
|
|
|
|
def createNewSubscriber() = createSubscriber(testActor).asInstanceOf[bus.Subscriber]
|
|
|
|
|
def getClassifierFor(event: BusType#Event) = classifierFor(event).asInstanceOf[bus.Classifier]
|
2011-10-11 18:00:29 +02:00
|
|
|
def createNewEvents(numberOfEvents: Int): Iterable[bus.Event] = createEvents(numberOfEvents).asInstanceOf[Iterable[bus.Event]]
|
2011-10-11 17:41:25 +02:00
|
|
|
|
|
|
|
|
val bus = createNewEventBus()
|
2011-10-11 18:00:29 +02:00
|
|
|
val events = createNewEvents(100)
|
2011-10-11 17:41:25 +02:00
|
|
|
val event = events.head
|
|
|
|
|
val classifier = getClassifierFor(event)
|
|
|
|
|
val subscriber = createNewSubscriber()
|
|
|
|
|
|
|
|
|
|
"allow subscribers" in {
|
2014-01-31 11:14:13 +01:00
|
|
|
bus.subscribe(subscriber, classifier) should be(true)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"allow to unsubscribe already existing subscriber" in {
|
2014-01-31 11:14:13 +01:00
|
|
|
bus.unsubscribe(subscriber, classifier) should be(true)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not allow to unsubscribe non-existing subscriber" in {
|
|
|
|
|
val sub = createNewSubscriber()
|
2014-01-31 11:14:13 +01:00
|
|
|
bus.unsubscribe(sub, classifier) should be(false)
|
2011-12-14 00:06:36 +01:00
|
|
|
disposeSubscriber(system, sub)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not allow for the same subscriber to subscribe to the same channel twice" in {
|
2014-01-31 11:14:13 +01:00
|
|
|
bus.subscribe(subscriber, classifier) should be(true)
|
|
|
|
|
bus.subscribe(subscriber, classifier) should be(false)
|
|
|
|
|
bus.unsubscribe(subscriber, classifier) should be(true)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not allow for the same subscriber to unsubscribe to the same channel twice" in {
|
2014-01-31 11:14:13 +01:00
|
|
|
bus.subscribe(subscriber, classifier) should be(true)
|
|
|
|
|
bus.unsubscribe(subscriber, classifier) should be(true)
|
|
|
|
|
bus.unsubscribe(subscriber, classifier) should be(false)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"allow to add multiple subscribers" in {
|
|
|
|
|
val subscribers = (1 to 10) map { _ ⇒ createNewSubscriber() }
|
|
|
|
|
val events = createEvents(10)
|
|
|
|
|
val classifiers = events map getClassifierFor
|
2014-01-31 11:14:13 +01:00
|
|
|
subscribers.zip(classifiers) forall { case (s, c) ⇒ bus.subscribe(s, c) } should be(true)
|
|
|
|
|
subscribers.zip(classifiers) forall { case (s, c) ⇒ bus.unsubscribe(s, c) } should be(true)
|
2011-10-11 17:41:25 +02:00
|
|
|
|
2011-12-14 00:06:36 +01:00
|
|
|
subscribers foreach (disposeSubscriber(system, _))
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 18:00:29 +02:00
|
|
|
"publishing events without any subscribers shouldn't be a problem" in {
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"publish the given event to the only subscriber" in {
|
|
|
|
|
bus.subscribe(subscriber, classifier)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
expectMsg(event)
|
2011-10-12 11:46:49 +02:00
|
|
|
expectNoMsg(1 second)
|
2011-10-11 18:00:29 +02:00
|
|
|
bus.unsubscribe(subscriber, classifier)
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 18:19:13 +02:00
|
|
|
"publish to the only subscriber multiple times" in {
|
|
|
|
|
bus.subscribe(subscriber, classifier)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
expectMsg(event)
|
|
|
|
|
expectMsg(event)
|
|
|
|
|
expectMsg(event)
|
2011-10-12 11:46:49 +02:00
|
|
|
expectNoMsg(1 second)
|
2011-10-11 18:19:13 +02:00
|
|
|
bus.unsubscribe(subscriber, classifier)
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 18:12:57 +02:00
|
|
|
"publish the given event to all intended subscribers" in {
|
2011-10-12 11:46:49 +02:00
|
|
|
val range = 0 until 10
|
|
|
|
|
val subscribers = range map (_ ⇒ createNewSubscriber())
|
2014-01-31 11:14:13 +01:00
|
|
|
subscribers foreach { s ⇒ bus.subscribe(s, classifier) should be(true) }
|
2011-10-11 18:12:57 +02:00
|
|
|
bus.publish(event)
|
2011-10-12 11:46:49 +02:00
|
|
|
range foreach { _ ⇒ expectMsg(event) }
|
2014-01-31 11:14:13 +01:00
|
|
|
subscribers foreach { s ⇒ bus.unsubscribe(s, classifier) should be(true); disposeSubscriber(system, s) }
|
2011-10-11 18:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 18:00:29 +02:00
|
|
|
"not publish the given event to any other subscribers than the intended ones" in {
|
|
|
|
|
val otherSubscriber = createNewSubscriber()
|
|
|
|
|
val otherClassifier = getClassifierFor(events.drop(1).head)
|
|
|
|
|
bus.subscribe(subscriber, classifier)
|
|
|
|
|
bus.subscribe(otherSubscriber, otherClassifier)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
expectMsg(event)
|
|
|
|
|
bus.unsubscribe(subscriber, classifier)
|
|
|
|
|
bus.unsubscribe(otherSubscriber, otherClassifier)
|
2011-10-11 18:12:57 +02:00
|
|
|
expectNoMsg(1 second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"not publish the given event to a former subscriber" in {
|
|
|
|
|
bus.subscribe(subscriber, classifier)
|
|
|
|
|
bus.unsubscribe(subscriber, classifier)
|
|
|
|
|
bus.publish(event)
|
|
|
|
|
expectNoMsg(1 second)
|
2011-10-11 18:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-11 17:41:25 +02:00
|
|
|
"cleanup subscriber" in {
|
2011-12-14 00:06:36 +01:00
|
|
|
disposeSubscriber(system, subscriber)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object ActorEventBusSpec {
|
2011-10-12 14:07:49 +02:00
|
|
|
class ComposedActorEventBus extends ActorEventBus with LookupClassification {
|
|
|
|
|
type Event = Int
|
|
|
|
|
type Classifier = String
|
|
|
|
|
|
2011-10-12 11:46:49 +02:00
|
|
|
def classify(event: Event) = event.toString
|
|
|
|
|
protected def mapSize = 32
|
|
|
|
|
def publish(event: Event, subscriber: Subscriber) = subscriber ! event
|
2011-10-11 18:12:57 +02:00
|
|
|
}
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ActorEventBusSpec extends EventBusSpec("ActorEventBus") {
|
2011-10-11 18:12:57 +02:00
|
|
|
import akka.event.ActorEventBusSpec._
|
2011-10-12 11:46:49 +02:00
|
|
|
import EventBusSpec.TestActorWrapperActor
|
2011-10-11 17:41:25 +02:00
|
|
|
|
|
|
|
|
type BusType = ComposedActorEventBus
|
|
|
|
|
def createNewEventBus(): BusType = new ComposedActorEventBus
|
|
|
|
|
|
2011-10-12 11:46:49 +02:00
|
|
|
def createEvents(numberOfEvents: Int) = (0 until numberOfEvents)
|
2011-10-11 17:41:25 +02:00
|
|
|
|
2011-12-07 11:10:54 +01:00
|
|
|
def createSubscriber(pipeTo: ActorRef) = system.actorOf(Props(new TestActorWrapperActor(pipeTo)))
|
2011-10-11 17:41:25 +02:00
|
|
|
|
2011-10-12 11:46:49 +02:00
|
|
|
def classifierFor(event: BusType#Event) = event.toString
|
2011-10-11 17:41:25 +02:00
|
|
|
|
2011-12-14 00:06:36 +01:00
|
|
|
def disposeSubscriber(system: ActorSystem, subscriber: BusType#Subscriber): Unit = system.stop(subscriber)
|
2011-10-11 17:41:25 +02:00
|
|
|
}
|
2011-10-12 11:46:49 +02:00
|
|
|
|
|
|
|
|
object ScanningEventBusSpec {
|
|
|
|
|
import akka.event.japi.ScanningEventBus
|
|
|
|
|
|
|
|
|
|
class MyScanningEventBus extends ScanningEventBus[Int, akka.japi.Procedure[Int], String] {
|
|
|
|
|
protected def compareClassifiers(a: Classifier, b: Classifier): Int = a compareTo b
|
2011-11-11 17:44:57 +01:00
|
|
|
protected def compareSubscribers(a: Subscriber, b: Subscriber): Int = akka.util.Helpers.compareIdentityHash(a, b)
|
2011-10-12 11:46:49 +02:00
|
|
|
|
|
|
|
|
protected def matches(classifier: Classifier, event: Event): Boolean = event.toString == classifier
|
|
|
|
|
|
|
|
|
|
protected def publish(event: Event, subscriber: Subscriber): Unit = subscriber(event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ScanningEventBusSpec extends EventBusSpec("ScanningEventBus") {
|
|
|
|
|
import ScanningEventBusSpec._
|
|
|
|
|
|
|
|
|
|
type BusType = MyScanningEventBus
|
|
|
|
|
|
|
|
|
|
def createNewEventBus(): BusType = new MyScanningEventBus
|
|
|
|
|
|
|
|
|
|
def createEvents(numberOfEvents: Int) = (0 until numberOfEvents)
|
|
|
|
|
|
|
|
|
|
def createSubscriber(pipeTo: ActorRef) = new Procedure[Int] { def apply(i: Int) = pipeTo ! i }
|
|
|
|
|
|
|
|
|
|
def classifierFor(event: BusType#Event) = event.toString
|
|
|
|
|
|
2011-12-14 00:06:36 +01:00
|
|
|
def disposeSubscriber(system: ActorSystem, subscriber: BusType#Subscriber): Unit = ()
|
2011-10-12 11:46:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object LookupEventBusSpec {
|
|
|
|
|
class MyLookupEventBus extends akka.event.japi.LookupEventBus[Int, akka.japi.Procedure[Int], String] {
|
|
|
|
|
protected def classify(event: Event): Classifier = event.toString
|
2011-11-11 17:44:57 +01:00
|
|
|
protected def compareSubscribers(a: Subscriber, b: Subscriber): Int = akka.util.Helpers.compareIdentityHash(a, b)
|
2011-10-12 11:46:49 +02:00
|
|
|
protected def mapSize = 32
|
|
|
|
|
protected def publish(event: Event, subscriber: Subscriber): Unit = subscriber(event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class LookupEventBusSpec extends EventBusSpec("LookupEventBus") {
|
|
|
|
|
import LookupEventBusSpec._
|
|
|
|
|
|
|
|
|
|
type BusType = MyLookupEventBus
|
|
|
|
|
|
|
|
|
|
def createNewEventBus(): BusType = new MyLookupEventBus
|
|
|
|
|
|
|
|
|
|
def createEvents(numberOfEvents: Int) = (0 until numberOfEvents)
|
|
|
|
|
|
|
|
|
|
def createSubscriber(pipeTo: ActorRef) = new Procedure[Int] { def apply(i: Int) = pipeTo ! i }
|
|
|
|
|
|
|
|
|
|
def classifierFor(event: BusType#Event) = event.toString
|
|
|
|
|
|
2011-12-14 00:06:36 +01:00
|
|
|
def disposeSubscriber(system: ActorSystem, subscriber: BusType#Subscriber): Unit = ()
|
2011-10-12 11:46:49 +02:00
|
|
|
}
|