Push deployer configuration out of autoconfig

It tends to pop back into function apps where it is not needed
otherwise. Users that want to use the library need to import
the FunctionConfiguration directly using the
@EnableFunctionDeployer convenience annotation..
This commit is contained in:
Dave Syer
2018-04-25 17:33:24 +01:00
parent 7fa0ed7b6b
commit ebd1646308
22 changed files with 656 additions and 328 deletions

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* A test suite for probing weird ordering problems in the tests.
*
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ FunctionCreatorConfigurationTests.FunctionCompositionTests.class,
FunctionCreatorConfigurationTests.SingleFunctionTests.class,
FunctionCreatorConfigurationTests.ManualSpringFunctionTests.class,
ContextRunnerTests.class,
SpringFunctionAppConfigurationTests.ProcessorTests.class,
SpringFunctionAppConfigurationTests.SourceTests.class,
FunctionCreatorConfigurationTests.ConsumerCompositionTests.class,
SpringFunctionAppConfigurationTests.CompositeTests.class,
ApplicationRunnerTests.class, SpringFunctionAppConfigurationTests.SinkTests.class,
FunctionCreatorConfigurationTests.SupplierCompositionTests.class })
@Ignore
public class AdhocTestSuite {
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import org.junit.Test;
import org.springframework.cloud.function.test.Doubler;
import org.springframework.cloud.function.test.FunctionApp;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*/
public class ApplicationRunnerTests {
@Test
public void startEvaluateAndStop() {
ApplicationRunner runner = new ApplicationRunner(getClass().getClassLoader(),
FunctionApp.class.getName());
runner.run("--spring.main.webEnvironment=false");
assertThat(runner.containsBean(Doubler.class.getName())).isTrue();
assertThat(runner.getBean(Doubler.class.getName())).isNotNull();
runner.close();
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.util.Collections;
import org.junit.Test;
import org.springframework.cloud.function.test.Doubler;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*/
public class ContextRunnerTests {
@Test
public void startEvaluateAndStop() {
ContextRunner runner = new ContextRunner();
runner.run(Doubler.class.getName(), Collections.emptyMap(),
"--spring.main.webEnvironment=false");
assertThat(runner.getContext()).isNotNull();
runner.close();
}
}

View File

@@ -1,120 +0,0 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FunctionConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"function.location=file:target/it/support/target/function-sample-1.0.0.M1.jar", })
public abstract class FunctionConfigurationTests {
@Autowired
protected MessageCollector messageCollector;
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=com.example.functions.Emitter" })
public static class SourceTests extends FunctionConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("one"));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.Emitter,com.example.functions.LengthCounter" })
public static class CompositeTests extends FunctionConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is(3));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.LengthCounter" })
public static class ProcessorTests extends FunctionConfigurationTests {
@Autowired
private Processor processor;
@Test
public void test() throws Exception {
processor.input().send(MessageBuilder.withPayload("hello").build());
Message<?> received = messageCollector.forChannel(processor.output()).poll(1,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("hello".length()));
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = {
"function.bean=com.example.functions.DoubleLogger" })
public static class SinkTests extends FunctionConfigurationTests {
@Autowired
private Sink sink;
@Test
public void test() throws Exception {
// Can't assert side effects.
sink.input().send(MessageBuilder.withPayload(5).build());
}
}
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2017-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { FunctionDeployerConfiguration.class })
@DirtiesContext
public abstract class FunctionCreatorConfigurationTests {
@Autowired
protected FunctionCatalog catalog;
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.location=file:target/test-classes",
"function.bean=org.springframework.cloud.function.test.Doubler" })
public static class SingleFunctionTests extends FunctionCreatorConfigurationTests {
@Test
public void testDouble() {
Function<Flux<Integer>, Flux<Integer>> function = catalog
.lookup(Function.class, "function0");
assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo(4);
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.location=app:classpath",
"function.bean=org.springframework.cloud.function.test.SpringDoubler" })
public static class ManualSpringFunctionTests
extends FunctionCreatorConfigurationTests {
@Test
public void testDouble() {
Function<Flux<Integer>, Flux<Integer>> function = catalog
.lookup(Function.class, "function0");
assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo(4);
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.location=file:target/test-classes",
"function.bean=org.springframework.cloud.function.test.NumberEmitter,"
+ "org.springframework.cloud.function.test.Frenchizer" })
public static class SupplierCompositionTests
extends FunctionCreatorConfigurationTests {
@Test
public void testSupplier() {
Supplier<Integer> function = catalog.lookup(Supplier.class, "function0");
assertThat(function).isNull();
}
@Test
public void testFunction() {
Supplier<Flux<String>> function = catalog.lookup(Supplier.class,
"function0|function1");
assertThat(function.get().blockFirst()).isEqualTo("un");
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.location=file:target/test-classes",
"function.bean=org.springframework.cloud.function.test.Doubler,"
+ "org.springframework.cloud.function.test.Frenchizer" })
public static class FunctionCompositionTests
extends FunctionCreatorConfigurationTests {
@Test
public void testFunction() {
Function<Flux<Integer>, Flux<String>> function = catalog
.lookup(Function.class, "function0|function1");
assertThat(function.apply(Flux.just(2)).blockFirst()).isEqualTo("quatre");
}
@Test
public void testThen() {
Function<Integer, String> function = catalog.lookup(Function.class,
"function1");
assertThat(function).isNull();
}
}
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.location=file:target/test-classes",
"function.bean=org.springframework.cloud.function.test.Frenchizer,"
+ "org.springframework.cloud.function.test.Printer" })
public static class ConsumerCompositionTests
extends FunctionCreatorConfigurationTests {
@Rule
public OutputCapture capture = new OutputCapture();
@Test
public void testConsumer() {
Function<Flux<Integer>, Mono<Void>> function = catalog.lookup(Function.class,
"function0|function1");
function.apply(Flux.just(2)).block();
capture.expect(containsString("Seen deux"));
}
}
}

