diff options
author | Micro Dotta <mirco.dotta@gmail.com> | 2011-08-17 13:32:25 +0000 |
---|---|---|
committer | Micro Dotta <mirco.dotta@gmail.com> | 2011-08-17 13:32:25 +0000 |
commit | 6ba1b9f3c974351e826485ca9c41df732c6de15a (patch) | |
tree | 25c08330ac9c176353cc98f6a3f6cbd0c541d07d /test/files/presentation/akka/src/akka/routing/Routing.scala | |
parent | 044099d4f1677425719ea8cad8c946dab8b5c2d9 (diff) | |
download | scala-6ba1b9f3c974351e826485ca9c41df732c6de15a.tar.gz scala-6ba1b9f3c974351e826485ca9c41df732c6de15a.tar.bz2 scala-6ba1b9f3c974351e826485ca9c41df732c6de15a.zip |
Major rewrite of the testing infrastructure for...
Major rewrite of the testing infrastructure for the presentation
compiler. Added several new tests that will be part of the nightly
build. Once the move to SBT is completed I will look into how to extract
the test infrastructure (as it should really not be living in the
compiler codebase). Review by dragos
Diffstat (limited to 'test/files/presentation/akka/src/akka/routing/Routing.scala')
-rw-r--r-- | test/files/presentation/akka/src/akka/routing/Routing.scala | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/test/files/presentation/akka/src/akka/routing/Routing.scala b/test/files/presentation/akka/src/akka/routing/Routing.scala new file mode 100644 index 0000000000..befc124248 --- /dev/null +++ b/test/files/presentation/akka/src/akka/routing/Routing.scala @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se> + */ + +package akka.routing + +import akka.actor.{ Actor, ActorRef } +import akka.actor.Actor._ + +object Routing { + + sealed trait RoutingMessage + case class Broadcast(message: Any) extends RoutingMessage + + type PF[A, B] = PartialFunction[A, B] + + /** + * Creates a new PartialFunction whose isDefinedAt is a combination + * of the two parameters, and whose apply is first to call filter.apply + * and then filtered.apply. + */ + def filter[A, B](filter: PF[A, Unit], filtered: PF[A, B]): PF[A, B] = { + case a: A if filtered.isDefinedAt(a) && filter.isDefinedAt(a) => + filter(a) + filtered(a) + } + + /** + * Interceptor is a filter(x,y) where x.isDefinedAt is considered to be always true. + */ + def intercept[A, B](interceptor: (A) => Unit, interceptee: PF[A, B]): PF[A, B] = + filter({ case a if a.isInstanceOf[A] => interceptor(a) }, interceptee) + + /** + * Creates a LoadBalancer from the thunk-supplied InfiniteIterator. + */ + def loadBalancerActor(actors: => InfiniteIterator[ActorRef]): ActorRef = + actorOf(new Actor with LoadBalancer { + val seq = actors + }).start() + + /** + * Creates a Dispatcher given a routing and a message-transforming function. + */ + def dispatcherActor(routing: PF[Any, ActorRef], msgTransformer: (Any) => Any): ActorRef = + actorOf(new Actor with Dispatcher { + override def transform(msg: Any) = msgTransformer(msg) + def routes = routing + }).start() + + /** + * Creates a Dispatcher given a routing. + */ + def dispatcherActor(routing: PF[Any, ActorRef]): ActorRef = actorOf(new Actor with Dispatcher { + def routes = routing + }).start() + + /** + * Creates an actor that pipes all incoming messages to + * both another actor and through the supplied function + */ + def loggerActor(actorToLog: ActorRef, logger: (Any) => Unit): ActorRef = + dispatcherActor({ case _ => actorToLog }, logger) +} |