Reactive changes for docs

- Relates #742
- Relates #744
This commit is contained in:
Janne Valkealahti
2019-05-11 10:05:16 +01:00
parent 64779f4039
commit be6ee233c5
7 changed files with 84 additions and 48 deletions

View File

@@ -64,6 +64,8 @@ import org.springframework.statemachine.support.StateMachineInterceptorAdapter;
import org.springframework.statemachine.transition.Transition;
import org.springframework.statemachine.transition.TransitionConflictPolicy;
import reactor.core.publisher.Mono;
/**
* Tests for state machine configuration.
*
@@ -547,7 +549,7 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests {
void method() {
StateMachine<States,Events> stateMachine = factory.getStateMachine();
stateMachine.start();
stateMachine.startReactively().subscribe();
}
}
// end::snippetL[]
@@ -589,13 +591,16 @@ public class DocsConfigurationSampleTests extends AbstractStateMachineTests {
StateMachine<States, Events> stateMachine;
void signalMachine() {
stateMachine.sendEvent(Events.E1);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.E1).build()))
.subscribe();
Message<Events> message = MessageBuilder
.withPayload(Events.E2)
.setHeader("foo", "bar")
.build();
stateMachine.sendEvent(message);
stateMachine.sendEvent(Mono.just(message)).subscribe();
}
// end::snippetO[]

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -18,13 +18,15 @@ package org.springframework.statemachine.docs;
import static org.hamcrest.Matchers.containsInAnyOrder;
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 org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineSystemConstants;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
@@ -39,12 +41,11 @@ public class DocsConfigurationSampleTests10 extends AbstractStateMachineTests {
public void testConfig1() throws Exception {
context.register(Config1.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
machine.start();
StateMachine<String, String> machine = resolveMachine(context);
doStartAndAssert(machine);
assertThat(machine.getState().getIds(), containsInAnyOrder("S1"));
assertThat(machine.getId(), is("mymachine"));
machine.sendEvent("E1");
doSendEventAndConsumeAll(machine, "E1");
assertThat(machine.getState().getIds(), containsInAnyOrder("S2"));
}
@@ -57,10 +58,10 @@ public class DocsConfigurationSampleTests10 extends AbstractStateMachineTests {
StateMachineFactory<String, String> factory = context.getBean(StateMachineFactory.class);
StateMachine<String, String> machine = factory.getStateMachine("mymachine");
// end::snippetB[]
machine.start();
doStartAndAssert(machine);
assertThat(machine.getState().getIds(), containsInAnyOrder("S1"));
assertThat(machine.getId(), is("mymachine"));
machine.sendEvent("E1");
doSendEventAndConsumeAll(machine, "E1");
assertThat(machine.getState().getIds(), containsInAnyOrder("S2"));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2018-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.
@@ -30,6 +30,8 @@ import org.springframework.statemachine.config.builders.StateMachineConfiguratio
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import reactor.core.publisher.Mono;
public class DocsConfigurationSampleTests11 extends AbstractStateMachineTests {
// tag::snippetA[]
@@ -114,10 +116,12 @@ public class DocsConfigurationSampleTests11 extends AbstractStateMachineTests {
StateMachine<String, String> stateMachine;
void sendEventUsingTimeout() {
stateMachine.sendEvent(MessageBuilder
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload("E1")
.setHeader(StateMachineMessageHeaders.HEADER_DO_ACTION_TIMEOUT, 5000)
.build());
.build()))
.subscribe();
}
// end::snippetC[]

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 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.
@@ -17,6 +17,9 @@ package org.springframework.statemachine.docs;
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.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -33,6 +36,7 @@ import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.StateContext;
@@ -54,6 +58,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;
public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
@Override
@@ -182,7 +188,10 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
@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);
}
@@ -198,17 +207,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
public void testConfig51() throws Exception {
context.register(Config5.class, ExecutorConfig.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
StateMachine<String, String> machine = resolveMachine(context);
TestListener listener = new TestListener();
machine.addStateListener(listener);
machine.start();
doStartAndAssert(machine);
assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(1));
listener.reset(0, 0, 2);
machine.sendEvent("DEPLOY");
machine.sendEvent("DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(2));
}
@@ -217,17 +225,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
public void testConfig52() throws Exception {
context.register(Config5.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
StateMachine<String, String> machine = resolveMachine(context);
TestListener listener = new TestListener();
machine.addStateListener(listener);
machine.start();
doStartAndAssert(machine);
assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(1));
listener.reset(0, 0, 2);
machine.sendEvent("DEPLOY");
machine.sendEvent("DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(2));
}
@@ -236,17 +243,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
public void testConfig61() throws Exception {
context.register(Config6.class, ExecutorConfig.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
StateMachine<String, String> machine = resolveMachine(context);
TestListener listener = new TestListener();
machine.addStateListener(listener);
machine.start();
doStartAndAssert(machine);
assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(1));
listener.reset(0, 0, 2);
machine.sendEvent("DEPLOY");
machine.sendEvent("DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(2));
}
@@ -255,17 +261,16 @@ public class DocsConfigurationSampleTests2 extends AbstractStateMachineTests {
public void testConfig62() throws Exception {
context.register(Config6.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
StateMachine<String, String> machine = resolveMachine(context);
TestListener listener = new TestListener();
machine.addStateListener(listener);
machine.start();
doStartAndAssert(machine);
assertThat(listener.stateMachineStartedLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredLatch.await(3, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(1));
listener.reset(0, 0, 2);
machine.sendEvent("DEPLOY");
machine.sendEvent("DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
doSendEventAndConsumeAll(machine, "DEPLOY");
assertThat(listener.readyStateEnteredLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.readyStateEnteredCount, is(2));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -23,6 +23,7 @@ import java.util.HashMap;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.AbstractStateMachineTests;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.StateMachineContext;
@@ -34,6 +35,8 @@ import org.springframework.statemachine.config.builders.StateMachineTransitionCo
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.persist.StateMachinePersister;
import reactor.core.publisher.Mono;
public class DocsConfigurationSampleTests5 extends AbstractStateMachineTests {
@Override
@@ -54,9 +57,12 @@ public class DocsConfigurationSampleTests5 extends AbstractStateMachineTests {
StateMachine<String, String> stateMachine1 = context.getBean("machine1", StateMachine.class);
StateMachine<String, String> stateMachine2 = context.getBean("machine2", StateMachine.class);
stateMachine1.start();
stateMachine1.startReactively().block();
stateMachine1.sendEvent("E1");
stateMachine1
.sendEvent(Mono.just(MessageBuilder
.withPayload("E1").build()))
.blockLast();
assertThat(stateMachine1.getState().getIds(), contains("S2"));
persister.persist(stateMachine1, "myid");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-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.
@@ -17,6 +17,8 @@ package org.springframework.statemachine.docs;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import java.util.ArrayList;
import java.util.Collection;
@@ -59,9 +61,9 @@ public class DocsConfigurationSampleTests6 {
ObjectStateMachineFactory<String, String> factory = new ObjectStateMachineFactory<>(stateMachineModel);
StateMachine<String, String> stateMachine = factory.getStateMachine();
// end::snippetA[]
stateMachine.start();
doStartAndAssert(stateMachine);
assertThat(stateMachine.getState().getIds(), contains("S1"));
stateMachine.sendEvent("E1");
doSendEventAndConsumeAll(stateMachine, "E1");
assertThat(stateMachine.getState().getIds(), contains("S2"));
}

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.
@@ -17,11 +17,15 @@ package org.springframework.statemachine.docs;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.springframework.statemachine.TestUtils.doSendEventAndConsumeAll;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;
import java.util.EnumSet;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
@@ -32,6 +36,8 @@ import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import reactor.core.publisher.Mono;
public class IntroSample {
// tag::snippetA[]
@@ -94,19 +100,26 @@ public class IntroSample {
StateMachine<States, Events> stateMachine;
void doSignals() {
stateMachine.sendEvent(Events.EVENT1);
stateMachine.sendEvent(Events.EVENT2);
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.EVENT1).build()))
.subscribe();
stateMachine
.sendEvent(Mono.just(MessageBuilder
.withPayload(Events.EVENT2).build()))
.subscribe();
}
}
// end::snippetD[]
@Test
public void testManual() throws Exception {
StateMachine<States, Events> stateMachine = buildMachine();
stateMachine.start();
doStartAndAssert(stateMachine);
assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE1));
stateMachine.sendEvent(Events.EVENT1);
doSendEventAndConsumeAll(stateMachine, Events.EVENT1);
assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE2));
stateMachine.sendEvent(Events.EVENT2);
doSendEventAndConsumeAll(stateMachine, Events.EVENT2);
assertThat(stateMachine.getState().getIds(), containsInAnyOrder(States.STATE1));
}