Reactive test changes for core

- Relates #744
This commit is contained in:
Janne Valkealahti
2019-05-11 13:23:23 +01:00
parent be6ee233c5
commit 175175419a
5 changed files with 43 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,9 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;
import java.util.concurrent.TimeUnit;
@@ -26,12 +29,15 @@ import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import reactor.core.publisher.Mono;
public class RelayTests extends AbstractStateMachineTests {
@Override
@@ -40,20 +46,17 @@ public class RelayTests extends AbstractStateMachineTests {
}
@Test
@SuppressWarnings("unchecked")
public void testRelayFromSubmachine() throws Exception {
context.register(Config1.class);
context.refresh();
ObjectStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
StateMachine<TestStates, TestEvents> machine = resolveMachine(context);
assertThat(machine, notNullValue());
TestStateMachineListener listener = new TestStateMachineListener();
machine.addStateListener(listener);
machine.start();
doStartAndAssert(machine);
listener.reset(3, 0);
machine.sendEvent(TestEvents.E1);
doSendEventAndConsumeAll(machine, TestEvents.E1);
assertThat(listener.stateChangedLatch.await(5, TimeUnit.SECONDS), is(true));
assertThat(machine.getState().getIds(), contains(TestStates.S2, TestStates.S21));
}
@@ -95,7 +98,10 @@ public class RelayTests extends AbstractStateMachineTests {
@Override
public void execute(StateContext<TestStates, TestEvents> context) {
context.getStateMachine().sendEvent(TestEvents.E2);
context.getStateMachine()
.sendEvent(Mono.just(MessageBuilder
.withPayload(TestEvents.E2).build()))
.subscribe();
}
};
}

View File

@@ -28,6 +28,7 @@ import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import org.hamcrest.FeatureMatcher;
@@ -66,6 +67,7 @@ public class StateContextTests extends AbstractStateMachineTests {
assertThat(listener.contexts, hasSize(19));
assertThat(listener.contexts, contains(
Arrays.asList(
hasStage(Stage.TRANSITION_START),
hasStage(Stage.EXTENDED_STATE_CHANGED),
hasStage(Stage.TRANSITION),
@@ -84,7 +86,7 @@ public class StateContextTests extends AbstractStateMachineTests {
hasStage(Stage.TRANSITION_END),
hasStage(Stage.STATE_CHANGED),
hasStage(Stage.STATEMACHINE_START),
hasStage(Stage.TRANSITION_END)
hasStage(Stage.TRANSITION_END))
));
assertThat(listener.contexts.get(0).getStage(), is(Stage.TRANSITION_START));
@@ -168,9 +170,10 @@ public class StateContextTests extends AbstractStateMachineTests {
// all nested machines sends these
assertThat(listener.contexts, contains(
Arrays.asList(
hasStage(Stage.EVENT_NOT_ACCEPTED),
hasStage(Stage.EVENT_NOT_ACCEPTED),
hasStage(Stage.EVENT_NOT_ACCEPTED)
hasStage(Stage.EVENT_NOT_ACCEPTED))
));
assertThat(listener.contexts.get(0).getStage(), is(Stage.EVENT_NOT_ACCEPTED));

View File

@@ -34,6 +34,7 @@ import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.TestUtils;
@@ -54,6 +55,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import reactor.core.publisher.Mono;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SessionScopedAnnotationTests.Config2.class, SessionScopedAnnotationTests.Config1.class})
@WebAppConfiguration
@@ -191,7 +194,10 @@ public class SessionScopedAnnotationTests {
@RequestMapping(path="/state", method=RequestMethod.POST)
public HttpEntity<Void> setState(@RequestParam("event") String event) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return new ResponseEntity<Void>(HttpStatus.ACCEPTED);
}

View File

@@ -35,6 +35,7 @@ import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.TestUtils;
@@ -53,6 +54,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import reactor.core.publisher.Mono;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
@@ -170,7 +173,10 @@ public class SessionScopedManualTests {
@RequestMapping(path="/state", method=RequestMethod.POST)
public HttpEntity<Void> setState(@RequestParam("event") String event) {
stateMachine.sendEvent(event);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(event).build()))
.subscribe();
return new ResponseEntity<Void>(HttpStatus.ACCEPTED);
}

View File

@@ -18,6 +18,9 @@ package org.springframework.statemachine.monitor;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import static org.springframework.statemachine.TestUtils.resolveMachine;
import java.util.HashMap;
import java.util.Map;
@@ -32,7 +35,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
@@ -45,23 +47,18 @@ import reactor.core.publisher.Mono;
public class StateMachineMonitorTests extends AbstractStateMachineTests {
@SuppressWarnings({ "unchecked" })
@Test
public void testSimpleMonitor() throws Exception {
context.register(Config1.class);
context.refresh();
StateMachine<String, String> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
StateMachine<String, String> machine = resolveMachine(context);
TestStateMachineMonitor monitor = context.getBean(TestStateMachineMonitor.class);
Action<String, String> taction = context.getBean("taction", Action.class);
Action<String, String> enaction = context.getBean("enaction", Action.class);
Action<String, String> exaction = context.getBean("exaction", Action.class);
LatchAction saction = context.getBean("saction", LatchAction.class);
machine.start();
doStartAndAssert(machine);
assertThat(machine.getState().getIds(), contains("S1"));
machine.sendEvent("E1");
doSendEventAndConsumeAll(machine, "E1");
assertThat(machine.getState().getIds(), contains("S2"));
// there's also initial transition, thus 2 instead 1
assertThat(monitor.transitions.size(), is(2));
@@ -69,9 +66,12 @@ public class StateMachineMonitorTests extends AbstractStateMachineTests {
assertThat(monitor.latch.await(2, TimeUnit.SECONDS), is(true));
assertThat(monitor.actions.size(), is(4));
// TODO: REACTOR yeah we wrap action internally so can't match like this anymore
// Action<String, String> taction = context.getBean("taction", Action.class);
// Action<String, String> enaction = context.getBean("enaction", Action.class);
// Action<String, String> exaction = context.getBean("exaction", Action.class);
// assertThat(monitor.actions.keySet(), containsInAnyOrder(taction, enaction, exaction, saction));
monitor.reset();
machine.sendEvent("E2");
doSendEventAndConsumeAll(machine, "E2");
assertThat(machine.getState().getIds(), contains("S1"));
}