increase test timeout in PersistentFSMSpec, #20725 (#21694)

* and add remainingOrDefault in Java testkit
This commit is contained in:
Patrik Nordwall 2016-10-21 16:50:44 +02:00 committed by Konrad Malawski
parent 385ea37497
commit 679db55eca
3 changed files with 47 additions and 31 deletions

View file

@ -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);

View file

@ -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)))
}

View file

@ -26,24 +26,24 @@ import java.util.concurrent.TimeUnit;
* bounds concerning timing are available in the form of <code>Within</code>
* blocks.
* <p>
*
*
* Beware of two points:
* <p>
*
*
* <ul>
* <li>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 <code>Within</code>
* blocks); take care not to run tests within a single test class instance in
* parallel.</li>
*
*
* <li>It should be noted that for CI servers and the like all maximum Durations
* are scaled using the <code>dilated</code> method, which uses the
* TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry
* "akka.test.timefactor".</li>
* </ul>
*
*
*
*
*/
public class JavaTestKit {
/**
@ -203,6 +203,16 @@ public class JavaTestKit {
return p.remainingOr(duration);
}
/**
* Obtain time remaining for execution of the innermost enclosing
* <code>Within</code> 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
* <code>min</code> and <code>max</code>. <code>Within</code> 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 <code>Within</code> block.
* <p>
*
*
* Note that the timeout is scaled using <code>dilated</code>, which uses the
* configuration entry "akka.test.timefactor", while the min Duration is not.
* <p>
*
*
* <pre>
* <code>
* // 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<Object>() {
@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<Object>() {
@Override
public Object apply() {
run();
return null;
@ -252,11 +264,11 @@ public class JavaTestKit {
* Await until the given condition evaluates to <code>true</code> or the
* timeout expires, whichever comes first.
* <p>
*
*
* If no timeout is given, take it from the innermost enclosing
* <code>Within</code> block.
* <p>
*
*
* 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<Object>() {
@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<Object>() {
@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.
* <p>
*
*
* If no timeout is given, take it from the innermost enclosing
* <code>Within</code> block.
* <p>
*
*
* 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<Object>() {
@Override
public Object apply() {
check();
return null;
@ -329,10 +344,10 @@ public class JavaTestKit {
* <p>
* The received object as transformed by the matching function can be
* retrieved with the <code>get</code> method.
*
*
* Use this variant to implement more complicated or conditional processing.
* <p>
*
*
* <pre>
* <code>
* final String out = new ExpectMsg&lt;String&gt;("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> 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> T expectMsgClass(FiniteDuration max, Class<T> 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).
* <p>
*
*
* 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.
* <p>
*
*
* Note that it is not an error to hit the <code>max</code> duration in this
* case.
* <p>
*
*
* 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 runs console output clean and do not miss real
* error messages.
* <p>
*
*
* If the <code>occurrences</code> is set to <code>Integer.MAX_VALUE</code>,
* no tracking is done.
*/
@ -627,6 +642,7 @@ public class JavaTestKit {
} else
throw new IllegalArgumentException("unknown LogLevel " + clazz);
return filter.intercept(new AbstractFunction0<T>() {
@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}.