From e28caf11640297dcf509a4838a98a13e2f842901 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 21 Jan 2016 08:45:17 +0000 Subject: [PATCH] Add test for timer transition --- .../trigger/TimerTriggerTests.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/trigger/TimerTriggerTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/trigger/TimerTriggerTests.java index e5bbfd46..4634dee8 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/trigger/TimerTriggerTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/trigger/TimerTriggerTests.java @@ -33,6 +33,7 @@ import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.action.Action; import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; +import org.springframework.statemachine.config.StateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import org.springframework.statemachine.listener.StateMachineListenerAdapter; @@ -92,6 +93,32 @@ public class TimerTriggerTests extends AbstractStateMachineTests { assertThat(action.count, greaterThan(90)); } + @SuppressWarnings("unchecked") + @Test + public void testTimerExternalTransitions() throws Exception { + context.register(Config3.class); + context.refresh(); + StateMachine machine = context.getBean(StateMachine.class); + TestTimerAction2 action = context.getBean("testTimerAction2", TestTimerAction2.class); + TestListener2 listener = new TestListener2(); + machine.addStateListener(listener); + + machine.start(); + assertThat(listener.stateMachineStartedLatch.await(2, TimeUnit.SECONDS), is(true)); + assertThat(machine.getState().getIds(), containsInAnyOrder("READY")); + + for (int i = 0; i < 4; i++) { + listener.reset(2); + action.reset(); + machine.sendEvent("SWITCH_TO_RUNNING"); + assertThat(action.latch.await(5, TimeUnit.SECONDS), is(true)); + assertThat(action.count, is(1)); + assertThat(listener.stateChangedLatch.await(5, TimeUnit.SECONDS), is(true)); + assertThat(listener.stateChangedCount, is(2)); + assertThat(machine.getState().getIds(), containsInAnyOrder("RUNNING_TESTING")); + } + } + static class Config1 { @Bean @@ -133,13 +160,84 @@ public class TimerTriggerTests extends AbstractStateMachineTests { } + @Configuration + @EnableStateMachine + static class Config3 extends StateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial("READY") + .state("RUNNING") + .state("RUNNING_TESTING") + .end("SHUTDOWN"); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source("READY") + .target("RUNNING") + .event("SWITCH_TO_RUNNING") + .and() + .withExternal() + .source("RUNNING") + .target("RUNNING_TESTING") + .timer(1000) + .action(testTimerAction2()) + .and() + .withExternal() + .source("RUNNING_TESTING") + .target("RUNNING") + .event("SWITCH_TO_RUNNING") + .and() + .withExternal() + .source("RUNNING") + .target("SHUTDOWN") + .event("SWITCH_TO_SHUTDOWN") + .and() + .withExternal() + .source("RUNNING_TESTING") + .target("SHUTDOWN") + .event("SWITCH_TO_SHUTDOWN"); + } + + @Bean + public TestTimerAction2 testTimerAction2() { + return new TestTimerAction2(); + } + + } + private static class TestTimerAction implements Action { int count = 0; + volatile CountDownLatch latch = new CountDownLatch(1); @Override public void execute(StateContext context) { count++; + latch.countDown(); + } + + } + + private static class TestTimerAction2 implements Action { + + int count = 0; + volatile CountDownLatch latch = new CountDownLatch(1); + + @Override + public void execute(StateContext context) { + count++; + latch.countDown(); + } + + public void reset() { + count = 0; + latch = new CountDownLatch(1); } } @@ -179,4 +277,39 @@ public class TimerTriggerTests extends AbstractStateMachineTests { } + private static class TestListener2 extends StateMachineListenerAdapter { + + volatile CountDownLatch stateMachineStartedLatch = new CountDownLatch(1); + volatile CountDownLatch stateChangedLatch = new CountDownLatch(1); + volatile CountDownLatch transitionLatch = new CountDownLatch(0); + volatile int stateChangedCount = 0; + + @Override + public void stateMachineStarted(StateMachine stateMachine) { + stateMachineStartedLatch.countDown(); + } + + @Override + public void stateChanged(State from, State to) { + stateChangedCount++; + stateChangedLatch.countDown(); + } + + @Override + public void transition(Transition transition) { + transitionLatch.countDown(); + } + + public void reset(int c1) { + reset(c1, 0); + } + + public void reset(int c1, int c2) { + stateChangedLatch = new CountDownLatch(c1); + transitionLatch = new CountDownLatch(c2); + stateChangedCount = 0; + } + + } + }