Add config default for TestKit wait periods outside within()

- introduce Duration.Undefined and use that to initialize TestKit.end
  (also use in places which abused Duration.MinusInf for similar
  purposes)
- TestKit.remaining now returns AkkaConfig.SingleExpectDefaultTimeout if
  end == Duration.Undefined

This will hopefully catch most of the cases where Jenkins previously
aborted the build after 60min.
This commit is contained in:
Roland 2011-11-11 16:50:39 +01:00
parent aa1977db8d
commit 9a10953219
3 changed files with 36 additions and 19 deletions

View file

@ -102,7 +102,7 @@ class TestKit(_app: ActorSystem) {
val testActor: ActorRef = app.systemActorOf(Props(new TestActor(queue)).copy(dispatcher = new CallingThreadDispatcher(app)),
"testActor" + TestKit.testActorId.incrementAndGet)
private var end: Duration = Duration.Inf
private var end: Duration = Duration.Undefined
/**
* if last assertion was expectNoMsg, disable timing failure upon within()
@ -147,9 +147,11 @@ class TestKit(_app: ActorSystem) {
def now: Duration = System.nanoTime.nanos
/**
* Obtain time remaining for execution of the innermost enclosing `within` block.
* Obtain time remaining for execution of the innermost enclosing `within`
* block or missing that it returns the properly dilated default for this
* case from AkkaConfig (key "akka.test.single-expect-default").
*/
def remaining: Duration = end - now
def remaining: Duration = if (end == Duration.Undefined) app.AkkaConfig.SingleExpectDefaultTimeout.dilated else end - now
/**
* Query queue status.
@ -165,8 +167,8 @@ class TestKit(_app: ActorSystem) {
*
* 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
def awaitCond(p: Boolean, max: Duration = Duration.Undefined, interval: Duration = 100.millis) {
val _max = if (max eq Duration.Undefined) remaining else max.dilated
val stop = now + _max
@tailrec
@ -200,7 +202,7 @@ class TestKit(_app: ActorSystem) {
def within[T](min: Duration, max: Duration)(f: T): T = {
val _max = max.dilated
val start = now
val rem = end - start
val rem = if (end == Duration.Undefined) Duration.Inf else end - start
assert(rem >= min, "required min time " + min + " not possible, only " + format(min.unit, rem) + " left")
lastWasNoMsg = false
@ -241,7 +243,7 @@ class TestKit(_app: ActorSystem) {
private def expectMsg_internal[T](max: Duration, obj: T): T = {
val o = receiveOne(max)
assert(o ne null, "timeout during expectMsg while waiting for " + obj)
assert(o ne null, "timeout (" + max + ") during expectMsg while waiting for " + obj)
assert(obj == o, "expected " + obj + ", found " + o)
o.asInstanceOf[T]
}
@ -256,10 +258,10 @@ class TestKit(_app: ActorSystem) {
*
* @return the received object as transformed by the partial function
*/
def expectMsgPF[T](max: Duration = Duration.MinusInf, hint: String = "")(f: PartialFunction[Any, T]): T = {
val _max = if (max eq Duration.MinusInf) remaining else max.dilated
def expectMsgPF[T](max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, T]): T = {
val _max = if (max eq Duration.Undefined) remaining else max.dilated
val o = receiveOne(_max)
assert(o ne null, "timeout during expectMsg: " + hint)
assert(o ne null, "timeout (" + max + ") during expectMsg: " + hint)
assert(f.isDefinedAt(o), "expected: " + hint + " but got unexpected message " + o)
f(o)
}
@ -272,13 +274,13 @@ class TestKit(_app: ActorSystem) {
* @return the last received messsage, i.e. the first one for which the
* partial function returned true
*/
def fishForMessage(max: Duration = Duration.MinusInf, hint: String = "")(f: PartialFunction[Any, Boolean]): Any = {
val _max = if (max eq Duration.MinusInf) remaining else max.dilated
def fishForMessage(max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, Boolean]): Any = {
val _max = if (max eq Duration.Undefined) remaining else max.dilated
val end = now + _max
@tailrec
def recv: Any = {
val o = receiveOne(end - now)
assert(o ne null, "timeout during fishForMessage, hint: " + hint)
assert(o ne null, "timeout (" + max + ") during fishForMessage, hint: " + hint)
assert(f.isDefinedAt(o), "fishForMessage(" + hint + ") found unexpected message " + o)
if (f(o)) o else recv
}
@ -315,7 +317,7 @@ class TestKit(_app: ActorSystem) {
private def expectMsgClass_internal[C](max: Duration, c: Class[C]): C = {
val o = receiveOne(max)
assert(o ne null, "timeout during expectMsgClass waiting for " + c)
assert(o ne null, "timeout (" + max + ") during expectMsgClass waiting for " + c)
assert(c isInstance o, "expected " + c + ", found " + o.getClass)
o.asInstanceOf[C]
}
@ -336,7 +338,7 @@ class TestKit(_app: ActorSystem) {
private def expectMsgAnyOf_internal[T](max: Duration, obj: T*): T = {
val o = receiveOne(max)
assert(o ne null, "timeout during expectMsgAnyOf waiting for " + obj.mkString("(", ", ", ")"))
assert(o ne null, "timeout (" + max + ") during expectMsgAnyOf waiting for " + obj.mkString("(", ", ", ")"))
assert(obj exists (_ == o), "found unexpected " + o)
o.asInstanceOf[T]
}
@ -357,7 +359,7 @@ class TestKit(_app: ActorSystem) {
private def expectMsgAnyClassOf_internal[C](max: Duration, obj: Class[_ <: C]*): C = {
val o = receiveOne(max)
assert(o ne null, "timeout during expectMsgAnyClassOf waiting for " + obj.mkString("(", ", ", ")"))
assert(o ne null, "timeout (" + max + ") during expectMsgAnyClassOf waiting for " + obj.mkString("(", ", ", ")"))
assert(obj exists (_ isInstance o), "found unexpected " + o)
o.asInstanceOf[C]
}
@ -470,8 +472,8 @@ class TestKit(_app: ActorSystem) {
* assert(series == (1 to 7).toList)
* </pre>
*/
def receiveWhile[T](max: Duration = Duration.MinusInf, idle: Duration = Duration.Inf, messages: Int = Int.MaxValue)(f: PartialFunction[AnyRef, T]): Seq[T] = {
val stop = now + (if (max eq Duration.MinusInf) remaining else max.dilated)
def receiveWhile[T](max: Duration = Duration.Undefined, idle: Duration = Duration.Inf, messages: Int = Int.MaxValue)(f: PartialFunction[AnyRef, T]): Seq[T] = {
val stop = now + (if (max eq Duration.Undefined) remaining else max.dilated)
var msg: Message = NullMessage
@tailrec
@ -513,7 +515,7 @@ class TestKit(_app: ActorSystem) {
for { x 1 to n } yield {
val timeout = stop - now
val o = receiveOne(timeout)
assert(o ne null, "timeout while expecting " + n + " messages")
assert(o ne null, "timeout (" + max + ") while expecting " + n + " messages")
o
}
}