Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop after resetting state machine #1151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

}
Loading