diff --git a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala index 42abee6c04..fa4896d0df 100644 --- a/akka-testkit/src/main/scala/akka/testkit/TestKit.scala +++ b/akka-testkit/src/main/scala/akka/testkit/TestKit.scala @@ -263,6 +263,27 @@ class TestKit(_app: AkkaApplication) { f(o) } + /** + * Hybrid of expectMsgPF and receiveWhile: receive messages while the + * partial function matches and returns false. Use it to ignore certain + * messages while waiting for a specific message. + * + * @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 + val end = now + _max + @tailrec + def recv: Any = { + val o = receiveOne(end - now) + assert(o ne null, "timeout during fishForMessage, hint: " + hint) + assert(f.isDefinedAt(o), "fishForMessage(" + hint + ") found unexpected message " + o) + if (f(o)) o else recv + } + recv + } + /** * Same as `expectMsgType[T](remaining)`, but correctly treating the timeFactor. */