From e2aa943f16101d9f992365854a77c4640dc22878 Mon Sep 17 00:00:00 2001 From: Radmir Rakhmatullin Date: Sun, 17 Mar 2024 17:44:24 +0500 Subject: [PATCH] Guaranteed return of machine's currentStatus while running - Fixes #1011 --- .../support/AbstractStateMachine.java | 3 + .../support/ReactiveLifecycleManager.java | 2 +- .../statemachine/StateMachineResetTests.java | 55 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java index ec12d739b..919435052 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java @@ -183,6 +183,9 @@ public AbstractStateMachine(Collection> states, Collection getState() { + if (isRunning()) { + return currentState; + } // if we're complete assume we're stopped // and state was stashed into lastState State s = lastState; diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ReactiveLifecycleManager.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ReactiveLifecycleManager.java index 2ddc0f317..e0b2600fb 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ReactiveLifecycleManager.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/ReactiveLifecycleManager.java @@ -114,7 +114,7 @@ public LifecycleState getLifecycleState() { } public boolean isRunning() { - return state.get() == LifecycleState.STARTED; + return state.get() == LifecycleState.STARTED || state.get() == LifecycleState.STARTING; } @Override diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineResetTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineResetTests.java index e521ee652..e1b3f4376 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineResetTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/StateMachineResetTests.java @@ -343,6 +343,22 @@ public void testResetFunkyEnumTypes2() throws Exception { assertThat(machine.getState().getIds()).containsOnly(SuperState.INITIAL); } + @Test + public void testResetAndStartFromCustomState() { + context.register(Config8.class); + context.refresh(); + + StateMachine machine = resolveMachine(context); + + DefaultStateMachineContext stateMachineContext = new DefaultStateMachineContext( + States8.S8_2, null, null, null); + + machine.getStateMachineAccessor().doWithAllRegions(function -> function.resetStateMachineReactively(stateMachineContext).block()); + + doStartAndAssert(machine); + assertThat(machine.getState().getIds()).containsOnly(States8.S8_5); + } + @Configuration @EnableStateMachine static class Config1 extends EnumStateMachineConfigurerAdapter { @@ -825,4 +841,43 @@ public void configure(StateMachineTransitionConfigurer t } } + + @Configuration + @EnableStateMachine + static class Config8 extends EnumStateMachineConfigurerAdapter { + @Override + public void configure(StateMachineStateConfigurer states) + throws Exception { + states + .withStates() + .initial(States8.S8_1) + .state(States8.S8_2) + .state(States8.S8_3) + .state(States8.S8_4) + .end(States8.S8_5) + ; + } + + @Override + public void configure(final StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(States8.S8_1).target(States8.S8_2) + .and() + .withExternal() + .source(States8.S8_2).target(States8.S8_3) + .and() + .withExternal() + .source(States8.S8_3).target(States8.S8_4) + .and() + .withExternal() + .source(States8.S8_4).target(States8.S8_5) + ; + } + } + + public enum States8 implements MyState { + S8_1, S8_2, S8_3, S8_4, S8_5 + } + }