fix javadsl awaitAssert in Typed, #25903
* returning null would result in retries until "timeout value is negative" * use same implementation as in untyped * real test
This commit is contained in:
parent
850a10443b
commit
0d0a0e8018
3 changed files with 63 additions and 3 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<String> 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<String> 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<String> probe = TestProbe.create(testKit.system());
|
||||
probe.getRef().tell("one");
|
||||
probe.getRef().tell("one");
|
||||
probe.getRef().tell("two");
|
||||
List<String> 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<String> probe = TestProbe.create(testKit.system());
|
||||
String withinResult = probe.within(Duration.ofSeconds(3), () -> {
|
||||
// ... something ...
|
||||
return "result";
|
||||
});
|
||||
assertEquals("result", withinResult);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue