Fix internal transition for dist machine

- Change previous DistributedStateMachine fixes which didn't
  really work. Now explicitely getting state from machine to be set
  into ensemble for post internal transition.
- Now can send Message<E> via test plan instead of plain E.
- More testing for tweaked features.
This commit is contained in:
Janne Valkealahti
2015-08-08 10:43:54 +01:00
parent 7572a9ced3
commit 5347ea4dd9
5 changed files with 206 additions and 12 deletions

View File

@@ -127,6 +127,20 @@ public class StateMachineTestPlan<S, E> {
log.info("Sending test event " + step.sendEvent + " via machine " + machine);
machine.sendEvent(step.sendEvent);
}
} else if (step.sendMessage != null) {
ArrayList<StateMachine<S, E>> sendVia = new ArrayList<StateMachine<S, E>>();
if (step.sendEventMachineId != null) {
sendVia.add(stateMachines.get(step.sendEventMachineId));
} else if (step.sendEventToAll) {
sendVia.addAll(stateMachines.values());
} else {
sendVia.add(stateMachines.values().iterator().next());
}
assertThat("Error finding machine to send via", sendVia, not(empty()));
for (StateMachine<S, E> machine : sendVia) {
log.info("Sending test event " + step.sendEvent + " via machine " + machine);
machine.sendEvent(step.sendMessage);
}
}
if (step.expectStateChanged != null) {
@@ -212,7 +226,7 @@ public class StateMachineTestPlan<S, E> {
for (StateMachine<S, E> stateMachine : stateMachines.values()) {
Map<Object, Object> variables = stateMachine.getExtendedState().getVariables();
for (Entry<Object, Object> entry : step.expectVariables.entrySet()) {
assertThat("Key " + entry.getKey() + " doesn exist in extended state variables",
assertThat("Key " + entry.getKey() + " doesn't exist in extended state variables",
variables.containsKey(entry.getKey()), is(true));
assertThat("Variable " + entry.getKey() + " doesn't match in extended state variables",
variables.get(entry.getKey()), is(entry.getValue()));

View File

@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateMachine;
/**
@@ -110,6 +111,7 @@ public class StateMachineTestPlanBuilder<S, E> {
public class StateMachineTestPlanStepBuilder {
E sendEvent;
Message<E> sendMessage;
Object sendEventMachineId;
boolean sendEventToAll = false;
final Collection<S> expectStates = new ArrayList<S>();
@@ -189,6 +191,46 @@ public class StateMachineTestPlanBuilder<S, E> {
return this;
}
/**
* Send a message {@code Message<E>}. In case multiple state machines
* exists, a random one will be chosen to send this event.
*
* @param event the event
* @return the state machine test plan step builder
*/
public StateMachineTestPlanStepBuilder sendEvent(Message<E> event) {
return sendEvent(event, false);
}
/**
* Send a message {@code Message<E>}. If {@code sendToAll} is set to {@code TRUE} event
* will be send to all existing machines.
*
* @param event the event
* @param sendToAll send to all machines
* @return the state machine test plan step builder
*/
public StateMachineTestPlanStepBuilder sendEvent(Message<E> event, boolean sendToAll) {
this.sendMessage = event;
this.sendEventMachineId = null;
this.sendEventToAll = sendToAll;
return this;
}
/**
* Send a message {@code Message<E>} into a state machine identified
* by {@code machineId}.
*
* @param event the event
* @param machineId the machine identifier for sending event
* @return the state machine test plan step builder
*/
public StateMachineTestPlanStepBuilder sendEvent(Message<E> event, Object machineId) {
this.sendMessage = event;
this.sendEventMachineId = machineId;
return this;
}
/**
* Expect variable to exist in extended state variables.
*
@@ -346,8 +388,8 @@ public class StateMachineTestPlanBuilder<S, E> {
* @return the state machine test plan builder for chaining
*/
public StateMachineTestPlanBuilder<S, E> and() {
steps.add(new StateMachineTestPlanStep<S, E>(sendEvent, sendEventMachineId, sendEventToAll, expectStates,
expectStateChanged, expectStateEntered, expectStateExited, expectEventNotAccepted,
steps.add(new StateMachineTestPlanStep<S, E>(sendEvent, sendMessage, sendEventMachineId, sendEventToAll,
expectStates, expectStateChanged, expectStateEntered, expectStateExited, expectEventNotAccepted,
expectTransition, expectTransitionStarted, expectTransitionEnded, expectStateMachineStarted,
expectStateMachineStopped, expectVariableKeys, expectVariables));
return StateMachineTestPlanBuilder.this;
@@ -357,6 +399,7 @@ public class StateMachineTestPlanBuilder<S, E> {
static class StateMachineTestPlanStep<S, E> {
E sendEvent;
Message<E> sendMessage;
Object sendEventMachineId;
boolean sendEventToAll = false;
final Collection<S> expectStates;
@@ -372,13 +415,14 @@ public class StateMachineTestPlanBuilder<S, E> {
final Collection<Object> expectVariableKeys;
final Map<Object, Object> expectVariables;
public StateMachineTestPlanStep(E sendEvent, Object sendEventMachineId, boolean sendEventToAll,
Collection<S> expectStates, Integer expectStateChanged, Integer expectStateEntered,
Integer expectStateExited, Integer expectEventNotAccepted, Integer expectTransition,
Integer expectTransitionStarted, Integer expectTransitionEnded, Integer expectStateMachineStarted,
Integer expectStateMachineStopped, Collection<Object> expectVariableKeys,
Map<Object, Object> expectVariables) {
public StateMachineTestPlanStep(E sendEvent, Message<E> sendMessage, Object sendEventMachineId,
boolean sendEventToAll, Collection<S> expectStates, Integer expectStateChanged,
Integer expectStateEntered, Integer expectStateExited, Integer expectEventNotAccepted,
Integer expectTransition, Integer expectTransitionStarted, Integer expectTransitionEnded,
Integer expectStateMachineStarted, Integer expectStateMachineStopped,
Collection<Object> expectVariableKeys, Map<Object, Object> expectVariables) {
this.sendEvent = sendEvent;
this.sendMessage = sendMessage;
this.sendEventMachineId = sendEventMachineId;
this.sendEventToAll = sendEventToAll;
this.expectStates = expectStates;