Class TestKit
expectMsg... methods. Assertions and
bounds concerning timing are available in the form of Within
blocks.
Beware of two points:
- the ActorSystem passed into the constructor needs to be shutdown,
otherwise thread pools and memory will be leaked - this trait is not
thread-safe (only one actor with one queue, one stack of Within
blocks); take care not to run tests within a single test class instance in
parallel.
- It should be noted that for CI servers and the like all maximum Durations
are scaled using the dilated method, which uses the
TestKitExtension.Settings.TestTimeFactor settable via pekko.conf entry
"pekko.test.timefactor".
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<A> AawaitAssert(Duration max, Duration interval, Supplier<A> a) Evaluate the given assert everyintervaluntil it does not throw an exception.<A> AawaitAssert(Duration max, Supplier<A> a) Evaluate the given assert everyintervaluntil it does not throw an exception and return the result.<A> AawaitAssert(Supplier<A> a) Evaluate the given assert everyintervaluntil it does not throw an exception and return the result.voidAwait until the given condition evaluates totrueor the timeout expires, whichever comes first.voidAwait until the given condition evaluates totrueor the timeout expires, whichever comes first.voidAwait until the given condition evaluates totrueor the timeout expires, whichever comes first.voidAwait until the given condition evaluates totrueor the timeout expires, whichever comes first.childActorOf(Props props) Spawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef.childActorOf(Props props, String name) Spawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef.childActorOf(Props props, String name, SupervisorStrategy supervisorStrategy) Spawns an actor as a child of this test actor, and returns the child's ActorRef.childActorOf(Props props, SupervisorStrategy supervisorStrategy) Spawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef.Java timeouts (durations) during tests with the configuredscala.concurrent.duration.FiniteDuration<T> TReceive one message from the test actor and assert that it equals the given object.<T> TReceive one message from the test actor and assert that it equals the given object.<T> TexpectMsg(T obj) Same asexpectMsg(remainingOrDefault, obj), but correctly treating the timeFactor.<T> List<T>expectMsgAllOf(Object... objs) Same asexpectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> List<T>expectMsgAllOf(scala.collection.immutable.Seq<T> objs) Same asexpectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> List<T>expectMsgAllOfWithin(Duration max, Object... objs) Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa.<T> List<T>expectMsgAllOfWithin(Duration max, scala.collection.immutable.Seq<T> objs) Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa.<T> TexpectMsgAnyClassOf(Class<?>... objs) Same asexpectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> TexpectMsgAnyClassOf(Duration max, Class<?>... objs) Receive one message from the test actor and assert that it conforms to one of the given classes.<T> TexpectMsgAnyClassOf(Duration max, scala.collection.immutable.Seq<Class<?>> objs) Receive one message from the test actor and assert that it conforms to one of the given classes.<T> TexpectMsgAnyClassOf(scala.collection.immutable.Seq<Class<?>> objs) Same asexpectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> TexpectMsgAnyOf(T first, Object... objs) Same asexpectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> TexpectMsgAnyOf(T first, scala.collection.immutable.Seq<T> objs) Same asexpectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor.<T> TexpectMsgAnyOfWithin(Duration max, Object... objs) Receive one message from the test actor and assert that it equals one of the given objects.<T> TexpectMsgAnyOfWithin(Duration max, scala.collection.immutable.Seq<T> objs) Receive one message from the test actor and assert that it equals one of the given objects.<T> TexpectMsgClass(Class<T> c) Same asexpectMsgClass(remainingOrDefault, c), but correctly treating the timeFactor.<T> TexpectMsgClass(Duration max, Class<T> c) Receive one message from the test actor and assert that it conforms to the given class.<T> TexpectMsgEquals(Duration max, T obj) Receive one message from the test actor and assert that it equals the given object.<T> TexpectMsgEquals(T obj) Same asexpectMsg(remainingOrDefault, obj), but correctly treating the timeFactor.<T> TexpectMsgPF(String hint, Function<Object, T> f) Receive one message from the test actor and assert that the given partial function accepts it.<T> TexpectMsgPF(Duration max, String hint, Function<Object, T> f) Receive one message from the test actor and assert that the given partial function accepts it.voidAssert that no message is received.voidexpectNoMessage(Duration max) Assert that no message is received for the specified time.expectTerminated(Duration max, ActorRef target) Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef.expectTerminated(ActorRef target) Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef.Hybrid of expectMsgPF and receiveWhile: receive messages while the partial function matches and returns false.<T> TfishForSpecificMessage(Duration max, String hint, Function<Object, T> f) Same asfishForMessage, but gets a different partial function and returns properly typed message.voidForward this message as if in the TestActor's receive method with self.forward.Get the last sender of the TestProbegetRef()Shorthand to get the testActor.Obtain time remaining for execution of the innermost enclosingwithinblock or throw anAssertionErrorif nowithinblock surrounds this call.getRemainingOr(Duration duration) Obtain time remaining for execution of the innermost enclosingwithinblock or missing that it returns the given duration.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.test.single-expect-default").ActorRef of the test actor.voidIgnore all messages in the test actor for which the given partial function returns true.voidStop ignoring messages in the test actor.booleanQuery queue status.receiveN(int n) Same asreceiveN(n, remaining)but correctly taking into account Duration.timeFactor.Receive N messages in a row before the given deadline.receiveOne(Duration max) Receive one message from the internal queue of the TestActor.<T> List<T>receiveWhile(Duration max, Duration idle, int messages, Function<Object, T> f) Receive a series of messages until one does not match the given partial function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed or expected messages count is reached.<T> List<T>receiveWhile(Duration max, Function<Object, T> f) voidSend message to the sender of the last dequeued message.voidSend message to an actor while using the probe's TestActor as the sender.voidsetAutoPilot(TestActor.AutoPilot pilot) Install an AutoPilot to drive the testActor: the AutoPilot will be run for each received message and can be used to send or forward messages, etc.static voidshutdownActorSystem(ActorSystem actorSystem) Shut down an actor system and wait for termination.static voidshutdownActorSystem(ActorSystem actorSystem, boolean verifySystemShutdown) Shut down an actor system and wait for termination.static voidshutdownActorSystem(ActorSystem actorSystem, Duration duration) Shut down an actor system and wait for termination.static voidshutdownActorSystem(ActorSystem actorSystem, Duration duration, boolean verifySystemShutdown) Shut down an actor system and wait for termination.static voidshutdownActorSystem(ActorSystem actorSystem, scala.concurrent.duration.Duration duration) Shut down an actor system and wait for termination.static voidshutdownActorSystem(ActorSystem actorSystem, scala.concurrent.duration.Duration duration, boolean verifySystemShutdown) Shut down an actor system and wait for termination.Have the testActor stop watching someone (i.e.Have the testActor watch someone (i.e.<T> TExecute code block while bounding its execution time betweenminandmax.<T> TExecute code block while bounding its execution time betweenminandmax.
-
Constructor Details
-
TestKit
-
-
Method Details
-
shutdownActorSystem
public static void shutdownActorSystem(ActorSystem actorSystem, scala.concurrent.duration.Duration duration, boolean verifySystemShutdown) Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system.If verifySystemShutdown is true, then an exception will be thrown on failure.
-
shutdownActorSystem
Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system. -
shutdownActorSystem
public static void shutdownActorSystem(ActorSystem actorSystem, scala.concurrent.duration.Duration duration) Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system. -
shutdownActorSystem
Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system.If verifySystemShutdown is true, then an exception will be thrown on failure.
-
shutdownActorSystem
Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system.The
durationis dilated by the timefactor. This overloaded method acceptsjava.time.Duration.If verifySystemShutdown is true, then an exception will be thrown on failure.
- Since:
- 1.3.0
-
shutdownActorSystem
public static void shutdownActorSystem(ActorSystem actorSystem, Duration duration, boolean verifySystemShutdown) Shut down an actor system and wait for termination. On failure debug output will be logged about the remaining actors in the system.The
durationis dilated by the timefactor. This overloaded method acceptsjava.time.Duration.- Since:
- 1.3.0
-
expectMsgAnyOf
Same asexpectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAnyOfWithin
Receive one message from the test actor and assert that it equals one of the given objects. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgAllOf
Same asexpectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAllOfWithin
Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa. This construct is useful when the order in which the objects are received is not fixed. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgAnyClassOf
Same asexpectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAnyClassOf
Receive one message from the test actor and assert that it conforms to one of the given classes. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
getTestActor
ActorRef of the test actor. Access is provided to enable e.g. registration as message target. -
getRef
Shorthand to get the testActor. -
getSystem
-
duration
-
dilated
Java timeouts (durations) during tests with the configured -
msgAvailable
public boolean msgAvailable()Query queue status. -
getLastSender
Get the last sender of the TestProbe -
send
Send message to an actor while using the probe's TestActor as the sender. Replies will be available for inspection with all of TestKit's assertion methods. -
forward
Forward this message as if in the TestActor's receive method with self.forward. -
reply
Send message to the sender of the last dequeued message. -
watch
Have the testActor watch someone (i.e.context.watch(...)). -
unwatch
Have the testActor stop watching someone (i.e.context.unwatch(...)). -
ignoreMsg
Ignore all messages in the test actor for which the given partial function returns true. -
ignoreNoMsg
public void ignoreNoMsg()Stop ignoring messages in the test actor. -
setAutoPilot
Install an AutoPilot to drive the testActor: the AutoPilot will be run for each received message and can be used to send or forward messages, etc. Each invocation must return the AutoPilot for the next round. -
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. -
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.test.single-expect-default"). -
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 timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor", while the min Duration is not.
within(java.time.Duration.ofMillis(50), () -> { test.tell("ping"); return expectMsgClass(String.class); }); -
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 timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor", while the min Duration is not.
within(java.time.Duration.ofMillis(50), () -> { test.tell("ping"); return expectMsgClass(String.class); }); -
awaitCond
Await until the given condition evaluates totrueor the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
awaitCond
Await until the given condition evaluates totrueor the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
awaitCond
Await until the given condition evaluates totrueor the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
awaitCond
Await until the given condition evaluates totrueor the timeout expires, whichever comes first.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
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.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
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.If no timeout is given, take it from the innermost enclosing
withinblock.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
-
awaitAssert
Evaluate the given assert everyintervaluntil it does not throw an exception. If themaxtimeout expires the last exception is thrown.Note that the timeout is scaled using Duration.dilated, which uses the configuration entry "pekko.test.timefactor".
- Returns:
- an arbitrary value that would be returned from awaitAssert if successful, if not interested in such value you can return null.
-
expectMsgEquals
public <T> T expectMsgEquals(T obj) Same asexpectMsg(remainingOrDefault, obj), but correctly treating the timeFactor. -
expectMsgEquals
Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.- Returns:
- the received object
-
expectMsg
public <T> T expectMsg(T obj) Same asexpectMsg(remainingOrDefault, obj), but correctly treating the timeFactor. -
expectMsg
Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsg
Receive one message from the test actor and assert that it equals the given object. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgPF
Receive one message from the test actor and assert that the given partial function accepts it. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.Use this variant to implement more complicated or conditional processing.
-
expectMsgPF
Receive one message from the test actor and assert that the given partial function accepts it. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.Use this variant to implement more complicated or conditional processing.
-
expectMsgClass
Same asexpectMsgClass(remainingOrDefault, c), but correctly treating the timeFactor. -
expectMsgClass
Receive one message from the test actor and assert that it conforms to the given class. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgAnyOf
public <T> T expectMsgAnyOf(T first, scala.collection.immutable.Seq<T> objs) Same asexpectMsgAnyOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAnyOfWithin
Receive one message from the test actor and assert that it equals one of the given objects. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgAllOf
Same asexpectMsgAllOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAllOfWithin
Receive a number of messages from the test actor matching the given number of objects and assert that for each given object one is received which equals it and vice versa. This construct is useful when the order in which the objects are received is not fixed. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectMsgAnyClassOf
Same asexpectMsgAnyClassOf(remainingOrDefault, obj...), but correctly treating the timeFactor. -
expectMsgAnyClassOf
Receive one message from the test actor and assert that it conforms to one of the given classes. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout. -
expectNoMessage
public void expectNoMessage()Assert that no message is received. Waits for the default period configured aspekko.actor.testkit.expect-no-message-default. That timeout is scaled using the configuration entry "pekko.actor.testkit.typed.timefactor". -
expectNoMessage
Assert that no message is received for the specified time. Supplied value is not dilated. -
expectTerminated
Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef. Before calling this method, you have towatchthe target actor ref. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.- Parameters:
max- wait no more than max time, otherwise throw AssertionFailuretarget- the actor ref expected to be Terminated- Returns:
- the received Terminated message
-
expectTerminated
Receive one message from the test actor and assert that it is the Terminated message of the given ActorRef. Before calling this method, you have towatchthe target actor ref. Wait time is bounded by the given duration, with an AssertionFailure being thrown in case of timeout.- Parameters:
target- the actor ref expected to be Terminated- Returns:
- the received Terminated message
-
fishForMessage
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.- Returns:
- the last received message, i.e. the first one for which the partial function returned true
-
fishForSpecificMessage
Same asfishForMessage, but gets a different partial function and returns properly typed message. -
receiveN
Same asreceiveN(n, remaining)but correctly taking into account Duration.timeFactor. -
receiveN
Receive N messages in a row before the given deadline. -
receiveOne
Receive one message from the internal queue of the TestActor. If the given duration is zero, the queue is polled (non-blocking).This method does NOT automatically scale its Duration parameter!
-
receiveWhile
Receive a series of messages until one does not match the given partial function or the idle timeout is met (disabled by default) or the overall maximum duration is elapsed or expected messages count is reached. Returns the sequence of messages.Note that it is not an error to hit the
maxduration in this case.One possible use of this method is for testing whether messages of certain characteristics are generated at a certain rate:
-
receiveWhile
-
childActorOf
Spawns an actor as a child of this test actor, and returns the child's ActorRef. -
childActorOf
Spawns an actor as a child of this test actor with an auto-generated name, and returns the child's ActorRef. -
childActorOf
Spawns an actor as a child of this test actor with a stopping supervisor strategy, and returns the child's ActorRef. -
childActorOf
Spawns an actor as a child of this test actor with an auto-generated name and stopping supervisor strategy, returning the child's ActorRef.
-