Tune tests, modify web sample, add more logging
- In web sample change other event C to K which brings machine back from S2 to S1. - Add more logging. - New test sending parallel events.
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -124,9 +125,13 @@ public class StateMachineTestPlan<S, E> {
|
||||
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.sendEvent);
|
||||
if (!step.sendEventParallel) {
|
||||
for (StateMachine<S, E> machine : sendVia) {
|
||||
log.info("Sending test event " + step.sendEvent + " via machine " + machine);
|
||||
machine.sendEvent(step.sendEvent);
|
||||
}
|
||||
} else {
|
||||
sendEventParallel(sendVia, step.sendEvent);
|
||||
}
|
||||
} else if (step.sendMessage != null) {
|
||||
ArrayList<StateMachine<S, E>> sendVia = new ArrayList<StateMachine<S, E>>();
|
||||
@@ -244,4 +249,40 @@ public class StateMachineTestPlan<S, E> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send event parallel to all machines.
|
||||
*
|
||||
* @param machines the machines
|
||||
* @param event the event
|
||||
*/
|
||||
private void sendEventParallel(final List<StateMachine<S, E>> machines, final E event) {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final ArrayList<Thread> joins = new ArrayList<Thread>();
|
||||
int threadCount = machines.size();
|
||||
for (int i = 0; i < threadCount; ++i) {
|
||||
final StateMachine<S,E> machine = machines.get(i);
|
||||
Runnable runner = new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
latch.await();
|
||||
machine.sendEvent(event);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread t = new Thread(runner, "EventSenderThread" + i);
|
||||
joins.add(t);
|
||||
t.start();
|
||||
}
|
||||
latch.countDown();
|
||||
for (Thread t : joins) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ public class StateMachineTestPlanBuilder<S, E> {
|
||||
Message<E> sendMessage;
|
||||
Object sendEventMachineId;
|
||||
boolean sendEventToAll = false;
|
||||
boolean sendEventParallel = false;
|
||||
final Collection<S> expectStates = new ArrayList<S>();
|
||||
Integer expectStateChanged;
|
||||
Integer expectStateEntered;
|
||||
@@ -172,9 +173,25 @@ public class StateMachineTestPlanBuilder<S, E> {
|
||||
* @return the state machine test plan step builder
|
||||
*/
|
||||
public StateMachineTestPlanStepBuilder sendEvent(E event, boolean sendToAll) {
|
||||
sendEvent(event, sendToAll, false);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event {@code E}. If {@code sendToAll} is set to {@code TRUE} event
|
||||
* will be send to all existing machines. If {@code sendPalallel} is set to
|
||||
* {@code TRUE} event to all machines will be send by parallel threads.
|
||||
*
|
||||
* @param event the event
|
||||
* @param sendToAll send to all machines
|
||||
* @param sendParallel send event parallel
|
||||
* @return the state machine test plan step builder
|
||||
*/
|
||||
public StateMachineTestPlanStepBuilder sendEvent(E event, boolean sendToAll, boolean sendParallel) {
|
||||
this.sendEvent = event;
|
||||
this.sendEventMachineId = null;
|
||||
this.sendEventToAll = sendToAll;
|
||||
this.sendEventParallel = sendParallel;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -405,9 +422,10 @@ public class StateMachineTestPlanBuilder<S, E> {
|
||||
*/
|
||||
public StateMachineTestPlanBuilder<S, E> and() {
|
||||
steps.add(new StateMachineTestPlanStep<S, E>(sendEvent, sendMessage, sendEventMachineId, sendEventToAll,
|
||||
expectStates, expectStateChanged, expectStateEntered, expectStateExited, expectEventNotAccepted,
|
||||
expectTransition, expectTransitionStarted, expectTransitionEnded, expectStateMachineStarted,
|
||||
expectStateMachineStopped, expectVariableKeys, expectVariables, expectExtendedStateChanged));
|
||||
sendEventParallel, expectStates, expectStateChanged, expectStateEntered, expectStateExited,
|
||||
expectEventNotAccepted, expectTransition, expectTransitionStarted, expectTransitionEnded,
|
||||
expectStateMachineStarted, expectStateMachineStopped, expectVariableKeys, expectVariables,
|
||||
expectExtendedStateChanged));
|
||||
return StateMachineTestPlanBuilder.this;
|
||||
}
|
||||
|
||||
@@ -418,6 +436,7 @@ public class StateMachineTestPlanBuilder<S, E> {
|
||||
Message<E> sendMessage;
|
||||
Object sendEventMachineId;
|
||||
boolean sendEventToAll = false;
|
||||
boolean sendEventParallel = false;
|
||||
final Collection<S> expectStates;
|
||||
Integer expectStateChanged;
|
||||
Integer expectStateEntered;
|
||||
@@ -433,15 +452,17 @@ public class StateMachineTestPlanBuilder<S, E> {
|
||||
final 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, Integer expectExtendedStateChanged) {
|
||||
boolean sendEventToAll, boolean sendEventParallel, 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,
|
||||
Integer expectExtendedStateChanged) {
|
||||
this.sendEvent = sendEvent;
|
||||
this.sendMessage = sendMessage;
|
||||
this.sendEventMachineId = sendEventMachineId;
|
||||
this.sendEventToAll = sendEventToAll;
|
||||
this.sendEventParallel = sendEventParallel;
|
||||
this.expectStates = expectStates;
|
||||
this.expectStateChanged = expectStateChanged;
|
||||
this.expectStateEntered = expectStateEntered;
|
||||
|
||||
Reference in New Issue
Block a user