Add extended state variable change into test plan

- Add missing test method for testing variable change events.
- This may also contribute to make tests more reliable when
  testing variable values.
- Should contribute to #76 also.
This commit is contained in:
Janne Valkealahti
2015-08-14 09:33:54 +01:00
parent dea718c13d
commit f12b94e548
4 changed files with 62 additions and 4 deletions

View File

@@ -101,7 +101,8 @@ public class StateMachineTestPlan<S, E> {
step.expectTransitionStarted != null ? step.expectTransitionStarted : 0,
step.expectTransitionEnded != null ? step.expectTransitionEnded : 0,
step.expectStateMachineStarted != null ? step.expectStateMachineStarted : 0,
step.expectStateMachineStopped != null ? step.expectStateMachineStopped : 0);
step.expectStateMachineStopped != null ? step.expectStateMachineStopped : 0,
step.expectExtendedStateChanged != null ? step.expectExtendedStateChanged : 0);
}
if (step.expectStateMachineStarted != null) {
@@ -212,6 +213,13 @@ public class StateMachineTestPlan<S, E> {
}
}
if (step.expectExtendedStateChanged != null) {
for (LatchStateMachineListener<S, E> listener : listeners.values()) {
assertThat(listener.getExtendedStateChangedLatch().await(defaultAwaitTime, TimeUnit.SECONDS), is(true));
assertThat(listener.getExtendedStateChanged().size(), is(step.expectExtendedStateChanged));
}
}
if (!step.expectVariableKeys.isEmpty()) {
for (StateMachine<S, E> stateMachine : stateMachines.values()) {
Map<Object, Object> variables = stateMachine.getExtendedState().getVariables();

View File

@@ -124,6 +124,7 @@ public class StateMachineTestPlanBuilder<S, E> {
Integer expectTransitionEnded;
Integer expectStateMachineStarted;
Integer expectStateMachineStopped;
Integer expectExtendedStateChanged;
final Collection<Object> expectVariableKeys = new ArrayList<Object>();
final Map<Object, Object> expectVariables = new HashMap<Object, Object>();
@@ -381,6 +382,21 @@ public class StateMachineTestPlanBuilder<S, E> {
return this;
}
/**
* Expect state machine extended state variables changing {@code count} times.
*
* @param count the count
* @return the state machine test plan step builder
*/
public StateMachineTestPlanStepBuilder expectExtendedStateChanged(int count) {
if (count < 0) {
throw new IllegalArgumentException("Expected count cannot be negative, was " + count);
}
this.expectExtendedStateChanged = count;
return this;
}
/**
* Add a new step and return {@link StateMachineTestPlanBuilder}
* for chaining.
@@ -391,7 +407,7 @@ public class StateMachineTestPlanBuilder<S, E> {
steps.add(new StateMachineTestPlanStep<S, E>(sendEvent, sendMessage, sendEventMachineId, sendEventToAll,
expectStates, expectStateChanged, expectStateEntered, expectStateExited, expectEventNotAccepted,
expectTransition, expectTransitionStarted, expectTransitionEnded, expectStateMachineStarted,
expectStateMachineStopped, expectVariableKeys, expectVariables));
expectStateMachineStopped, expectVariableKeys, expectVariables, expectExtendedStateChanged));
return StateMachineTestPlanBuilder.this;
}
@@ -412,6 +428,7 @@ public class StateMachineTestPlanBuilder<S, E> {
Integer expectTransitionEnded;
Integer expectStateMachineStarted;
Integer expectStateMachineStopped;
Integer expectExtendedStateChanged;
final Collection<Object> expectVariableKeys;
final Map<Object, Object> expectVariables;
@@ -420,7 +437,7 @@ public class StateMachineTestPlanBuilder<S, E> {
Integer expectStateEntered, Integer expectStateExited, Integer expectEventNotAccepted,
Integer expectTransition, Integer expectTransitionStarted, Integer expectTransitionEnded,
Integer expectStateMachineStarted, Integer expectStateMachineStopped,
Collection<Object> expectVariableKeys, Map<Object, Object> expectVariables) {
Collection<Object> expectVariableKeys, Map<Object, Object> expectVariables, Integer expectExtendedStateChanged) {
this.sendEvent = sendEvent;
this.sendMessage = sendMessage;
this.sendEventMachineId = sendEventMachineId;
@@ -437,6 +454,7 @@ public class StateMachineTestPlanBuilder<S, E> {
this.expectStateMachineStopped = expectStateMachineStopped;
this.expectVariableKeys = expectVariableKeys;
this.expectVariables = expectVariables;
this.expectExtendedStateChanged = expectExtendedStateChanged;
}
}

View File

@@ -48,6 +48,7 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
private volatile CountDownLatch transitionEndedLatch = new CountDownLatch(1);
private volatile CountDownLatch stateMachineStartedLatch = new CountDownLatch(1);
private volatile CountDownLatch stateMachineStoppedLatch = new CountDownLatch(1);
private volatile CountDownLatch extendedStateChangedLatch = new CountDownLatch(1);
private final List<StateChangedWrapper<S, E>> stateChanged = new ArrayList<StateChangedWrapper<S, E>>();
private final List<State<S, E>> stateEntered = new ArrayList<State<S, E>>();
@@ -58,6 +59,7 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
private final List<Transition<S, E>> transitionEnded = new ArrayList<Transition<S, E>>();
private final List<StateMachine<S, E>> stateMachineStarted = new ArrayList<StateMachine<S, E>>();
private final List<StateMachine<S, E>> stateMachineStopped = new ArrayList<StateMachine<S, E>>();
private final List<ExtendedStateChangedWrapper> extendedStateChanged = new ArrayList<ExtendedStateChangedWrapper>();
@Override
public void stateChanged(State<S, E> from, State<S, E> to) {
@@ -131,9 +133,17 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
}
}
@Override
public void extendedStateChanged(Object key, Object value) {
synchronized (lock) {
this.extendedStateChanged.add(new ExtendedStateChangedWrapper(key, value));
this.extendedStateChangedLatch.countDown();
}
}
public void reset(int stateChangedCount, int stateEnteredCount, int stateExitedCount, int eventNotAcceptedCount,
int transitionCount, int transitionStartedCount, int transitionEndedCount, int stateMachineStartedCount,
int stateMachineStoppedCount) {
int stateMachineStoppedCount, int extendedStateChangedCount) {
synchronized (lock) {
this.stateChangedLatch = new CountDownLatch(stateChangedCount);
this.stateEnteredLatch = new CountDownLatch(stateEnteredCount);
@@ -144,6 +154,7 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
this.transitionEndedLatch = new CountDownLatch(transitionEndedCount);
this.stateMachineStartedLatch = new CountDownLatch(stateMachineStartedCount);
this.stateMachineStoppedLatch = new CountDownLatch(stateMachineStoppedCount);
this.extendedStateChangedLatch = new CountDownLatch(extendedStateChangedCount);
this.stateChanged.clear();
this.stateEntered.clear();
this.stateExited.clear();
@@ -153,6 +164,7 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
this.transitionEnded.clear();
this.stateMachineStarted.clear();
this.stateMachineStopped.clear();
this.extendedStateChanged.clear();
}
}
@@ -192,6 +204,10 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
return stateMachineStoppedLatch;
}
public CountDownLatch getExtendedStateChangedLatch() {
return extendedStateChangedLatch;
}
public List<StateChangedWrapper<S, E>> getStateChanged() {
return stateChanged;
}
@@ -228,6 +244,10 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
return stateMachineStopped;
}
public List<ExtendedStateChangedWrapper> getExtendedStateChanged() {
return extendedStateChanged;
}
public static class StateChangedWrapper<S, E> {
final State<S, E> from;
final State<S, E> to;
@@ -239,4 +259,15 @@ public class LatchStateMachineListener<S, E> extends StateMachineListenerAdapter
}
public static class ExtendedStateChangedWrapper {
final Object key;
final Object value;
public ExtendedStateChangedWrapper(Object key, Object value) {
this.key = key;
this.value = value;
}
}
}

View File

@@ -374,6 +374,7 @@ public class ZookeeperStateMachineTests extends AbstractZookeeperTests {
.step()
.sendEvent(message, machine1)
.expectTransition(1)
.expectExtendedStateChanged(1)
.expectVariable("testVariable", "x1")
.and()
.build();