Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Spring StateMachine重启似乎缓存了最后一次执行_Spring Boot_Mockito_Spring Statemachine - Fatal编程技术网

Spring boot Spring StateMachine重启似乎缓存了最后一次执行

Spring boot Spring StateMachine重启似乎缓存了最后一次执行,spring-boot,mockito,spring-statemachine,Spring Boot,Mockito,Spring Statemachine,我已将Spring状态机配置为以下状态和转换: 空闲(初始)->已启动->预快照(预快照操作)->快照(快照操作)->空闲 以下是我的配置: @TestConfiguration @EnableStateMachineFactory public class StateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> { public static final String

我已将Spring状态机配置为以下状态和转换:

空闲(初始)->已启动->预快照(预快照操作)->快照(快照操作)->空闲

以下是我的配置:

@TestConfiguration
@EnableStateMachineFactory
public class StateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {

    public static final String IDLE = "IDLE";
    public static final String STARTED = "STARTED";
    public static final String PRE_SNAPSHOT = "PRE_SNAPSHOT";
    public static final String SNAPSHOT = "SNAPSHOT";
    public static final String START_EVENT = "START_EVENT";

    @Autowired
    private PreSnapshotActionMock preSnapshotActionMock;

    @Autowired
    private SnapshotActionMock snapshotActionMock;

    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
        config.withConfiguration().autoStartup(true);
    }

    @Override
    public void configure(StateMachineStateConfigurer<String, String> stateConfigurer) throws Exception {
        stateConfigurer
                .withStates()
                .initial(IDLE)
                .state(STARTED)
                .stateDo(PRE_SNAPSHOT, preSnapshotActionMock)
                .stateDo(SNAPSHOT, snapshotActionMock)
                .end(IDLE);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitionConfigurer) throws Exception {
        transitionConfigurer
                .withExternal().source(IDLE).target(STARTED).event(START_EVENT)
                .and()
                .withExternal().source(STARTED).target(PRE_SNAPSHOT)
                .and()
                .withExternal().source(PRE_SNAPSHOT).target(SNAPSHOT)
                .and()
                .withExternal().source(SNAPSHOT).target(IDLE);
    }

    @Bean
    public StateMachine<String, String> testStateMachine(@Autowired StateMachineFactory<String, String> smFactory) throws Exception {
        return smFactory.getStateMachine();
    }
}
第一个测试用例通过,因为状态机正在执行长时间的快照前操作,这是预期的。但是,第二个测试失败了,因为快照前的操作似乎执行得很慢。更有趣的是,如果我将test1中的睡眠时间从10_000更改为100,下面是两个测试的日志:

测试1:

pre-snapshot action from test1 #0
pre-snapshot action from test1 #1
pre-snapshot action from test1 #2
pre-snapshot action from test1 #3
测试2:

Sending start event now
pre-snapshot action from test1 #4
pre-snapshot action from test1 #5
pre-snapshot action from test1 #6
pre-snapshot action from test1 #7
pre-snapshot action from test1 #8
pre-snapshot action from test1 #9
snapshot action from test2 #0
看起来,即使我在@Before方法中配置了操作的默认行为,在test2中它根本不会执行,相反,快照前操作从test1中停止的位置继续工作,直到状态机转换到快照状态

如果我配置的状态机和测试用例与单元测试完全相同,那么下面是test2的日志:

Sending start event now
default pre-snapshot action short execution
snapshot action from test2 #0
pre-snapshot action from test1 #4
pre-snapshot action from test1 #5
pre-snapshot action from test1 #6
这显示了相反的行为(在我看来也是预期的):首先执行默认的快照前操作


你认为这可能是状态机中的一个bug,还是我遗漏了什么?我同意这可能是某种恢复机制,但如果是这样的话,它与嘲弄不太兼容。起初,我认为原因可能是Mockito的回答延迟了,但由于我用其他对象进行了测试,情况似乎并非如此

你没有这个代码或相关的部分,我可以自己检查和运行吗?另外,我也不确定当你将空闲定义为初始状态和终端状态时会发生什么,因为这是错误的。谢谢你的回复,Janne。我已经设法在我们项目的情况下解决了这个问题。我的目标是能够停止当前状态的操作,以防我想恢复到以前的某个状态。我通过调用sate.exit()并在操作本身中处理“InterruptedException”来实现这一点。这是常见的方法吗?你没有这个代码或相关的部分我可以自己检查和运行吗?另外,我也不确定当你将空闲定义为初始状态和终端状态时会发生什么,因为这是错误的。谢谢你的回复,Janne。我已经设法在我们项目的情况下解决了这个问题。我的目标是能够停止当前状态的操作,以防我想恢复到以前的某个状态。我通过调用sate.exit()并在操作本身中处理“InterruptedException”来实现这一点。这是常见的方式吗?
Sending start event now
default pre-snapshot action short execution
snapshot action from test2 #0
pre-snapshot action from test1 #4
pre-snapshot action from test1 #5
pre-snapshot action from test1 #6