Skip to content

PostStateChange interceptors are not given the correct state #353

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

Closed
heffebaycay opened this issue Apr 26, 2017 · 3 comments
Closed

PostStateChange interceptors are not given the correct state #353

heffebaycay opened this issue Apr 26, 2017 · 3 comments
Labels
type/bug Is a bug report
Milestone

Comments

@heffebaycay
Copy link
Contributor

heffebaycay commented Apr 26, 2017

Let's say I have a state machine with the following structure :

Initial State --> Choice "Choice_State" :
                             (guard) --> State "One"
                             (else)  --> State "Default"

And I configure an Interceptor that looks just like this:

public class CustomInterceptor extends StateMachineInterceptorAdapter<CustomState, CustomEvent> {

    @Override
    public void preStateChange(State<Custom State, CustomEvent> state, Message<CustomEvent> message, Transition<Custom State, CustomEvent> transition, StateMachine<Custom State, CustomEvent> stateMachine) {
        System.out.printf("Interceptor - preState : %s\n", state.getId());
        super.preStateChange(state, message, transition, stateMachine);
    }

    @Override
    public void postStateChange(State<Custom State, CustomEvent> state, Message<CustomEvent> message, Transition<Custom State, CustomEvent> transition, StateMachine<Custom State, CustomEvent> stateMachine) {
        System.out.printf("Interceptor - postState : %s\n", state.getId());
        super.postStateChange(state, message, transition, stateMachine);
    }

Depending on what the guard function returns, the following paths are possible :

  1. if guard returns true : Initial State --> "Choice_State" -> "One"

The interceptor prints the following info:

Interceptor - preState One
Interceptor - postState Choice_State

  1. if guard returns false : Initial State --> "Choice_State" -> "Default"

The interceptor prints the following info:

Interceptor - preState Default
Interceptor - postState Choice_State

In both cases, I expected the postStateChangeInterceptor to show the same state as the preState interceptor.

I believe the culprit is the following call in AbstractStateMachine::switchToState :

callPostStateChangeInterceptors(state, message, transition, stateMachine);

When it should be

callPostStateChangeInterceptors(toState, message, transition, stateMachine);

The toState variable holds a reference to the actual state the state machine will transition to (after pseudo states transitions are followed), whereas the state variable holds a reference to the destination state before following pseudo states transitions.

@jvalkeal
Copy link
Contributor

jvalkeal commented May 3, 2017

Hmm, for quick look you may be right about this. There's been no real use of post stage internally so this may have slipped from any existing tests. I'll try to poke around more to create some new tests.

@jvalkeal jvalkeal added the type/bug Is a bug report label May 3, 2017
@jvalkeal jvalkeal added this to the 1.2.4.RELEASE milestone May 3, 2017
jvalkeal added a commit to jvalkeal/spring-statemachine that referenced this issue May 3, 2017
- Now calling post state interceptor with
  correct/same state as pre.
- Fixes spring-projects#353
@jvalkeal
Copy link
Contributor

jvalkeal commented May 3, 2017

Fixed per 37642b1

@heffebaycay
Copy link
Contributor Author

@jvalkeal Perfect, thanks for the fix :)

jvalkeal added a commit to jvalkeal/spring-statemachine that referenced this issue May 5, 2017
- Now calling post state interceptor with
  correct/same state as pre.
- Backport spring-projects#353
- Relates spring-projects#307
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Is a bug report
Projects
None yet
Development

No branches or pull requests

2 participants