Fix machine autostart with annotation config
- Backport #141 fixes #142 - This seem to relate with machine build from an annotation config which is then used as a session scoped bean. - Fix autostart usage where appropriate and also fix some test which were using wrong logic to determine autostart usage(tests were wrong).
This commit is contained in:
@@ -149,6 +149,7 @@ public class StateMachineConfiguration<S, E> extends
|
||||
stateMachineFactory.setBeanFactory(getBeanFactory());
|
||||
stateMachineFactory.setContextEventsEnabled(contextEvents);
|
||||
stateMachineFactory.setBeanName(beanName);
|
||||
stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
|
||||
StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
|
||||
this.lifecycle = (SmartLifecycle) stateMachine;
|
||||
this.disposableBean = (DisposableBean) stateMachine;
|
||||
|
||||
@@ -70,6 +70,17 @@ public class SessionScopedAnnotationTests {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoStart() throws Exception {
|
||||
MockHttpSession session1 = new MockHttpSession();
|
||||
mvc.
|
||||
perform(get("/ping").session(session1)).
|
||||
andExpect(status().isOk());
|
||||
Object machine = session1.getAttribute("scopedTarget.stateMachine");
|
||||
assertThat(machine, notNullValue());
|
||||
assertThat(TestUtils.callMethod("isRunning", machine), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScopedMachines() throws Exception {
|
||||
MockHttpSession session1 = new MockHttpSession();
|
||||
@@ -171,6 +182,13 @@ public class SessionScopedAnnotationTests {
|
||||
@Autowired
|
||||
StateMachine<String, String> stateMachine;
|
||||
|
||||
@RequestMapping(path="/ping", method=RequestMethod.GET)
|
||||
public HttpEntity<Void> dummyPing() {
|
||||
// dummy ping to instantiate session and then create machine
|
||||
stateMachine.getState();
|
||||
return new ResponseEntity<Void>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(path="/state", method=RequestMethod.POST)
|
||||
public HttpEntity<Void> setState(@RequestParam("event") String event) {
|
||||
stateMachine.sendEvent(event);
|
||||
@@ -180,7 +198,6 @@ public class SessionScopedAnnotationTests {
|
||||
@RequestMapping(path="/state", method=RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public String getState() {
|
||||
stateMachine.start();
|
||||
return stateMachine.getState().getId();
|
||||
}
|
||||
|
||||
|
||||
@@ -114,6 +114,17 @@ public class SessionScopedManualTests {
|
||||
assertThat(TestUtils.readField("running", machine), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoStart() throws Exception {
|
||||
MockHttpSession session1 = new MockHttpSession();
|
||||
mvc.
|
||||
perform(get("/ping").session(session1)).
|
||||
andExpect(status().isOk());
|
||||
Object machine = session1.getAttribute("scopedTarget.stateMachine");
|
||||
assertThat(machine, notNullValue());
|
||||
assertThat(TestUtils.callMethod("isRunning", machine), is(true));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@@ -150,6 +161,13 @@ public class SessionScopedManualTests {
|
||||
@Autowired
|
||||
StateMachine<String, String> stateMachine;
|
||||
|
||||
@RequestMapping(path="/ping", method=RequestMethod.GET)
|
||||
public HttpEntity<Void> dummyPing() {
|
||||
// dummy ping to instantiate session and then create machine
|
||||
stateMachine.getState();
|
||||
return new ResponseEntity<Void>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(path="/state", method=RequestMethod.POST)
|
||||
public HttpEntity<Void> setState(@RequestParam("event") String event) {
|
||||
stateMachine.sendEvent(event);
|
||||
|
||||
@@ -191,10 +191,10 @@ public class ZookeeperStateMachineTests extends AbstractZookeeperTests {
|
||||
StateMachine<String, String> machine2 =
|
||||
context.getBean("sm2", StateMachine.class);
|
||||
|
||||
assertThat(((SmartLifecycle)machine1).isAutoStartup(), is(true));
|
||||
assertThat(((SmartLifecycle)machine1).isRunning(), is(true));
|
||||
assertThat(((SmartLifecycle)machine2).isAutoStartup(), is(true));
|
||||
assertThat(((SmartLifecycle)machine2).isRunning(), is(true));
|
||||
assertThat(((SmartLifecycle)machine1).isAutoStartup(), is(false));
|
||||
assertThat(((SmartLifecycle)machine1).isRunning(), is(false));
|
||||
assertThat(((SmartLifecycle)machine2).isAutoStartup(), is(false));
|
||||
assertThat(((SmartLifecycle)machine2).isRunning(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -387,10 +387,10 @@ public class ZookeeperStateMachineTests extends AbstractZookeeperTests {
|
||||
StateMachine<String, String> machine2 =
|
||||
buildTestStateMachine2(curatorClient);
|
||||
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("EV")
|
||||
.setHeader("testVariable", "x1")
|
||||
.build();
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("EV")
|
||||
.setHeader("testVariable", "x1")
|
||||
.build();
|
||||
|
||||
StateMachineTestPlan<String, String> plan =
|
||||
StateMachineTestPlanBuilder.<String, String>builder()
|
||||
@@ -423,10 +423,10 @@ public class ZookeeperStateMachineTests extends AbstractZookeeperTests {
|
||||
StateMachine<String, String> machine2 =
|
||||
buildTestStateMachine2(curatorClient);
|
||||
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("EV")
|
||||
.setHeader("testVariable", "x1")
|
||||
.build();
|
||||
Message<String> message = MessageBuilder
|
||||
.withPayload("EV")
|
||||
.setHeader("testVariable", "x1")
|
||||
.build();
|
||||
|
||||
StateMachineTestPlan<String, String> plan =
|
||||
StateMachineTestPlanBuilder.<String, String>builder()
|
||||
@@ -704,7 +704,10 @@ public class ZookeeperStateMachineTests extends AbstractZookeeperTests {
|
||||
.and()
|
||||
.withConfiguration()
|
||||
.listener(stateMachineListener())
|
||||
.autoStartup(true);
|
||||
// TODO: false, really? testStateChangesConfigSetup() will fail if true
|
||||
// maybe it's due to dist needs to be reseted!
|
||||
// previously setting it true, didn't actually enable autostart.
|
||||
.autoStartup(false);
|
||||
}
|
||||
|
||||
public abstract StateMachineEnsemble<String, String> stateMachineEnsemble() throws Exception;
|
||||
|
||||
Reference in New Issue
Block a user