|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.statemachine.action; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.springframework.statemachine.TestUtils.doSendEventsAndConsumeAllWithComplete; |
| 20 | +import static org.springframework.statemachine.TestUtils.doStartAndAssert; |
| 21 | +import static org.springframework.statemachine.TestUtils.resolveMachine; |
| 22 | + |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.concurrent.CountDownLatch; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.concurrent.atomic.AtomicLong; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.statemachine.AbstractStateMachineTests; |
| 33 | +import org.springframework.statemachine.StateContext; |
| 34 | +import org.springframework.statemachine.StateMachine; |
| 35 | +import org.springframework.statemachine.config.EnableStateMachine; |
| 36 | +import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; |
| 37 | +import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; |
| 38 | +import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; |
| 39 | + |
| 40 | +import reactor.core.publisher.Mono; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests for state machine reactive actions. |
| 44 | + * |
| 45 | + * @author Janne Valkealahti |
| 46 | + * |
| 47 | + */ |
| 48 | +public class ReactiveAction2Tests extends AbstractStateMachineTests { |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSimpleReactiveActions() throws Exception { |
| 52 | + context.register(Config1.class); |
| 53 | + context.refresh(); |
| 54 | + StateMachine<TestStates, TestEvents> machine = resolveMachine(context); |
| 55 | + doStartAndAssert(machine); |
| 56 | + |
| 57 | + TestCountAction testAction3 = context.getBean("testAction3", TestCountAction.class); |
| 58 | + TestCountAction testAction4 = context.getBean("testAction4", TestCountAction.class); |
| 59 | + doSendEventsAndConsumeAllWithComplete(machine, TestEvents.E1, TestEvents.E2); |
| 60 | + assertThat(testAction3.latch.await(6, TimeUnit.SECONDS)).isTrue(); |
| 61 | + assertThat(testAction4.latch.await(6, TimeUnit.SECONDS)).isTrue(); |
| 62 | + assertThat(testAction4.time.get() - testAction3.time.get()).isGreaterThan(1000); |
| 63 | + } |
| 64 | + |
| 65 | + @Configuration |
| 66 | + @EnableStateMachine |
| 67 | + static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> { |
| 68 | + |
| 69 | + @Override |
| 70 | + public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception { |
| 71 | + states |
| 72 | + .withStates() |
| 73 | + .initial(TestStates.S1) |
| 74 | + .stateEntryFunction(TestStates.S2, testAction3()) |
| 75 | + .stateEntryFunction(TestStates.S3, testAction4()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception { |
| 80 | + transitions |
| 81 | + .withExternal() |
| 82 | + .source(TestStates.S1) |
| 83 | + .target(TestStates.S2) |
| 84 | + .event(TestEvents.E1) |
| 85 | + .and() |
| 86 | + .withExternal() |
| 87 | + .source(TestStates.S2) |
| 88 | + .target(TestStates.S3) |
| 89 | + .event(TestEvents.E2); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public TestCountAction testAction3() { |
| 94 | + return new TestCountAction("ACTION3"); |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + public TestCountAction testAction4() { |
| 99 | + return new TestCountAction("ACTION4"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected AnnotationConfigApplicationContext buildContext() { |
| 105 | + return new AnnotationConfigApplicationContext(); |
| 106 | + } |
| 107 | + |
| 108 | + private static class TestCountAction implements ReactiveAction<TestStates, TestEvents> { |
| 109 | + |
| 110 | + private final String id; |
| 111 | + int count = 0; |
| 112 | + CountDownLatch latch = new CountDownLatch(1); |
| 113 | + AtomicLong time = new AtomicLong(); |
| 114 | + |
| 115 | + TestCountAction(String id) { |
| 116 | + this.id = id; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Mono<Void> apply(StateContext<TestStates, TestEvents> context) { |
| 121 | + return Mono.delay(Duration.ofMillis(2000)) |
| 122 | + .doFinally(x -> { |
| 123 | + count++; |
| 124 | + time.set(System.currentTimeMillis()); |
| 125 | + latch.countDown(); |
| 126 | + }) |
| 127 | + .then() |
| 128 | + .log(id); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments