Skip to content

Commit

Permalink
Guaranteed return of machine's currentStatus while running
Browse files Browse the repository at this point in the history
- Fixes #1011
  • Loading branch information
radmirr committed Mar 17, 2024
1 parent 4495c7d commit e2aa943
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ public AbstractStateMachine(Collection<State<S, E>> states, Collection<Transitio

@Override
public State<S,E> getState() {
if (isRunning()) {
return currentState;
}
// if we're complete assume we're stopped
// and state was stashed into lastState
State<S, E> s = lastState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyState, MyEvent> machine = resolveMachine(context);

DefaultStateMachineContext<MyState, MyEvent> stateMachineContext = new DefaultStateMachineContext<MyState, MyEvent>(
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<States, Events> {
Expand Down Expand Up @@ -825,4 +841,43 @@ public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> t
}

}

@Configuration
@EnableStateMachine
static class Config8 extends EnumStateMachineConfigurerAdapter<States8, MyEvent> {
@Override
public void configure(StateMachineStateConfigurer<States8, MyEvent> 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<States8, MyEvent> 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
}

}

0 comments on commit e2aa943

Please sign in to comment.