diff --git a/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java b/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java index adeab19ddc..13473cf337 100644 --- a/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java +++ b/akka-persistence/src/test/java/akka/persistence/fsm/AbstractPersistentFSMTest.java @@ -126,7 +126,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite { PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class); assertTransition(stateTransition, fsmRef, UserState.LOOKING_AROUND, UserState.SHOPPING); - new Within(duration("0.9 seconds"), duration("1.9 seconds")) { + new Within(duration("0.9 seconds"), remainingOrDefault()) { @Override protected void run() { PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class); @@ -134,7 +134,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite { } }; - new Within(duration("1.9 seconds"), duration("2.9 seconds")) { + new Within(duration("1.9 seconds"), remainingOrDefault()) { @Override protected void run() { expectTerminated(fsmRef); @@ -312,7 +312,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite { currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class); assertEquals(currentState.state(), UserState.SHOPPING); - new Within(duration("0.9 seconds"), duration("1.9 seconds")) { + new Within(duration("0.9 seconds"), remainingOrDefault()) { @Override protected void run() { PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class); @@ -331,7 +331,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite { currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class); assertEquals(currentState.state(), UserState.INACTIVE); - new Within(duration("1.9 seconds"), duration("2.9 seconds")) { + new Within(duration("1.9 seconds"), remainingOrDefault()) { @Override protected void run() { expectTerminated(recoveredFsmRef2); diff --git a/akka-persistence/src/test/scala/akka/persistence/fsm/PersistentFSMSpec.scala b/akka-persistence/src/test/scala/akka/persistence/fsm/PersistentFSMSpec.scala index 2022b1d1dc..5dbeb63417 100644 --- a/akka-persistence/src/test/scala/akka/persistence/fsm/PersistentFSMSpec.scala +++ b/akka-persistence/src/test/scala/akka/persistence/fsm/PersistentFSMSpec.scala @@ -71,7 +71,7 @@ abstract class PersistentFSMSpec(config: Config) extends PersistenceSpec(config) expectMsg(CurrentState(fsmRef, LookingAround, None)) expectMsg(Transition(fsmRef, LookingAround, Shopping, Some(1 second))) - within(0.9 seconds, 1.9 seconds) { + within(0.9 seconds, remainingOrDefault) { expectMsg(Transition(fsmRef, Shopping, Inactive, Some(2 seconds))) } @@ -201,7 +201,7 @@ abstract class PersistentFSMSpec(config: Config) extends PersistenceSpec(config) expectMsg(CurrentState(recoveredFsmRef, Shopping, Some(1 second))) - within(0.9 seconds, 1.9 seconds) { + within(0.9 seconds, remainingOrDefault) { expectMsg(Transition(recoveredFsmRef, Shopping, Inactive, Some(2 seconds))) } diff --git a/akka-testkit/src/main/java/akka/testkit/JavaTestKit.java b/akka-testkit/src/main/java/akka/testkit/JavaTestKit.java index ce0a98097d..6ea34a3048 100644 --- a/akka-testkit/src/main/java/akka/testkit/JavaTestKit.java +++ b/akka-testkit/src/main/java/akka/testkit/JavaTestKit.java @@ -26,24 +26,24 @@ import java.util.concurrent.TimeUnit; * bounds concerning timing are available in the form of Within * blocks. *

- * + * * Beware of two points: *

- * + * *

- * - * + * + * */ public class JavaTestKit { /** @@ -203,6 +203,16 @@ public class JavaTestKit { return p.remainingOr(duration); } + /** + * Obtain time remaining for execution of the innermost enclosing + * Within block or missing that it returns the properly dilated + * default for this case from settings (key + * "akka.test.single-expect-default"). + */ + public FiniteDuration remainingOrDefault() { + return p.remainingOrDefault(); + } + /** * Execute code block while bounding its execution time between * min and max. Within blocks may be @@ -210,11 +220,11 @@ public class JavaTestKit { * available in a version which implicitly uses the remaining time governed by * the innermost enclosing Within block. *

- * + * * Note that the timeout is scaled using dilated, which uses the * configuration entry "akka.test.timefactor", while the min Duration is not. *

- * + * *

    * 
    * // the run() method needs to finish within 3 seconds
@@ -231,6 +241,7 @@ public class JavaTestKit {
 
     public Within(FiniteDuration max) {
       p.within(max, new AbstractFunction0() {
+        @Override
         public Object apply() {
           run();
           return null;
@@ -240,6 +251,7 @@ public class JavaTestKit {
 
     public Within(FiniteDuration min, FiniteDuration max) {
       p.within(min, max, new AbstractFunction0() {
+        @Override
         public Object apply() {
           run();
           return null;
@@ -252,11 +264,11 @@ public class JavaTestKit {
    * Await until the given condition evaluates to true or the
    * timeout expires, whichever comes first.
    * 

- * + * * If no timeout is given, take it from the innermost enclosing * Within block. *

- * + * * Note that the timeout is scaled using Duration.dilated, which uses the * configuration entry "akka.test.timefactor". */ @@ -273,6 +285,7 @@ public class JavaTestKit { public AwaitCond(Duration max, Duration interval) { p.awaitCond(new AbstractFunction0() { + @Override public Object apply() { return cond(); } @@ -281,6 +294,7 @@ public class JavaTestKit { public AwaitCond(Duration max, Duration interval, String message) { p.awaitCond(new AbstractFunction0() { + @Override public Object apply() { return cond(); } @@ -293,11 +307,11 @@ public class JavaTestKit { * expires, whichever comes first. If the timeout expires the last exception * is thrown. *

- * + * * If no timeout is given, take it from the innermost enclosing * Within block. *

- * + * * Note that the timeout is scaled using Duration.dilated, which uses the * configuration entry "akka.test.timefactor". */ @@ -314,6 +328,7 @@ public class JavaTestKit { public AwaitAssert(Duration max, Duration interval) { p.awaitAssert(new AbstractFunction0() { + @Override public Object apply() { check(); return null; @@ -329,10 +344,10 @@ public class JavaTestKit { *

* The received object as transformed by the matching function can be * retrieved with the get method. - * + * * Use this variant to implement more complicated or conditional processing. *

- * + * *

    * 
    * final String out = new ExpectMsg<String>("match hint") {
@@ -385,7 +400,7 @@ public class JavaTestKit {
    * 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.
-   * 
+   *
    * @return the received object
    */
   public  T expectMsgEquals(FiniteDuration max, T msg) {
@@ -404,7 +419,7 @@ public class JavaTestKit {
    * 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.
-   * 
+   *
    * @return the received object
    */
   public  T expectMsgClass(FiniteDuration max, Class clazz) {
@@ -423,7 +438,7 @@ public class JavaTestKit {
    * 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.
-   * 
+   *
    * @return the received object
    */
   public Object expectMsgAnyOf(FiniteDuration max, Object... msgs) {
@@ -463,7 +478,7 @@ public class JavaTestKit {
    * 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.
-   * 
+   *
    * @return the received object
    */
   public Object expectMsgAnyClassOf(FiniteDuration max, Class... classes) {
@@ -523,7 +538,7 @@ public class JavaTestKit {
    * 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! */ public Object receiveOne(Duration max) { @@ -536,11 +551,11 @@ public class JavaTestKit { * default) or the overall maximum duration is elapsed. Returns the sequence * of messages. *

- * + * * Note that it is not an error to hit the max duration in this * case. *

- * + * * One possible use of this method is for testing whether messages of certain * characteristics are generated at a certain rate. */ @@ -585,7 +600,7 @@ public class JavaTestKit { * that you can keep your test run’s console output clean and do not miss real * error messages. *

- * + * * If the occurrences is set to Integer.MAX_VALUE, * no tracking is done. */ @@ -627,6 +642,7 @@ public class JavaTestKit { } else throw new IllegalArgumentException("unknown LogLevel " + clazz); return filter.intercept(new AbstractFunction0() { + @Override public T apply() { return run(); } @@ -687,7 +703,7 @@ public class JavaTestKit { public void shutdown(ActorSystem actorSystem, Boolean verifySystemShutdown) { shutdown(actorSystem, null, verifySystemShutdown); } - + /** * Spawns an actor as a child of this test actor, and returns the child's ActorRef. * @param props Props to create the child actor @@ -697,7 +713,7 @@ public class JavaTestKit { public ActorRef childActorOf(Props props, String name, SupervisorStrategy supervisorStrategy) { return p.childActorOf(props, name, supervisorStrategy); } - + /** * Spawns an actor as a child of this test actor, and returns the child's ActorRef. * The actor will have an auto-generated name. @@ -707,7 +723,7 @@ public class JavaTestKit { public ActorRef childActorOf(Props props, SupervisorStrategy supervisorStrategy) { return p.childActorOf(props, supervisorStrategy); } - + /** * Spawns an actor as a child of this test actor, and returns the child's ActorRef. * The actor will be supervised using {@link SupervisorStrategy.stoppingStrategy}. @@ -717,7 +733,7 @@ public class JavaTestKit { public ActorRef childActorOf(Props props, String name) { return p.childActorOf(props, name); } - + /** * Spawns an actor as a child of this test actor, and returns the child's ActorRef. * The actor will have an auto-generated name and will be supervised using {@link SupervisorStrategy.stoppingStrategy}.