+tes #15132 Add additional overloaded expectMsg to TestKit

This commit is contained in:
Marcin Kubala 2014-06-05 23:30:11 +02:00 committed by Marcin Kubala
parent e48b6b4185
commit cdf2bc24d4
2 changed files with 48 additions and 4 deletions

View file

@ -332,10 +332,20 @@ trait TestKitBase {
*/ */
def expectMsg[T](max: FiniteDuration, obj: T): T = expectMsg_internal(max.dilated, obj) def expectMsg[T](max: FiniteDuration, obj: T): T = expectMsg_internal(max.dilated, obj)
private def expectMsg_internal[T](max: Duration, obj: T): T = { /**
* 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
*/
def expectMsg[T](max: FiniteDuration, hint: String, obj: T): T = expectMsg_internal(max.dilated, obj, Some(hint))
private def expectMsg_internal[T](max: Duration, obj: T, hint: Option[String] = None): T = {
val o = receiveOne(max) val o = receiveOne(max)
assert(o ne null, s"timeout ($max) during expectMsg while waiting for $obj") lazy val hintOrEmptyString = hint.map(": " + _).getOrElse("")
assert(obj == o, s"expected $obj, found $o") assert(o ne null, s"timeout ($max) during expectMsg while waiting for $obj" + hintOrEmptyString)
assert(obj == o, s"expected $obj, found $o" + hintOrEmptyString)
o.asInstanceOf[T] o.asInstanceOf[T]
} }

View file

@ -9,6 +9,7 @@ import akka.actor._
import scala.concurrent.{ Future, Await } import scala.concurrent.{ Future, Await }
import scala.concurrent.duration._ import scala.concurrent.duration._
import akka.pattern.ask import akka.pattern.ask
import scala.util.Try
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner]) @org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class TestProbeSpec extends AkkaSpec with DefaultTimeout { class TestProbeSpec extends AkkaSpec with DefaultTimeout {
@ -39,7 +40,40 @@ class TestProbeSpec extends AkkaSpec with DefaultTimeout {
probe1.send(probe2.ref, "hello") probe1.send(probe2.ref, "hello")
probe2.expectMsg(0 millis, "hello") probe2.expectMsg(0 millis, "hello")
probe2.lastMessage.sender ! "world" probe2.lastMessage.sender ! "world"
probe1.expectMsg(0 millis, "world") probe1.expectMsg(0 millis, "some hint here", "world")
}
def assertFailureMessageContains(expectedHint: String)(block: Unit) {
Try {
block
} match {
case scala.util.Failure(e: AssertionError)
if (!(e.getMessage contains expectedHint))
fail(s"failure message did not contain hint! Was: ${e.getMessage}, expected to contain $expectedHint")
case scala.util.Failure(oth)
fail(s"expected AssertionError but got: $oth")
case scala.util.Success(result)
fail(s"expected failure but got: $result")
}
}
"throw AssertionError containing hint in its message if max await time is exceeded" in {
val probe = TestProbe()
val hint = "some hint"
assertFailureMessageContains(hint) {
probe.expectMsg(0 millis, hint, "hello")
}
}
"throw AssertionError containing hint in its message if received message doesn't match" in {
val probe = TestProbe()
val hint = "some hint"
assertFailureMessageContains(hint) {
probe.ref ! "hello"
probe.expectMsg(0 millis, hint, "bye")
}
} }
"have an AutoPilot" in { "have an AutoPilot" in {