add TestKit.setAutoPilot, see #1807

This commit is contained in:
Roland 2012-02-19 00:09:04 +01:00
parent b5826f9bd9
commit ec15fd6cfd
3 changed files with 67 additions and 3 deletions

View file

@ -16,9 +16,14 @@ import akka.util.Timeout
object TestActor {
type Ignore = Option[PartialFunction[AnyRef, Boolean]]
trait AutoPilot {
def run(sender: ActorRef, msg: Any): Option[AutoPilot]
}
case class SetIgnore(i: Ignore)
case class Watch(ref: ActorRef)
case class UnWatch(ref: ActorRef)
case class SetAutoPilot(ap: AutoPilot)
trait Message {
def msg: AnyRef
@ -36,11 +41,15 @@ class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor {
var ignore: Ignore = None
var autopilot: Option[AutoPilot] = None
def receive = {
case SetIgnore(ign) ignore = ign
case x @ Watch(ref) context.watch(ref); queue.offerLast(RealMessage(x, self))
case x @ UnWatch(ref) context.unwatch(ref); queue.offerLast(RealMessage(x, self))
case SetIgnore(ign) ignore = ign
case x @ Watch(ref) context.watch(ref); queue.offerLast(RealMessage(x, self))
case x @ UnWatch(ref) context.unwatch(ref); queue.offerLast(RealMessage(x, self))
case SetAutoPilot(pilot) autopilot = Some(pilot)
case x: AnyRef
autopilot = autopilot.flatMap(_.run(sender, x))
val observe = ignore map (ignoreFunc if (ignoreFunc isDefinedAt x) !ignoreFunc(x) else true) getOrElse true
if (observe) queue.offerLast(RealMessage(x, sender))
}
@ -148,6 +157,13 @@ class TestKit(_system: ActorSystem) {
expectMsg(msg)
}
/**
* Install an AutoPilot to drive the testActor: the AutoPilot will be run
* for each received message and can be used to send or forward messages,
* etc. Each invocation must return the AutoPilot for the next round.
*/
def setAutoPilot(pilot: TestActor.AutoPilot): Unit = testActor ! TestActor.SetAutoPilot(pilot)
/**
* Obtain current time (`System.nanoTime`) as Duration.
*/