diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala index 863dbc3cba..7ec80dede8 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/internal/TestProbeImpl.scala @@ -184,31 +184,37 @@ private[akka] final class TestProbeImpl[M](name: String, system: ActorSystem[_]) // not tailrec but that should be ok def loop(timeout: FiniteDuration, seen: List[M]): List[M] = { val start = System.nanoTime() - val msg = receiveOne(timeout) - try { - fisher(msg) match { - case FishingOutcome.Complete ⇒ (msg :: seen).reverse - case FishingOutcome.Fail(error) ⇒ throw new AssertionError(s"$error, hint: $hint") - case continue ⇒ - val newTimeout = - if (timeout.isFinite()) timeout - (System.nanoTime() - start).nanos - else timeout - if (newTimeout.toMillis <= 0) { - throw new AssertionError(s"timeout ($max) during fishForMessage, seen messages ${seen.reverse}, hint: $hint") - } else { + val maybeMsg = Option(receiveOne(timeout)) + maybeMsg match { + case Some(msg) ⇒ + try { + fisher(msg) match { + case FishingOutcome.Complete ⇒ (msg :: seen).reverse + case FishingOutcome.Fail(error) ⇒ throw new AssertionError(s"$error, hint: $hint") + case continue ⇒ + val newTimeout = + if (timeout.isFinite()) timeout - (System.nanoTime() - start).nanos + else timeout + if (newTimeout.toMillis <= 0) { + throw new AssertionError(s"timeout ($max) during fishForMessage, seen messages ${seen.reverse}, hint: $hint") + } else { - continue match { - case FishingOutcome.Continue ⇒ loop(newTimeout, msg :: seen) - case FishingOutcome.ContinueAndIgnore ⇒ loop(newTimeout, seen) - case _ ⇒ ??? // cannot happen - } + continue match { + case FishingOutcome.Continue ⇒ loop(newTimeout, msg :: seen) + case FishingOutcome.ContinueAndIgnore ⇒ loop(newTimeout, seen) + case _ ⇒ ??? // cannot happen + } + } } - } - } catch { - case ex: MatchError ⇒ throw new AssertionError( - s"Unexpected message $msg while fishing for messages, " + - s"seen messages ${seen.reverse}, hint: $hint", ex) + } catch { + case ex: MatchError ⇒ throw new AssertionError( + s"Unexpected message $msg while fishing for messages, " + + s"seen messages ${seen.reverse}, hint: $hint", ex) + } + + case None ⇒ + throw new AssertionError(s"timeout ($max) during fishForMessage, seen messages ${seen.reverse}, hint: $hint") } } diff --git a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala index 09d0368080..40da1e9e2f 100644 --- a/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala +++ b/akka-actor-testkit-typed/src/test/scala/akka/actor/testkit/typed/scaladsl/TestProbeSpec.scala @@ -4,8 +4,8 @@ package akka.actor.testkit.typed.scaladsl -import scala.concurrent.duration._ import akka.actor.typed.scaladsl.Behaviors +import scala.concurrent.duration._ import org.scalatest.WordSpecLike class TestProbeSpec extends ScalaTestWithActorTestKit with WordSpecLike { @@ -84,6 +84,17 @@ class TestProbeSpec extends ScalaTestWithActorTestKit with WordSpecLike { } } + "throw an AssertionError when the fishing probe times out" in { + val probe = TestProbe[AnyRef]() + + assertThrows[AssertionError] { + probe.fishForMessage(100.millis) { _ ⇒ + Thread.sleep(150) + FishingOutcomes.complete + } + } + } + "fail for unknown message when fishing for messages" in { val probe = TestProbe[String]()