* and add remainingOrDefault in Java testkit
This commit is contained in:
parent
385ea37497
commit
679db55eca
3 changed files with 47 additions and 31 deletions
|
|
@ -126,7 +126,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite {
|
||||||
PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class);
|
PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class);
|
||||||
assertTransition(stateTransition, fsmRef, UserState.LOOKING_AROUND, UserState.SHOPPING);
|
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
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class);
|
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
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
expectTerminated(fsmRef);
|
expectTerminated(fsmRef);
|
||||||
|
|
@ -312,7 +312,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite {
|
||||||
currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class);
|
currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class);
|
||||||
assertEquals(currentState.state(), UserState.SHOPPING);
|
assertEquals(currentState.state(), UserState.SHOPPING);
|
||||||
|
|
||||||
new Within(duration("0.9 seconds"), duration("1.9 seconds")) {
|
new Within(duration("0.9 seconds"), remainingOrDefault()) {
|
||||||
@Override
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class);
|
PersistentFSM.Transition stateTransition = expectMsgClass(PersistentFSM.Transition.class);
|
||||||
|
|
@ -331,7 +331,7 @@ public class AbstractPersistentFSMTest extends JUnitSuite {
|
||||||
currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class);
|
currentState = expectMsgClass(akka.persistence.fsm.PersistentFSM.CurrentState.class);
|
||||||
assertEquals(currentState.state(), UserState.INACTIVE);
|
assertEquals(currentState.state(), UserState.INACTIVE);
|
||||||
|
|
||||||
new Within(duration("1.9 seconds"), duration("2.9 seconds")) {
|
new Within(duration("1.9 seconds"), remainingOrDefault()) {
|
||||||
@Override
|
@Override
|
||||||
protected void run() {
|
protected void run() {
|
||||||
expectTerminated(recoveredFsmRef2);
|
expectTerminated(recoveredFsmRef2);
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ abstract class PersistentFSMSpec(config: Config) extends PersistenceSpec(config)
|
||||||
expectMsg(CurrentState(fsmRef, LookingAround, None))
|
expectMsg(CurrentState(fsmRef, LookingAround, None))
|
||||||
expectMsg(Transition(fsmRef, LookingAround, Shopping, Some(1 second)))
|
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)))
|
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)))
|
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)))
|
expectMsg(Transition(recoveredFsmRef, Shopping, Inactive, Some(2 seconds)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,24 +26,24 @@ import java.util.concurrent.TimeUnit;
|
||||||
* bounds concerning timing are available in the form of <code>Within</code>
|
* bounds concerning timing are available in the form of <code>Within</code>
|
||||||
* blocks.
|
* blocks.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* Beware of two points:
|
* Beware of two points:
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>the ActorSystem passed into the constructor needs to be shutdown,
|
* <li>the ActorSystem passed into the constructor needs to be shutdown,
|
||||||
* otherwise thread pools and memory will be leaked - this trait is not
|
* 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>
|
* 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
|
* blocks); take care not to run tests within a single test class instance in
|
||||||
* parallel.</li>
|
* parallel.</li>
|
||||||
*
|
*
|
||||||
* <li>It should be noted that for CI servers and the like all maximum Durations
|
* <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
|
* are scaled using the <code>dilated</code> method, which uses the
|
||||||
* TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry
|
* TestKitExtension.Settings.TestTimeFactor settable via akka.conf entry
|
||||||
* "akka.test.timefactor".</li>
|
* "akka.test.timefactor".</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class JavaTestKit {
|
public class JavaTestKit {
|
||||||
/**
|
/**
|
||||||
|
|
@ -203,6 +203,16 @@ public class JavaTestKit {
|
||||||
return p.remainingOr(duration);
|
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
|
* Execute code block while bounding its execution time between
|
||||||
* <code>min</code> and <code>max</code>. <code>Within</code> blocks may be
|
* <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
|
* available in a version which implicitly uses the remaining time governed by
|
||||||
* the innermost enclosing <code>Within</code> block.
|
* the innermost enclosing <code>Within</code> block.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* Note that the timeout is scaled using <code>dilated</code>, which uses the
|
* Note that the timeout is scaled using <code>dilated</code>, which uses the
|
||||||
* configuration entry "akka.test.timefactor", while the min Duration is not.
|
* configuration entry "akka.test.timefactor", while the min Duration is not.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* <code>
|
* <code>
|
||||||
* // the run() method needs to finish within 3 seconds
|
* // the run() method needs to finish within 3 seconds
|
||||||
|
|
@ -231,6 +241,7 @@ public class JavaTestKit {
|
||||||
|
|
||||||
public Within(FiniteDuration max) {
|
public Within(FiniteDuration max) {
|
||||||
p.within(max, new AbstractFunction0<Object>() {
|
p.within(max, new AbstractFunction0<Object>() {
|
||||||
|
@Override
|
||||||
public Object apply() {
|
public Object apply() {
|
||||||
run();
|
run();
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -240,6 +251,7 @@ public class JavaTestKit {
|
||||||
|
|
||||||
public Within(FiniteDuration min, FiniteDuration max) {
|
public Within(FiniteDuration min, FiniteDuration max) {
|
||||||
p.within(min, max, new AbstractFunction0<Object>() {
|
p.within(min, max, new AbstractFunction0<Object>() {
|
||||||
|
@Override
|
||||||
public Object apply() {
|
public Object apply() {
|
||||||
run();
|
run();
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -252,11 +264,11 @@ public class JavaTestKit {
|
||||||
* Await until the given condition evaluates to <code>true</code> or the
|
* Await until the given condition evaluates to <code>true</code> or the
|
||||||
* timeout expires, whichever comes first.
|
* timeout expires, whichever comes first.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* If no timeout is given, take it from the innermost enclosing
|
* If no timeout is given, take it from the innermost enclosing
|
||||||
* <code>Within</code> block.
|
* <code>Within</code> block.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* Note that the timeout is scaled using Duration.dilated, which uses the
|
* Note that the timeout is scaled using Duration.dilated, which uses the
|
||||||
* configuration entry "akka.test.timefactor".
|
* configuration entry "akka.test.timefactor".
|
||||||
*/
|
*/
|
||||||
|
|
@ -273,6 +285,7 @@ public class JavaTestKit {
|
||||||
|
|
||||||
public AwaitCond(Duration max, Duration interval) {
|
public AwaitCond(Duration max, Duration interval) {
|
||||||
p.awaitCond(new AbstractFunction0<Object>() {
|
p.awaitCond(new AbstractFunction0<Object>() {
|
||||||
|
@Override
|
||||||
public Object apply() {
|
public Object apply() {
|
||||||
return cond();
|
return cond();
|
||||||
}
|
}
|
||||||
|
|
@ -281,6 +294,7 @@ public class JavaTestKit {
|
||||||
|
|
||||||
public AwaitCond(Duration max, Duration interval, String message) {
|
public AwaitCond(Duration max, Duration interval, String message) {
|
||||||
p.awaitCond(new AbstractFunction0<Object>() {
|
p.awaitCond(new AbstractFunction0<Object>() {
|
||||||
|
@Override
|
||||||
public Object apply() {
|
public Object apply() {
|
||||||
return cond();
|
return cond();
|
||||||
}
|
}
|
||||||
|
|
@ -293,11 +307,11 @@ public class JavaTestKit {
|
||||||
* expires, whichever comes first. If the timeout expires the last exception
|
* expires, whichever comes first. If the timeout expires the last exception
|
||||||
* is thrown.
|
* is thrown.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* If no timeout is given, take it from the innermost enclosing
|
* If no timeout is given, take it from the innermost enclosing
|
||||||
* <code>Within</code> block.
|
* <code>Within</code> block.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* Note that the timeout is scaled using Duration.dilated, which uses the
|
* Note that the timeout is scaled using Duration.dilated, which uses the
|
||||||
* configuration entry "akka.test.timefactor".
|
* configuration entry "akka.test.timefactor".
|
||||||
*/
|
*/
|
||||||
|
|
@ -314,6 +328,7 @@ public class JavaTestKit {
|
||||||
|
|
||||||
public AwaitAssert(Duration max, Duration interval) {
|
public AwaitAssert(Duration max, Duration interval) {
|
||||||
p.awaitAssert(new AbstractFunction0<Object>() {
|
p.awaitAssert(new AbstractFunction0<Object>() {
|
||||||
|
@Override
|
||||||
public Object apply() {
|
public Object apply() {
|
||||||
check();
|
check();
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -329,10 +344,10 @@ public class JavaTestKit {
|
||||||
* <p>
|
* <p>
|
||||||
* The received object as transformed by the matching function can be
|
* The received object as transformed by the matching function can be
|
||||||
* retrieved with the <code>get</code> method.
|
* retrieved with the <code>get</code> method.
|
||||||
*
|
*
|
||||||
* Use this variant to implement more complicated or conditional processing.
|
* Use this variant to implement more complicated or conditional processing.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* <code>
|
* <code>
|
||||||
* final String out = new ExpectMsg<String>("match hint") {
|
* 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
|
* 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
|
* object. Wait time is bounded by the given duration, with an
|
||||||
* AssertionFailure being thrown in case of timeout.
|
* AssertionFailure being thrown in case of timeout.
|
||||||
*
|
*
|
||||||
* @return the received object
|
* @return the received object
|
||||||
*/
|
*/
|
||||||
public <T> T expectMsgEquals(FiniteDuration max, T msg) {
|
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
|
* 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
|
* given class. Wait time is bounded by the given duration, with an
|
||||||
* AssertionFailure being thrown in case of timeout.
|
* AssertionFailure being thrown in case of timeout.
|
||||||
*
|
*
|
||||||
* @return the received object
|
* @return the received object
|
||||||
*/
|
*/
|
||||||
public <T> T expectMsgClass(FiniteDuration max, Class<T> clazz) {
|
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
|
* 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
|
* the given objects. Wait time is bounded by the given duration, with an
|
||||||
* AssertionFailure being thrown in case of timeout.
|
* AssertionFailure being thrown in case of timeout.
|
||||||
*
|
*
|
||||||
* @return the received object
|
* @return the received object
|
||||||
*/
|
*/
|
||||||
public Object expectMsgAnyOf(FiniteDuration max, Object... msgs) {
|
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
|
* 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
|
* of the given classes. Wait time is bounded by the given duration, with an
|
||||||
* AssertionFailure being thrown in case of timeout.
|
* AssertionFailure being thrown in case of timeout.
|
||||||
*
|
*
|
||||||
* @return the received object
|
* @return the received object
|
||||||
*/
|
*/
|
||||||
public Object expectMsgAnyClassOf(FiniteDuration max, Class<?>... classes) {
|
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
|
* Receive one message from the internal queue of the TestActor. If the given
|
||||||
* duration is zero, the queue is polled (non-blocking).
|
* duration is zero, the queue is polled (non-blocking).
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* This method does NOT automatically scale its Duration parameter!
|
* This method does NOT automatically scale its Duration parameter!
|
||||||
*/
|
*/
|
||||||
public Object receiveOne(Duration max) {
|
public Object receiveOne(Duration max) {
|
||||||
|
|
@ -536,11 +551,11 @@ public class JavaTestKit {
|
||||||
* default) or the overall maximum duration is elapsed. Returns the sequence
|
* default) or the overall maximum duration is elapsed. Returns the sequence
|
||||||
* of messages.
|
* of messages.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* Note that it is not an error to hit the <code>max</code> duration in this
|
* Note that it is not an error to hit the <code>max</code> duration in this
|
||||||
* case.
|
* case.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* One possible use of this method is for testing whether messages of certain
|
* One possible use of this method is for testing whether messages of certain
|
||||||
* characteristics are generated at a certain rate.
|
* 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
|
* that you can keep your test run’s console output clean and do not miss real
|
||||||
* error messages.
|
* error messages.
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
* If the <code>occurrences</code> is set to <code>Integer.MAX_VALUE</code>,
|
* If the <code>occurrences</code> is set to <code>Integer.MAX_VALUE</code>,
|
||||||
* no tracking is done.
|
* no tracking is done.
|
||||||
*/
|
*/
|
||||||
|
|
@ -627,6 +642,7 @@ public class JavaTestKit {
|
||||||
} else
|
} else
|
||||||
throw new IllegalArgumentException("unknown LogLevel " + clazz);
|
throw new IllegalArgumentException("unknown LogLevel " + clazz);
|
||||||
return filter.intercept(new AbstractFunction0<T>() {
|
return filter.intercept(new AbstractFunction0<T>() {
|
||||||
|
@Override
|
||||||
public T apply() {
|
public T apply() {
|
||||||
return run();
|
return run();
|
||||||
}
|
}
|
||||||
|
|
@ -687,7 +703,7 @@ public class JavaTestKit {
|
||||||
public void shutdown(ActorSystem actorSystem, Boolean verifySystemShutdown) {
|
public void shutdown(ActorSystem actorSystem, Boolean verifySystemShutdown) {
|
||||||
shutdown(actorSystem, null, verifySystemShutdown);
|
shutdown(actorSystem, null, verifySystemShutdown);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
||||||
* @param props Props to create the child actor
|
* @param props Props to create the child actor
|
||||||
|
|
@ -697,7 +713,7 @@ public class JavaTestKit {
|
||||||
public ActorRef childActorOf(Props props, String name, SupervisorStrategy supervisorStrategy) {
|
public ActorRef childActorOf(Props props, String name, SupervisorStrategy supervisorStrategy) {
|
||||||
return p.childActorOf(props, name, supervisorStrategy);
|
return p.childActorOf(props, name, supervisorStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
||||||
* The actor will have an auto-generated name.
|
* The actor will have an auto-generated name.
|
||||||
|
|
@ -707,7 +723,7 @@ public class JavaTestKit {
|
||||||
public ActorRef childActorOf(Props props, SupervisorStrategy supervisorStrategy) {
|
public ActorRef childActorOf(Props props, SupervisorStrategy supervisorStrategy) {
|
||||||
return p.childActorOf(props, supervisorStrategy);
|
return p.childActorOf(props, supervisorStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
* 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}.
|
* The actor will be supervised using {@link SupervisorStrategy.stoppingStrategy}.
|
||||||
|
|
@ -717,7 +733,7 @@ public class JavaTestKit {
|
||||||
public ActorRef childActorOf(Props props, String name) {
|
public ActorRef childActorOf(Props props, String name) {
|
||||||
return p.childActorOf(props, name);
|
return p.childActorOf(props, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns an actor as a child of this test actor, and returns the child's ActorRef.
|
* 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}.
|
* The actor will have an auto-generated name and will be supervised using {@link SupervisorStrategy.stoppingStrategy}.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue