StateMachineTestPlan starts machine too early

- Delaying starting statemachine inside step and
  after listeners has been reset.
- Fixes #104
This commit is contained in:
Janne Valkealahti
2015-09-04 08:28:22 +01:00
parent f33fa4055c
commit 5dc813ab3f
2 changed files with 70 additions and 1 deletions

View File

@@ -84,7 +84,6 @@ public class StateMachineTestPlan<S, E> {
LatchStateMachineListener<S, E> listener = new LatchStateMachineListener<S, E>();
listeners.put(stateMachine, listener);
stateMachine.addStateListener(listener);
stateMachine.start();
}
log.info("Running test plan for machines "
+ StringUtils.collectionToCommaDelimitedString(stateMachines.values()));
@@ -106,6 +105,11 @@ public class StateMachineTestPlan<S, E> {
step.expectExtendedStateChanged != null ? step.expectExtendedStateChanged : 0);
}
// need to call start here, ok to call from all steps
for (StateMachine<S, E> stateMachine : stateMachines.values()) {
stateMachine.start();
}
if (step.expectStateMachineStarted != null) {
for (Entry<StateMachine<S, E>, LatchStateMachineListener<S, E>> entry : listeners.entrySet()) {
assertThat("StateMachineStarted Await not matched for machine " + entry.getKey(), entry.getValue()

View File

@@ -17,10 +17,14 @@ package org.springframework.statemachine.test;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
@@ -43,6 +47,24 @@ public class StateMachineTestingTests extends AbstractStateMachineTests {
plan.test();
}
@SuppressWarnings("unchecked")
@Test
public void testStarted() throws Exception {
registerAndRefresh(Config2.class);
StateMachine<String, String> machine = context.getBean(StateMachine.class);
StateMachineTestPlan<String, String> plan =
StateMachineTestPlanBuilder.<String, String>builder()
.stateMachine(machine)
.step().expectStateMachineStarted(1).and()
.step().expectState("SI").and()
.step().sendEvent("E1").expectStateChanged(1).expectState("S1").and()
.step().sendEvent("E2").expectStateChanged(1).expectState("S2").and()
.build();
plan.test();
}
@Override
protected AnnotationConfigApplicationContext buildContext() {
return new AnnotationConfigApplicationContext();
@@ -77,4 +99,47 @@ public class StateMachineTestingTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
static class Config2 extends StateMachineConfigurerAdapter<String, String> {
@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
config
.withConfiguration()
.taskExecutor(taskExecutor());
}
@Override
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
states
.withStates()
.initial("SI")
.state("S1")
.state("S2");
}
@Override
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
transitions
.withExternal()
.source("SI")
.target("S1")
.event("E1")
.and()
.withExternal()
.source("S1")
.target("S2")
.event("E2");
}
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(1);
return taskExecutor;
}
}
}