2011-05-19 21:34:21 +02:00
|
|
|
/**
|
2011-07-14 16:03:08 +02:00
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
2011-05-19 21:34:21 +02:00
|
|
|
*/
|
2011-03-05 14:36:08 +01:00
|
|
|
package akka.testkit
|
2010-12-26 22:49:40 +01:00
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
import akka.actor._
|
2010-12-26 22:49:40 +01:00
|
|
|
import Actor._
|
2011-03-05 14:36:08 +01:00
|
|
|
import akka.util.Duration
|
|
|
|
|
import akka.util.duration._
|
2011-06-05 10:45:27 +02:00
|
|
|
import java.util.concurrent.{ BlockingDeque, LinkedBlockingDeque, TimeUnit, atomic }
|
|
|
|
|
import atomic.AtomicInteger
|
2010-12-30 22:43:24 +01:00
|
|
|
import scala.annotation.tailrec
|
2011-10-06 21:19:46 +02:00
|
|
|
import akka.AkkaApplication
|
2010-12-26 22:49:40 +01:00
|
|
|
|
2010-12-28 23:51:41 +01:00
|
|
|
object TestActor {
|
2011-01-04 13:50:50 +01:00
|
|
|
type Ignore = Option[PartialFunction[AnyRef, Boolean]]
|
2010-12-29 21:47:05 +01:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
case class SetTimeout(d: Duration)
|
|
|
|
|
case class SetIgnore(i: Ignore)
|
2011-06-13 22:36:46 +02:00
|
|
|
|
|
|
|
|
trait Message {
|
|
|
|
|
def msg: AnyRef
|
|
|
|
|
def channel: UntypedChannel
|
|
|
|
|
}
|
|
|
|
|
case class RealMessage(msg: AnyRef, channel: UntypedChannel) extends Message
|
|
|
|
|
case object NullMessage extends Message {
|
|
|
|
|
override def msg: AnyRef = throw new IllegalActorStateException("last receive did not dequeue a message")
|
|
|
|
|
override def channel: UntypedChannel = throw new IllegalActorStateException("last receive did not dequeue a message")
|
|
|
|
|
}
|
2010-12-28 23:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
class TestActor(queue: BlockingDeque[TestActor.Message]) extends Actor with FSM[Int, TestActor.Ignore] {
|
2011-01-04 13:50:50 +01:00
|
|
|
import FSM._
|
|
|
|
|
import TestActor._
|
|
|
|
|
|
|
|
|
|
startWith(0, None)
|
|
|
|
|
when(0, stateTimeout = 5 seconds) {
|
2011-05-18 17:25:30 +02:00
|
|
|
case Ev(SetTimeout(d)) ⇒
|
2011-01-04 13:50:50 +01:00
|
|
|
setStateTimeout(0, if (d.finite_?) d else None)
|
|
|
|
|
stay
|
2011-05-18 17:25:30 +02:00
|
|
|
case Ev(SetIgnore(ign)) ⇒ stay using ign
|
|
|
|
|
case Ev(StateTimeout) ⇒
|
2011-01-04 13:50:50 +01:00
|
|
|
stop
|
2011-09-27 18:02:57 +02:00
|
|
|
case Event(x: AnyRef, data) ⇒
|
|
|
|
|
val observe = data map (ignoreFunc ⇒ if (ignoreFunc isDefinedAt x) !ignoreFunc(x) else true) getOrElse true
|
|
|
|
|
if (observe)
|
2011-09-19 11:22:27 +02:00
|
|
|
queue.offerLast(RealMessage(x, channel))
|
2011-09-27 18:02:57 +02:00
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
stay
|
|
|
|
|
}
|
|
|
|
|
initialize
|
2010-12-26 22:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2010-12-28 23:51:41 +01:00
|
|
|
/**
|
|
|
|
|
* Test kit for testing actors. Inheriting from this trait enables reception of
|
|
|
|
|
* replies from actors, which are queued by an internal actor and can be
|
2010-12-29 21:47:05 +01:00
|
|
|
* examined using the `expectMsg...` methods. Assertions and bounds concerning
|
2010-12-28 23:51:41 +01:00
|
|
|
* timing are available in the form of `within` blocks.
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* class Test extends TestKit {
|
2011-09-08 11:02:17 +02:00
|
|
|
* val test = actorOf[SomeActor]
|
2010-12-28 23:51:41 +01:00
|
|
|
*
|
|
|
|
|
* within (1 second) {
|
2011-01-04 13:50:50 +01:00
|
|
|
* test ! SomeWork
|
|
|
|
|
* expectMsg(Result1) // bounded to 1 second
|
|
|
|
|
* expectMsg(Result2) // bounded to the remainder of the 1 second
|
2010-12-28 23:51:41 +01:00
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* Beware of two points:
|
|
|
|
|
*
|
|
|
|
|
* - the internal test actor needs to be stopped, either explicitly using
|
|
|
|
|
* `stopTestActor` or implicitly by using its internal inactivity timeout,
|
|
|
|
|
* see `setTestActorTimeout`
|
|
|
|
|
* - this trait is not thread-safe (only one actor with one queue, one stack
|
|
|
|
|
* of `within` blocks); it is expected that the code is executed from a
|
|
|
|
|
* constructor as shown above, which makes this a non-issue, otherwise take
|
2010-12-29 21:47:05 +01:00
|
|
|
* care not to run tests within a single test class instance in parallel.
|
|
|
|
|
*
|
2011-06-17 22:19:17 +02:00
|
|
|
* It should be noted that for CI servers and the like all maximum Durations
|
|
|
|
|
* are scaled using their Duration.dilated method, which uses the
|
|
|
|
|
* Duration.timeFactor settable via akka.conf entry "akka.test.timefactor".
|
|
|
|
|
*
|
2010-12-29 21:47:05 +01:00
|
|
|
* @author Roland Kuhn
|
|
|
|
|
* @since 1.1
|
2010-12-28 23:51:41 +01:00
|
|
|
*/
|
2011-10-10 11:12:34 +02:00
|
|
|
class TestKit(_app: AkkaApplication) {
|
2010-12-26 22:49:40 +01:00
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
import TestActor.{ Message, RealMessage, NullMessage }
|
2011-10-07 15:22:36 +02:00
|
|
|
|
2011-10-11 16:05:48 +02:00
|
|
|
implicit val app = _app
|
2011-06-13 22:36:46 +02:00
|
|
|
|
|
|
|
|
private val queue = new LinkedBlockingDeque[Message]()
|
|
|
|
|
private[akka] var lastMessage: Message = NullMessage
|
2011-05-18 17:25:30 +02:00
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
/**
|
|
|
|
|
* ActorRef of the test actor. Access is provided to enable e.g.
|
|
|
|
|
* registration as message target.
|
|
|
|
|
*/
|
2011-10-13 13:16:41 +02:00
|
|
|
val testActor: ActorRef = new LocalActorRef(app, Props(new TestActor(queue)).copy(dispatcher = new CallingThreadDispatcher(app)), "testActor" + TestKit.testActorId.incrementAndGet(), true)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
private var end: Duration = Duration.Inf
|
2011-06-26 17:40:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* if last assertion was expectNoMsg, disable timing failure upon within()
|
|
|
|
|
* block end.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-06-26 17:40:30 +02:00
|
|
|
private var lastWasNoMsg = false
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop test actor. Should be done at the end of the test unless relying on
|
|
|
|
|
* test actor timeout.
|
|
|
|
|
*/
|
2011-09-28 12:57:33 +02:00
|
|
|
def stopTestActor() { testActor.stop() }
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set test actor timeout. By default, the test actor shuts itself down
|
|
|
|
|
* after 5 seconds of inactivity. Set this to Duration.Inf to disable this
|
|
|
|
|
* behavior, but make sure that someone will then call `stopTestActor`,
|
|
|
|
|
* unless you want to leak actors, e.g. wrap test in
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* try {
|
|
|
|
|
* ...
|
|
|
|
|
* } finally { stopTestActor }
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def setTestActorTimeout(d: Duration) { testActor ! TestActor.SetTimeout(d) }
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ignore all messages in the test actor for which the given partial
|
|
|
|
|
* function returns true.
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def ignoreMsg(f: PartialFunction[AnyRef, Boolean]) { testActor ! TestActor.SetIgnore(Some(f)) }
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop ignoring messages in the test actor.
|
|
|
|
|
*/
|
2011-09-28 12:57:33 +02:00
|
|
|
def ignoreNoMsg() { testActor ! TestActor.SetIgnore(None) }
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
2011-05-29 20:18:07 +02:00
|
|
|
* Obtain current time (`System.nanoTime`) as Duration.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def now: Duration = System.nanoTime.nanos
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Obtain time remaining for execution of the innermost enclosing `within` block.
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def remaining: Duration = end - now
|
2011-01-04 13:50:50 +01:00
|
|
|
|
2011-06-02 22:54:38 +02:00
|
|
|
/**
|
|
|
|
|
* Query queue status.
|
|
|
|
|
*/
|
|
|
|
|
def msgAvailable = !queue.isEmpty
|
|
|
|
|
|
2011-06-17 22:19:17 +02:00
|
|
|
/**
|
|
|
|
|
* Block until the given condition evaluates to `true` or the timeout
|
|
|
|
|
* expires, whichever comes first.
|
|
|
|
|
*
|
|
|
|
|
* If no timeout is given, take it from the innermost enclosing `within`
|
|
|
|
|
* block.
|
|
|
|
|
*
|
|
|
|
|
* Note that the timeout is scaled using Duration.timeFactor.
|
|
|
|
|
*/
|
|
|
|
|
def awaitCond(p: ⇒ Boolean, max: Duration = Duration.MinusInf, interval: Duration = 100.millis) {
|
|
|
|
|
val _max = if (max eq Duration.MinusInf) remaining else max.dilated
|
|
|
|
|
val stop = now + _max
|
|
|
|
|
|
|
|
|
|
@tailrec
|
|
|
|
|
def poll(t: Duration) {
|
|
|
|
|
if (!p) {
|
|
|
|
|
assert(now < stop, "timeout " + _max + " expired")
|
|
|
|
|
Thread.sleep(t.toMillis)
|
|
|
|
|
poll((stop - now) min interval)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poll(_max min interval)
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
/**
|
|
|
|
|
* Execute code block while bounding its execution time between `min` and
|
|
|
|
|
* `max`. `within` blocks may be nested. All methods in this trait which
|
|
|
|
|
* take maximum wait times are available in a version which implicitly uses
|
|
|
|
|
* the remaining time governed by the innermost enclosing `within` block.
|
|
|
|
|
*
|
2011-06-17 22:19:17 +02:00
|
|
|
* Note that the max Duration is scaled by Duration.timeFactor while the min
|
|
|
|
|
* Duration is not.
|
|
|
|
|
*
|
2011-01-04 13:50:50 +01:00
|
|
|
* <pre>
|
|
|
|
|
* val ret = within(50 millis) {
|
|
|
|
|
* test ! "ping"
|
|
|
|
|
* expectMsgClass(classOf[String])
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def within[T](min: Duration, max: Duration)(f: ⇒ T): T = {
|
2011-06-17 22:19:17 +02:00
|
|
|
val _max = max.dilated
|
2011-01-04 13:50:50 +01:00
|
|
|
val start = now
|
|
|
|
|
val rem = end - start
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(rem >= min, "required min time " + min + " not possible, only " + format(min.unit, rem) + " left")
|
2011-01-04 13:50:50 +01:00
|
|
|
|
2011-06-26 17:40:30 +02:00
|
|
|
lastWasNoMsg = false
|
|
|
|
|
|
2011-06-17 22:19:17 +02:00
|
|
|
val max_diff = _max min rem
|
2011-01-04 13:50:50 +01:00
|
|
|
val prev_end = end
|
|
|
|
|
end = start + max_diff
|
|
|
|
|
|
2011-03-27 18:15:40 +02:00
|
|
|
val ret = try f finally end = prev_end
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
val diff = now - start
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(min <= diff, "block took " + format(min.unit, diff) + ", should at least have been " + min)
|
2011-06-26 17:40:30 +02:00
|
|
|
if (!lastWasNoMsg) {
|
2011-06-17 22:19:17 +02:00
|
|
|
assert(diff <= max_diff, "block took " + format(_max.unit, diff) + ", exceeding " + format(_max.unit, max_diff))
|
2010-12-26 22:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as calling `within(0 seconds, max)(f)`.
|
|
|
|
|
*/
|
2011-05-18 17:25:30 +02:00
|
|
|
def within[T](max: Duration)(f: ⇒ T): T = within(0 seconds, max)(f)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
/**
|
|
|
|
|
* Send reply to the last dequeued message. Will throw
|
|
|
|
|
* IllegalActorStateException if no message has been dequeued, yet. Dequeuing
|
|
|
|
|
* means reception of the message as part of an expect... or receive... call,
|
|
|
|
|
* not reception by the testActor.
|
|
|
|
|
*/
|
2011-10-12 15:36:10 +02:00
|
|
|
def reply(msg: AnyRef) { lastMessage.channel.!(msg)(testActor) }
|
2011-06-13 22:36:46 +02:00
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsg(remaining, obj)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsg[T](obj: T): T = expectMsg_internal(remaining, obj)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that it equals the
|
|
|
|
|
* given object. Wait time is bounded by the given duration, with an
|
|
|
|
|
* AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsg[T](max: Duration, obj: T): T = expectMsg_internal(max.dilated, obj)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsg_internal[T](max: Duration, obj: T): T = {
|
2011-01-04 13:50:50 +01:00
|
|
|
val o = receiveOne(max)
|
2011-09-16 10:59:12 +02:00
|
|
|
assert(o ne null, "timeout during expectMsg while waiting for " + obj)
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(obj == o, "expected " + obj + ", found " + o)
|
2011-05-29 20:18:07 +02:00
|
|
|
o.asInstanceOf[T]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that the given
|
|
|
|
|
* partial function accepts it. Wait time is bounded by the given duration,
|
|
|
|
|
* with an AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* Use this variant to implement more complicated or conditional
|
|
|
|
|
* processing.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object as transformed by the partial function
|
|
|
|
|
*/
|
2011-09-27 18:02:57 +02:00
|
|
|
def expectMsgPF[T](max: Duration = Duration.MinusInf, hint: String = "")(f: PartialFunction[Any, T]): T = {
|
2011-05-25 00:17:15 +02:00
|
|
|
val _max = if (max eq Duration.MinusInf) remaining else max.dilated
|
|
|
|
|
val o = receiveOne(_max)
|
2011-09-27 18:02:57 +02:00
|
|
|
assert(o ne null, "timeout during expectMsg: " + hint)
|
2011-10-18 12:40:44 +02:00
|
|
|
assert(f.isDefinedAt(o), "expected: " + hint + " but got unexpected message " + o)
|
2011-01-04 13:50:50 +01:00
|
|
|
f(o)
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-26 17:40:30 +02:00
|
|
|
/**
|
|
|
|
|
* Same as `expectMsgType[T](remaining)`, but correctly treating the timeFactor.
|
|
|
|
|
*/
|
|
|
|
|
def expectMsgType[T](implicit m: Manifest[T]): T = expectMsgClass_internal(remaining, m.erasure.asInstanceOf[Class[T]])
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that it conforms to the
|
|
|
|
|
* given type (after erasure). Wait time is bounded by the given duration,
|
|
|
|
|
* with an AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object
|
|
|
|
|
*/
|
|
|
|
|
def expectMsgType[T](max: Duration)(implicit m: Manifest[T]): T = expectMsgClass_internal(max.dilated, m.erasure.asInstanceOf[Class[T]])
|
|
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgClass(remaining, c)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-06-17 22:19:17 +02:00
|
|
|
def expectMsgClass[C](c: Class[C]): C = expectMsgClass_internal(remaining, c)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that it conforms to
|
|
|
|
|
* the given class. Wait time is bounded by the given duration, with an
|
|
|
|
|
* AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object
|
|
|
|
|
*/
|
2011-06-17 22:19:17 +02:00
|
|
|
def expectMsgClass[C](max: Duration, c: Class[C]): C = expectMsgClass_internal(max.dilated, c)
|
|
|
|
|
|
|
|
|
|
private def expectMsgClass_internal[C](max: Duration, c: Class[C]): C = {
|
2011-01-04 13:50:50 +01:00
|
|
|
val o = receiveOne(max)
|
2011-09-16 10:59:12 +02:00
|
|
|
assert(o ne null, "timeout during expectMsgClass waiting for " + c)
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(c isInstance o, "expected " + c + ", found " + o.getClass)
|
2011-01-04 13:50:50 +01:00
|
|
|
o.asInstanceOf[C]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgAnyOf(remaining, obj...)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAnyOf[T](obj: T*): T = expectMsgAnyOf_internal(remaining, obj: _*)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that it equals one of
|
|
|
|
|
* the given objects. Wait time is bounded by the given duration, with an
|
|
|
|
|
* AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAnyOf[T](max: Duration, obj: T*): T = expectMsgAnyOf_internal(max.dilated, obj: _*)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsgAnyOf_internal[T](max: Duration, obj: T*): T = {
|
2011-01-04 13:50:50 +01:00
|
|
|
val o = receiveOne(max)
|
2011-09-16 10:59:12 +02:00
|
|
|
assert(o ne null, "timeout during expectMsgAnyOf waiting for " + obj.mkString("(", ", ", ")"))
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(obj exists (_ == o), "found unexpected " + o)
|
2011-05-29 20:18:07 +02:00
|
|
|
o.asInstanceOf[T]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgAnyClassOf(remaining, obj...)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAnyClassOf[C](obj: Class[_ <: C]*): C = expectMsgAnyClassOf_internal(remaining, obj: _*)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive one message from the test actor and assert that it conforms to
|
|
|
|
|
* one of the given classes. Wait time is bounded by the given duration,
|
|
|
|
|
* with an AssertionFailure being thrown in case of timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return the received object
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAnyClassOf[C](max: Duration, obj: Class[_ <: C]*): C = expectMsgAnyClassOf_internal(max.dilated, obj: _*)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsgAnyClassOf_internal[C](max: Duration, obj: Class[_ <: C]*): C = {
|
2011-01-04 13:50:50 +01:00
|
|
|
val o = receiveOne(max)
|
2011-09-16 10:59:12 +02:00
|
|
|
assert(o ne null, "timeout during expectMsgAnyClassOf waiting for " + obj.mkString("(", ", ", ")"))
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(obj exists (_ isInstance o), "found unexpected " + o)
|
2011-05-29 20:18:07 +02:00
|
|
|
o.asInstanceOf[C]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgAllOf(remaining, obj...)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllOf[T](obj: T*): Seq[T] = expectMsgAllOf_internal(remaining, obj: _*)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive a number of messages from the test actor matching the given
|
|
|
|
|
* number of objects and assert that for each given object one is received
|
2011-05-29 20:18:07 +02:00
|
|
|
* which equals it and vice versa. This construct is useful when the order in
|
|
|
|
|
* which the objects are received is not fixed. Wait time is bounded by the
|
|
|
|
|
* given duration, with an AssertionFailure being thrown in case of timeout.
|
2011-01-04 13:50:50 +01:00
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* dispatcher ! SomeWork1()
|
|
|
|
|
* dispatcher ! SomeWork2()
|
2011-07-16 21:30:08 -04:00
|
|
|
* expectMsgAllOf(1 second, Result1(), Result2())
|
2011-01-04 13:50:50 +01:00
|
|
|
* </pre>
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllOf[T](max: Duration, obj: T*): Seq[T] = expectMsgAllOf_internal(max.dilated, obj: _*)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsgAllOf_internal[T](max: Duration, obj: T*): Seq[T] = {
|
|
|
|
|
val recv = receiveN_internal(obj.size, max)
|
|
|
|
|
obj foreach (x ⇒ assert(recv exists (x == _), "not found " + x))
|
|
|
|
|
recv foreach (x ⇒ assert(obj exists (x == _), "found unexpected " + x))
|
|
|
|
|
recv.asInstanceOf[Seq[T]]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgAllClassOf(remaining, obj...)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllClassOf[T](obj: Class[_ <: T]*): Seq[T] = expectMsgAllClassOf_internal(remaining, obj: _*)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive a number of messages from the test actor matching the given
|
|
|
|
|
* number of classes and assert that for each given class one is received
|
|
|
|
|
* which is of that class (equality, not conformance). This construct is
|
|
|
|
|
* useful when the order in which the objects are received is not fixed.
|
|
|
|
|
* Wait time is bounded by the given duration, with an AssertionFailure
|
|
|
|
|
* being thrown in case of timeout.
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllClassOf[T](max: Duration, obj: Class[_ <: T]*): Seq[T] = expectMsgAllClassOf_internal(max.dilated, obj: _*)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsgAllClassOf_internal[T](max: Duration, obj: Class[_ <: T]*): Seq[T] = {
|
|
|
|
|
val recv = receiveN_internal(obj.size, max)
|
|
|
|
|
obj foreach (x ⇒ assert(recv exists (_.getClass eq x), "not found " + x))
|
|
|
|
|
recv foreach (x ⇒ assert(obj exists (_ eq x.getClass), "found non-matching object " + x))
|
|
|
|
|
recv.asInstanceOf[Seq[T]]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectMsgAllConformingOf(remaining, obj...)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllConformingOf[T](obj: Class[_ <: T]*): Seq[T] = expectMsgAllClassOf_internal(remaining, obj: _*)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Receive a number of messages from the test actor matching the given
|
|
|
|
|
* number of classes and assert that for each given class one is received
|
2011-05-29 20:18:07 +02:00
|
|
|
* which conforms to that class (and vice versa). This construct is useful
|
|
|
|
|
* when the order in which the objects are received is not fixed. Wait time
|
|
|
|
|
* is bounded by the given duration, with an AssertionFailure being thrown in
|
|
|
|
|
* case of timeout.
|
2011-01-04 13:50:50 +01:00
|
|
|
*
|
|
|
|
|
* Beware that one object may satisfy all given class constraints, which
|
|
|
|
|
* may be counter-intuitive.
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def expectMsgAllConformingOf[T](max: Duration, obj: Class[_ <: T]*): Seq[T] = expectMsgAllConformingOf(max.dilated, obj: _*)
|
2011-06-17 22:19:17 +02:00
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
private def expectMsgAllConformingOf_internal[T](max: Duration, obj: Class[_ <: T]*): Seq[T] = {
|
|
|
|
|
val recv = receiveN_internal(obj.size, max)
|
|
|
|
|
obj foreach (x ⇒ assert(recv exists (x isInstance _), "not found " + x))
|
|
|
|
|
recv foreach (x ⇒ assert(obj exists (_ isInstance x), "found non-matching object " + x))
|
|
|
|
|
recv.asInstanceOf[Seq[T]]
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `expectNoMsg(remaining)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-09-28 12:57:33 +02:00
|
|
|
def expectNoMsg() { expectNoMsg_internal(remaining) }
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert that no message is received for the specified time.
|
|
|
|
|
*/
|
2011-06-17 22:19:17 +02:00
|
|
|
def expectNoMsg(max: Duration) { expectNoMsg_internal(max.dilated) }
|
|
|
|
|
|
|
|
|
|
private def expectNoMsg_internal(max: Duration) {
|
2011-01-04 13:50:50 +01:00
|
|
|
val o = receiveOne(max)
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(o eq null, "received unexpected message " + o)
|
2011-06-26 17:40:30 +02:00
|
|
|
lastWasNoMsg = true
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-17 22:19:17 +02:00
|
|
|
* Same as `receiveWhile(remaining)(f)`, but correctly treating the timeFactor.
|
2011-01-04 13:50:50 +01:00
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
@deprecated("insert empty first parameter list", "1.2")
|
|
|
|
|
def receiveWhile[T](f: PartialFunction[AnyRef, T]): Seq[T] = receiveWhile(remaining / Duration.timeFactor)(f)
|
2011-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
|
/**
|
2011-05-29 20:18:07 +02:00
|
|
|
* Receive a series of messages until one does not match the given partial
|
|
|
|
|
* function or the idle timeout is met (disabled by default) or the overall
|
|
|
|
|
* maximum duration is elapsed. Returns the sequence of messages.
|
2011-01-04 13:50:50 +01:00
|
|
|
*
|
2011-06-17 22:19:17 +02:00
|
|
|
* Note that it is not an error to hit the `max` duration in this case.
|
2011-01-04 13:50:50 +01:00
|
|
|
*
|
|
|
|
|
* One possible use of this method is for testing whether messages of
|
|
|
|
|
* certain characteristics are generated at a certain rate:
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* test ! ScheduleTicks(100 millis)
|
|
|
|
|
* val series = receiveWhile(750 millis) {
|
|
|
|
|
* case Tick(count) => count
|
|
|
|
|
* }
|
|
|
|
|
* assert(series == (1 to 7).toList)
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def receiveWhile[T](max: Duration = Duration.MinusInf, idle: Duration = Duration.Inf)(f: PartialFunction[AnyRef, T]): Seq[T] = {
|
|
|
|
|
val stop = now + (if (max == Duration.MinusInf) remaining else max.dilated)
|
2011-06-13 22:36:46 +02:00
|
|
|
var msg: Message = NullMessage
|
2011-01-04 13:50:50 +01:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
@tailrec
|
|
|
|
|
def doit(acc: List[T]): List[T] = {
|
2011-05-29 20:18:07 +02:00
|
|
|
receiveOne((stop - now) min idle)
|
2011-06-13 22:36:46 +02:00
|
|
|
lastMessage match {
|
|
|
|
|
case NullMessage ⇒
|
|
|
|
|
lastMessage = msg
|
2011-01-04 13:50:50 +01:00
|
|
|
acc.reverse
|
2011-06-13 22:36:46 +02:00
|
|
|
case RealMessage(o, _) if (f isDefinedAt o) ⇒
|
|
|
|
|
msg = lastMessage
|
2011-01-04 13:50:50 +01:00
|
|
|
doit(f(o) :: acc)
|
2011-06-13 22:36:46 +02:00
|
|
|
case RealMessage(o, _) ⇒
|
|
|
|
|
queue.offerFirst(lastMessage)
|
|
|
|
|
lastMessage = msg
|
2011-01-04 13:50:50 +01:00
|
|
|
acc.reverse
|
|
|
|
|
}
|
2010-12-26 22:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2011-01-04 13:50:50 +01:00
|
|
|
val ret = doit(Nil)
|
2011-06-26 17:40:30 +02:00
|
|
|
lastWasNoMsg = true
|
2011-01-04 13:50:50 +01:00
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 20:18:07 +02:00
|
|
|
/**
|
|
|
|
|
* Same as `receiveN(n, remaining)` but correctly taking into account
|
|
|
|
|
* Duration.timeFactor.
|
|
|
|
|
*/
|
|
|
|
|
def receiveN(n: Int): Seq[AnyRef] = receiveN_internal(n, remaining)
|
|
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
/**
|
|
|
|
|
* Receive N messages in a row before the given deadline.
|
|
|
|
|
*/
|
2011-05-29 20:18:07 +02:00
|
|
|
def receiveN(n: Int, max: Duration): Seq[AnyRef] = receiveN_internal(n, max.dilated)
|
|
|
|
|
|
|
|
|
|
private def receiveN_internal(n: Int, max: Duration): Seq[AnyRef] = {
|
|
|
|
|
val stop = max + now
|
2011-05-18 17:25:30 +02:00
|
|
|
for { x ← 1 to n } yield {
|
2011-01-04 13:50:50 +01:00
|
|
|
val timeout = stop - now
|
|
|
|
|
val o = receiveOne(timeout)
|
2011-05-18 17:25:30 +02:00
|
|
|
assert(o ne null, "timeout while expecting " + n + " messages")
|
2011-01-04 13:50:50 +01:00
|
|
|
o
|
2010-12-28 23:51:41 +01:00
|
|
|
}
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
/**
|
|
|
|
|
* Receive one message from the internal queue of the TestActor. If the given
|
|
|
|
|
* duration is zero, the queue is polled (non-blocking).
|
2011-06-17 22:19:17 +02:00
|
|
|
*
|
|
|
|
|
* This method does NOT automatically scale its Duration parameter!
|
2011-06-13 22:36:46 +02:00
|
|
|
*/
|
|
|
|
|
def receiveOne(max: Duration): AnyRef = {
|
|
|
|
|
val message =
|
|
|
|
|
if (max == 0.seconds) {
|
|
|
|
|
queue.pollFirst
|
|
|
|
|
} else if (max.finite_?) {
|
|
|
|
|
queue.pollFirst(max.length, max.unit)
|
|
|
|
|
} else {
|
|
|
|
|
queue.takeFirst
|
|
|
|
|
}
|
2011-06-26 17:40:30 +02:00
|
|
|
lastWasNoMsg = false
|
2011-06-13 22:36:46 +02:00
|
|
|
message match {
|
|
|
|
|
case null ⇒
|
|
|
|
|
lastMessage = NullMessage
|
|
|
|
|
null
|
|
|
|
|
case RealMessage(msg, _) ⇒
|
|
|
|
|
lastMessage = message
|
|
|
|
|
msg
|
2010-12-28 23:51:41 +01:00
|
|
|
}
|
2011-01-04 13:50:50 +01:00
|
|
|
}
|
2010-12-31 17:57:08 +01:00
|
|
|
|
2011-05-18 17:25:30 +02:00
|
|
|
private def format(u: TimeUnit, d: Duration) = "%.3f %s".format(d.toUnit(u), u.toString.toLowerCase)
|
2010-12-26 22:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2011-06-05 10:45:27 +02:00
|
|
|
object TestKit {
|
|
|
|
|
private[testkit] val testActorId = new AtomicInteger(0)
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-13 22:36:46 +02:00
|
|
|
/**
|
|
|
|
|
* TestKit-based probe which allows sending, reception and reply.
|
|
|
|
|
*/
|
2011-10-06 21:19:46 +02:00
|
|
|
class TestProbe(_application: AkkaApplication) extends TestKit(_application) {
|
2011-06-13 22:36:46 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shorthand to get the testActor.
|
|
|
|
|
*/
|
|
|
|
|
def ref = testActor
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send message to an actor while using the probe's TestActor as the sender.
|
|
|
|
|
* Replies will be available for inspection with all of TestKit's assertion
|
|
|
|
|
* methods.
|
|
|
|
|
*/
|
|
|
|
|
def send(actor: ActorRef, msg: AnyRef) = {
|
2011-10-12 15:36:10 +02:00
|
|
|
actor.!(msg)(testActor)
|
2011-06-13 22:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Forward this message as if in the TestActor's receive method with self.forward.
|
|
|
|
|
*/
|
|
|
|
|
def forward(actor: ActorRef, msg: AnyRef = lastMessage.msg) {
|
|
|
|
|
actor.!(msg)(lastMessage.channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get channel of last received message.
|
|
|
|
|
*/
|
|
|
|
|
def channel = lastMessage.channel
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object TestProbe {
|
2011-10-13 14:23:44 +02:00
|
|
|
def apply()(implicit app: AkkaApplication) = new TestProbe(app)
|
2011-06-13 22:36:46 +02:00
|
|
|
}
|
2011-10-11 16:05:48 +02:00
|
|
|
|
|
|
|
|
trait ImplicitSender { this: TestKit ⇒
|
|
|
|
|
implicit def implicitSenderTestActor = testActor
|
|
|
|
|
}
|