spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.53k stars 599 forks source link

Apparently wrong Callback to Mono bridge implementation #1007

Open sabomichal opened 2 years ago

sabomichal commented 2 years ago

org.springframework.statemachine.support.StateMachineExecutor.MonoSinkStateMachineExecutorCallback implementation seems to be wrong when trying to bridge callback API to Mono.

The callback bridge implementation should look similar to this one:

Callback<String> callback = new Callback<String>() {
    @Override
    public void onResult(String data) {
        sink.success(data.toLowerCase());
    }

    @Override
    public void onError(Exception e) {
        sink.error(e);
    }
}

However, the current implementation publishes event whenever there is a demand, regardless of the callback being actually called - please note the final else case:

@Override
public void accept(MonoSink<Void> t) {
    if (complete) {
        t.success();
    } else if (error != null) {
        t.error(error);
    } else {
        t.success();
    }
}

With the current implementation, there is no way to wait for the transitions to complete after sending an event via org.springframework.statemachine.StateMachineEventResult#complete Mono. The Mono completes immediately after subscription.