Class TestProbe<M>
- All Implemented Interfaces:
RecipientRef<M>
create factories in the TestProbe companion
or via ActorTestKit.createTestProbe().
A test probe is essentially a queryable mailbox which can be used in place of an actor and the received messages can then be asserted etc.
Not for user extension
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.pekko.actor.typed.RecipientRef
RecipientRef.RecipientRefOps<T>, RecipientRef.RecipientRefOps$ -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <A> AawaitAssert(Duration max, Duration interval, Creator<A> creator) Evaluate the given assert everyintervaluntil it does not throw an exception and return the result.abstract <A> AawaitAssert(Duration max, Creator<A> creator) Evaluate the given assert every 100 milliseconds until it does not throw an exception and return the result.abstract <A> AawaitAssert(Creator<A> creator) Evaluate the given assert every 100 milliseconds until it does not throw an exception and return the result.static <M> TestProbe<M>create(Class<M> clazz, ActorSystem<?> system) static <M> TestProbe<M>create(String name, Class<M> clazz, ActorSystem<?> system) static <M> TestProbe<M>create(String name, ActorSystem<?> system) static <M> TestProbe<M>create(ActorSystem<?> system) abstract <T extends M>
TexpectMessage(Duration max, String hint, T obj) Receive one message from the test actor and assert that it equals the given object.abstract <T extends M>
TexpectMessage(Duration max, T obj) Receive one message from the test actor and assert that it equals the given object.abstract <T extends M>
TexpectMessage(T obj) Same asexpectMessage(remainingOrDefault, obj), but using the default timeout as deadline.abstract <T extends M>
TexpectMessageClass(Class<T> clazz) Same asexpectMessageType(clazz, remainingOrDefault),but using the default timeout as deadline.abstract <T extends M>
TexpectMessageClass(Class<T> clazz, Duration max) Wait for a message of type M and return it when it arrives, or fail if themaxtimeout is hit.abstract voidAssert that no message is received.abstract voidexpectNoMessage(Duration max) Assert that no message is received for the specified time.abstract <U> voidexpectTerminated(ActorRef<U> actorRef) Expect the given actor to be stopped or stop within the default timeout.abstract <U> voidexpectTerminated(ActorRef<U> actorRef, Duration max) Expect the given actor to be stopped or stop within the given timeout or throw anAssertionError.fishForMessage(Duration max, String hint, Function<M, FishingOutcome> fisher) Same as the otherfishForMessagebut includes the provided hint in all error messagesfishForMessage(Duration max, Function<M, FishingOutcome> fisher) Java API: Allows for flexible matching of multiple messages within a timeout, the fisher function is fed each incoming message, and returns one of the following effects to decide on what happens next:getRef()ActorRef for this TestProbeabstract DurationObtain time remaining for execution of the innermost enclosingwithinblock or throw anAssertionErrorif nowithinblock surrounds this call.abstract DurationgetRemainingOr(Duration duration) Obtain time remaining for execution of the innermost enclosingwithinblock or missing that it returns the given duration.abstract DurationObtain time remaining for execution of the innermost enclosingwithinblock or missing that it returns the properly dilated default for this case from settings (key "pekko.actor.testkit.typed.single-expect-default").abstract MReceive one message of typeMwithin the default timeout as deadline.abstract MreceiveMessage(Duration max) Receive one message of typeM.receiveSeveralMessages(int n) Same asreceiveSeveralMessages(n, remaining)but using the default timeout as deadline.receiveSeveralMessages(int n, Duration max) Receivenmessages in a row before the given deadline.ref()ActorRef for this TestProbeprotected abstract TestKitSettingssettings()abstract voidstop()Stops theTestProbe.getRef, which is useful when testing watch and termination.abstract <T> TExecute code block while bounding its execution time betweenminandmax.abstract <T> TSame as callingwithin(0 seconds, max)(f).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.pekko.actor.typed.RecipientRef
tell
-
Constructor Details
-
TestProbe
public TestProbe()
-
-
Method Details
-
create
-
create
-
create
-
create
-
settings
-
ref
ActorRef for this TestProbe -
getRef
ActorRef for this TestProbe -
getRemainingOrDefault
Obtain time remaining for execution of the innermost enclosingwithinblock or missing that it returns the properly dilated default for this case from settings (key "pekko.actor.testkit.typed.single-expect-default"). -
getRemaining
Obtain time remaining for execution of the innermost enclosingwithinblock or throw anAssertionErrorif nowithinblock surrounds this call. -
getRemainingOr
Obtain time remaining for execution of the innermost enclosingwithinblock or missing that it returns the given duration. -
within
Execute code block while bounding its execution time betweenminandmax.withinblocks may be nested. All methods in this trait which take maximum wait times are available in a version which implicitly uses the remaining time governed by the innermost enclosingwithinblock.Note that the max timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor", while the min Duration is not.
val ret = within(50 millis) { test ! Ping expectMessageType[Pong] } -
within
Same as callingwithin(0 seconds, max)(f). -
expectMessage
Same asexpectMessage(remainingOrDefault, obj), but using the default timeout as deadline. -
expectMessage
Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with anAssertionErrorbeing thrown in case of timeout.- Returns:
- the received object
-
expectMessage
Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with anAssertionErrorbeing thrown in case of timeout.- Returns:
- the received object
-
expectNoMessage
Assert that no message is received for the specified time. Supplied value is not dilated. -
expectNoMessage
public abstract void expectNoMessage()Assert that no message is received. Waits for the default period configured aspekko.actor.testkit.typed.expect-no-message-default. That timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor". -
expectMessageClass
Same asexpectMessageType(clazz, remainingOrDefault),but using the default timeout as deadline. -
expectMessageClass
Wait for a message of type M and return it when it arrives, or fail if themaxtimeout is hit.Note that the timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".
-
receiveMessage
Receive one message of typeMwithin the default timeout as deadline. -
receiveMessage
Receive one message of typeM. Wait time is bounded by themaxduration, with anAssertionErrorraised in case of timeout. -
receiveSeveralMessages
Same asreceiveSeveralMessages(n, remaining)but using the default timeout as deadline. -
receiveSeveralMessages
Receivenmessages in a row before the given deadline.Note that the timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".
-
fishForMessage
Java API: Allows for flexible matching of multiple messages within a timeout, the fisher function is fed each incoming message, and returns one of the following effects to decide on what happens next:*
FishingOutcomes.continueAndCollect- continue with the next message given that the timeout has not been reached *FishingOutcomes.complete- successfully complete and return the message *FishingOutcomes.fail- fail the test with a custom messageAdditionally failures includes the list of messages consumed. If a message of type
Mbut not of typeTis received this will also fail the test, additionally if thefisherfunction throws a match error the error is decorated with some fishing details and the test is failed (making it convenient to use this method with a partial function).- Parameters:
max- Max total time without the fisher function returningCompleteFishingbefore failing. The timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".- Returns:
- The messages accepted in the order they arrived
-
fishForMessage
public abstract List<M> fishForMessage(Duration max, String hint, Function<M, FishingOutcome> fisher) Same as the otherfishForMessagebut includes the provided hint in all error messages -
expectTerminated
Expect the given actor to be stopped or stop within the given timeout or throw anAssertionError.Note that the timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".
-
expectTerminated
Expect the given actor to be stopped or stop within the default timeout. -
awaitAssert
Evaluate the given assert everyintervaluntil it does not throw an exception and return the result.If the
maxtimeout expires the last exception is thrown.Note that the timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".
-
awaitAssert
Evaluate the given assert every 100 milliseconds until it does not throw an exception and return the result.If the
maxtimeout expires the last exception is thrown.Note that the timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor".
-
awaitAssert
Evaluate the given assert every 100 milliseconds until it does not throw an exception and return the result. A max time is taken it from the innermost enclosingwithinblock. -
stop
public abstract void stop()Stops theTestProbe.getRef, which is useful when testing watch and termination.
-