View File

@@ -16,50 +16,45 @@
package org.springframework.cloud.function.deployer;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FunctionConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@SpringBootTest(classes = FunctionDeployerConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource(properties = {
"function.location=file:target/it/support/target/function-sample-1.0.0.M1-exec.jar", })
public abstract class SpringFunctionAppConfigurationTests {
@Autowired
protected MessageCollector messageCollector;
protected FunctionCatalog catalog;
@EnableAutoConfiguration
@TestPropertySource(properties = { "function.bean=myEmitter",
"function.main=com.example.functions.FunctionApp" })
public static class SourceTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("one"));
Supplier<Flux<String>> function = catalog.lookup(Supplier.class, "function0");
assertThat(function.get().blockFirst()).isEqualTo("one");
}
}
@@ -69,16 +64,11 @@ public abstract class SpringFunctionAppConfigurationTests {
"function.main=com.example.functions.FunctionApp" })
public static class CompositeTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Source source;
@Test
public void test() throws Exception {
Message<?> received = messageCollector.forChannel(source.output()).poll(2,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is(3));
Supplier<Flux<Integer>> function = catalog.lookup(Supplier.class,
"function0|function1");
assertThat(function.get().blockFirst()).isEqualTo(3);
}
}
@@ -88,16 +78,11 @@ public abstract class SpringFunctionAppConfigurationTests {
"function.main=com.example.functions.FunctionApp" })
public static class ProcessorTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Processor processor;
@Test
public void test() throws Exception {
processor.input().send(MessageBuilder.withPayload("hello").build());
Message<?> received = messageCollector.forChannel(processor.output()).poll(1,
TimeUnit.SECONDS);
assertThat(received.getPayload(), Matchers.is("hello".length()));
Function<Flux<String>, Flux<Integer>> function = catalog
.lookup(Function.class, "function0");
assertThat(function.apply(Flux.just("spam")).blockFirst()).isEqualTo(4);
}
}
@@ -107,13 +92,16 @@ public abstract class SpringFunctionAppConfigurationTests {
"function.main=com.example.functions.FunctionApp" })
public static class SinkTests extends SpringFunctionAppConfigurationTests {
@Autowired
private Sink sink;
@Rule
public OutputCapture capture = new OutputCapture();
@Test
public void test() throws Exception {
// Can't assert side effects.
sink.input().send(MessageBuilder.withPayload(5).build());
Function<Flux<Integer>, Mono<Void>> function = catalog.lookup(Function.class,
"function0");
function.apply(Flux.just(5)).block();
capture.expect(containsString(String.format("10%n")));
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import java.util.function.Function;
public class Doubler implements Function<Integer, Integer> {
@Override
public Integer apply(Integer integer) {
return 2 * integer;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import java.util.function.Function;
import javax.annotation.PostConstruct;
public class Frenchizer implements Function<Integer, String> {
private String[] numbers;
@PostConstruct
public void init() {
this.numbers = new String[4];
numbers[0] = "un";
numbers[1] = "deux";
numbers[2] = "trois";
numbers[3] = "quatre";
}
@Override
public String apply(Integer integer) {
if (integer < this.numbers.length + 1) {
return this.numbers[integer - 1];
}
throw new RuntimeException();
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
/**
* @author Dave Syer
*/
@SpringBootConfiguration
public class FunctionApp {
@Bean
public Doubler myDoubler() {
return new Doubler();
}
@Bean
public Frenchizer myFrenchizer() {
return new Frenchizer();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(FunctionApp.class, args);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import java.util.function.Supplier;
public class NumberEmitter implements Supplier<Integer> {
@Override
public Integer get() {
return 1;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import java.util.function.Consumer;
public class Printer implements Consumer<Object> {
@Override
public void accept(Object o) {
System.err.println("Seen " + o);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.test;
import java.util.function.Function;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
public class SpringDoubler implements Function<Integer, Integer> {
@Autowired
private ConfigurableApplicationContext context;
@PostConstruct
public void init() {
if (this.context == null) {
context = new SpringApplicationBuilder(FunctionApp.class).bannerMode(Mode.OFF).registerShutdownHook(false)
.web(false).run();
}
}
@PreDestroy
public void close() {
if (context != null) {
context.close();
}
}
@Override
public Integer apply(Integer integer) {
return 2 * integer;
}
}