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 cb2be54e63..42e94974af 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 @@ -272,16 +272,22 @@ private[akka] final class TestProbeImpl[M](name: String, system: ActorSystem[_]) @tailrec def poll(t: Duration): A = { + // cannot use null-ness of result as signal it failed + // because Java API and not wanting to return a value will be "return null" + var failed = false val result: A = try { - a + val aRes = a + failed = false + aRes } catch { case NonFatal(e) ⇒ + failed = true if ((now + t) >= stop) throw e else null.asInstanceOf[A] } - if (result != null) result + if (!failed) result else { Thread.sleep(t.toMillis) poll((stop - now) min interval) diff --git a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala index 8b0cc21433..ef85cdb9b7 100644 --- a/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala +++ b/akka-actor-testkit-typed/src/main/scala/akka/actor/testkit/typed/javadsl/TestProbe.scala @@ -269,7 +269,7 @@ abstract class TestProbe[M] { fishForMessage(max, "", fisher) /** - * Same as the other `fishForMessageJava` but includes the provided hint in all error messages + * Same as the other `fishForMessage` but includes the provided hint in all error messages */ def fishForMessage(max: Duration, hint: String, fisher: java.util.function.Function[M, FishingOutcome]): java.util.List[M] = fishForMessage_internal(max.asScala, hint, fisher.apply).asJava diff --git a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/TestProbeTest.java b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/TestProbeTest.java index 807fe7e806..ce8d9077bb 100644 --- a/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/TestProbeTest.java +++ b/akka-actor-testkit-typed/src/test/java/akka/actor/testkit/typed/javadsl/TestProbeTest.java @@ -5,6 +5,7 @@ package akka.actor.testkit.typed.javadsl; import java.time.Duration; +import java.util.Arrays; import java.util.List; import akka.actor.testkit.typed.scaladsl.TestProbeSpec; @@ -54,4 +55,57 @@ public class TestProbeTest extends JUnitSuite { probe.receiveOne(Duration.ofMillis(100)); } + @Test + public void testAwaitAssert() { + TestProbe probe = TestProbe.create(testKit.system()); + probe.awaitAssert(() -> { + // ... something ... + return null; + }); + probe.awaitAssert(Duration.ofSeconds(3), () -> { + // ... something ... + return null; + }); + String awaitAssertResult = + probe.awaitAssert(Duration.ofSeconds(3), Duration.ofMillis(100), () -> { + // ... something ... + return "some result"; + }); + assertEquals("some result", awaitAssertResult); + } + + @Test + public void testExpectMessage() { + TestProbe probe = TestProbe.create(testKit.system()); + probe.getRef().tell("message"); + String messageResult = probe.expectMessage("message"); + probe.getRef().tell("message2"); + String expectClassResult = probe.expectMessageClass(String.class); + probe.expectNoMessage(); + } + + @Test + public void testFish() { + TestProbe probe = TestProbe.create(testKit.system()); + probe.getRef().tell("one"); + probe.getRef().tell("one"); + probe.getRef().tell("two"); + List results = probe.fishForMessage(Duration.ofSeconds(3), "hint", message -> { + if (message.equals("one")) return FishingOutcomes.continueAndIgnore(); + else if (message.equals("two")) return FishingOutcomes.complete(); + else return FishingOutcomes.fail("error"); + }); + assertEquals(Arrays.asList("two"), results); + } + + @Test + public void testWithin() { + TestProbe probe = TestProbe.create(testKit.system()); + String withinResult = probe.within(Duration.ofSeconds(3), () -> { + // ... something ... + return "result"; + }); + assertEquals("result", withinResult); + } + }