From 9a1095321942c298b7b114f782b8f2da75c0b64a Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 11 Nov 2011 16:50:39 +0100 Subject: [PATCH] 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. --- .../main/scala/akka/actor/ActorSystem.scala | 1 + .../src/main/scala/akka/util/Duration.scala | 14 +++++++ .../src/main/scala/akka/testkit/TestKit.scala | 40 ++++++++++--------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala index 86c47522b0..3685fe388d 100644 --- a/akka-actor/src/main/scala/akka/actor/ActorSystem.scala +++ b/akka-actor/src/main/scala/akka/actor/ActorSystem.scala @@ -95,6 +95,7 @@ class ActorSystem(val name: String, val config: Configuration) extends ActorRefF try java.lang.Double.parseDouble(System.getProperty("akka.test.timefactor")) catch { case _: Exception ⇒ getDouble("akka.test.timefactor", 1.0) } + val SingleExpectDefaultTimeout = Duration(getDouble("akka.test.single-expect-default", 1), DefaultTimeUnit) val TestEventFilterLeeway = Duration(getDouble("akka.test.filter-leeway", 0.5), DefaultTimeUnit) val LogLevel = getString("akka.loglevel", "INFO") diff --git a/akka-actor/src/main/scala/akka/util/Duration.scala b/akka-actor/src/main/scala/akka/util/Duration.scala index 26d863d5a9..fbb27526f1 100644 --- a/akka-actor/src/main/scala/akka/util/Duration.scala +++ b/akka-actor/src/main/scala/akka/util/Duration.scala @@ -121,6 +121,20 @@ object Duration { } val Zero: Duration = new FiniteDuration(0, NANOSECONDS) + val Undefined: Duration = new Duration with Infinite { + override def toString = "Duration.Undefined" + override def equals(other: Any) = other.asInstanceOf[AnyRef] eq this + override def +(other: Duration): Duration = throw new IllegalArgumentException("cannot add Undefined duration") + override def -(other: Duration): Duration = throw new IllegalArgumentException("cannot subtract Undefined duration") + override def *(factor: Double): Duration = throw new IllegalArgumentException("cannot multiply Undefined duration") + override def /(factor: Double): Duration = throw new IllegalArgumentException("cannot divide Undefined duration") + override def /(other: Duration): Double = throw new IllegalArgumentException("cannot divide Undefined duration") + def >(other: Duration) = throw new IllegalArgumentException("cannot compare Undefined duration") + def >=(other: Duration) = throw new IllegalArgumentException("cannot compare Undefined duration") + def <(other: Duration) = throw new IllegalArgumentException("cannot compare Undefined duration") + def <=(other: Duration) = throw new IllegalArgumentException("cannot compare Undefined duration") + def unary_- : Duration = throw new IllegalArgumentException("cannot negate Undefined duration") + } trait Infinite { this: Duration ⇒ diff --git a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala index d939302100..c6facdda52 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala @@ -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) * */ - 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 } }