Fix state action tests

- Tests should now be more reliable as trying to
  make sure actions run before testing further.
- Fixes #257
This commit is contained in:
Janne Valkealahti
2016-09-25 11:20:20 +01:00
parent 49bae38eb3
commit 9c20ddd051
3 changed files with 8 additions and 5 deletions

View File

@@ -166,6 +166,7 @@ public abstract class AbstractStateMachineTests {
long sleep;
long now;
public CountDownLatch onExecuteStartLatch = new CountDownLatch(1);
public AtomicBoolean interrupted = new AtomicBoolean(false);
public CountDownLatch interruptedLatch = new CountDownLatch(1);
@@ -176,12 +177,12 @@ public abstract class AbstractStateMachineTests {
@Override
public void execute(StateContext<TestStates, TestEvents> context) {
onExecuteStartLatch.countDown();
now = System.currentTimeMillis();
if (sleep > 0) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("XXXXX " + e);
interrupted.set(true);
interruptedLatch.countDown();
}

View File

@@ -49,11 +49,11 @@ public class StateDoActivityActionTests extends AbstractStateMachineTests {
assertThat(machine, notNullValue());
machine.start();
machine.sendEvent(TestEvents.E1);
assertThat(testActionS1.onExecuteLatch.await(2, TimeUnit.SECONDS), is(true));
machine.sendEvent(TestEvents.E1);
machine.sendEvent(TestEvents.E2);
assertThat(testActionS2.onExecuteLatch.await(2, TimeUnit.SECONDS), is(true));
machine.sendEvent(TestEvents.E2);
}
@Test
@@ -68,10 +68,12 @@ public class StateDoActivityActionTests extends AbstractStateMachineTests {
assertThat(machine, notNullValue());
machine.start();
assertThat(testActionS1.onExecuteStartLatch.await(2, TimeUnit.SECONDS), is(true));
machine.sendEvent(TestEvents.E1);
assertThat(testActionS1.interruptedLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(testActionS1.onExecuteLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(testActionS2.onExecuteStartLatch.await(2, TimeUnit.SECONDS), is(true));
machine.sendEvent(TestEvents.E2);
assertThat(testActionS2.interruptedLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(testActionS2.onExecuteLatch.await(2, TimeUnit.SECONDS), is(true));

View File

@@ -671,11 +671,11 @@ public class UmlStateMachineModelFactoryTests extends AbstractUmlTests {
LatchAction e2Action = context.getBean("e2Action", LatchAction.class);
stateMachine.start();
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S1"));
stateMachine.sendEvent("E1");
assertThat(e1Action.latch.await(1, TimeUnit.SECONDS), is(true));
stateMachine.sendEvent("E1");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S2"));
stateMachine.sendEvent("E2");
assertThat(e2Action.latch.await(1, TimeUnit.SECONDS), is(true));
stateMachine.sendEvent("E2");
assertThat(stateMachine.getState().getIds(), containsInAnyOrder("S3"));
}