Reduce use of SocketUtils in tests

Resolves #825
This commit is contained in:
onobc
2022-03-13 14:29:02 -05:00
committed by Oleg Zhurakousky
parent 2ce45726ab
commit 8b589c67e5
19 changed files with 420 additions and 251 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2020-2020 the original author or authors. * Copyright 2020-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@@ -50,6 +49,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Daniel Zou * @author Daniel Zou
* @author Mike Eltsufin * @author Mike Eltsufin
* @author Chris Bono
*/ */
final public class LocalServerTestSupport { final public class LocalServerTestSupport {
@@ -82,10 +82,7 @@ final public class LocalServerTestSupport {
} }
} }
static ServerProcess startServer(Class<?> springApplicationMainClass, String function) static ServerProcess startServer(Class<?> springApplicationMainClass, String function) throws IOException {
throws InterruptedException, IOException {
int port = SocketUtils.findAvailableTcpPort();
String signatureType = "http"; String signatureType = "http";
String target = FunctionInvoker.class.getCanonicalName(); String target = FunctionInvoker.class.getCanonicalName();
@@ -102,7 +99,7 @@ final public class LocalServerTestSupport {
ProcessBuilder processBuilder = new ProcessBuilder().command(command).redirectErrorStream(true); ProcessBuilder processBuilder = new ProcessBuilder().command(command).redirectErrorStream(true);
Map<String, String> environment = new HashMap<>(); Map<String, String> environment = new HashMap<>();
environment.put("PORT", String.valueOf(port)); environment.put("PORT", String.valueOf(0));
environment.put("K_SERVICE", "test-function"); environment.put("K_SERVICE", "test-function");
environment.put("FUNCTION_SIGNATURE_TYPE", signatureType); environment.put("FUNCTION_SIGNATURE_TYPE", signatureType);
environment.put("FUNCTION_TARGET", target); environment.put("FUNCTION_TARGET", target);
@@ -112,28 +109,28 @@ final public class LocalServerTestSupport {
} }
processBuilder.environment().putAll(environment); processBuilder.environment().putAll(environment);
Process serverProcess = processBuilder.start(); Process serverProcess = processBuilder.start();
CountDownLatch ready = new CountDownLatch(1); Future<Integer> outputMonitorResult = EXECUTOR.submit(() -> monitorOutput(serverProcess.getInputStream()));
StringBuilder output = new StringBuilder();
Future<?> outputMonitorResult = EXECUTOR int port;
.submit(() -> monitorOutput(serverProcess.getInputStream(), ready, output)); try {
boolean serverReady = ready.await(5, TimeUnit.SECONDS); port = outputMonitorResult.get(5L, TimeUnit.SECONDS);
if (!serverReady) { }
catch (Exception ex) {
serverProcess.destroy(); serverProcess.destroy();
throw new AssertionError("Server never became ready"); throw new AssertionError("Server never became ready");
} }
return new ServerProcess(serverProcess, outputMonitorResult, output, port); return new ServerProcess(serverProcess, port);
} }
private static void monitorOutput(InputStream processOutput, CountDownLatch ready, StringBuilder output) { private static Integer monitorOutput(InputStream processOutput) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(processOutput))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(processOutput))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.contains(SERVER_READY_STRING)) {
ready.countDown();
}
System.out.println(line); System.out.println(line);
synchronized (output) { if (line.contains(SERVER_READY_STRING)) {
output.append(line).append('\n'); // Started ServerConnector@192b07fd{HTTP/1.1,[http/1.1]}{0.0.0.0:59259}
String portStr = line.substring(line.lastIndexOf(':') + 1, line.lastIndexOf('}'));
return Integer.parseInt(portStr);
} }
if (line.contains("WARNING")) { if (line.contains("WARNING")) {
throw new AssertionError("Found warning in server output:\n" + line); throw new AssertionError("Found warning in server output:\n" + line);
@@ -143,22 +140,17 @@ final public class LocalServerTestSupport {
catch (IOException e) { catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
throw new RuntimeException("End of input stream and server never became ready");
} }
static class ServerProcess implements AutoCloseable { static class ServerProcess implements AutoCloseable {
private final Process process; private final Process process;
private final Future<?> outputMonitorResult;
private final StringBuilder output;
private final int port; private final int port;
ServerProcess(Process process, Future<?> outputMonitorResult, StringBuilder output, int port) { ServerProcess(Process process, int port) {
this.process = process; this.process = process;
this.outputMonitorResult = outputMonitorResult;
this.output = output;
this.port = port; this.port = port;
} }
@@ -166,7 +158,6 @@ final public class LocalServerTestSupport {
return process; return process;
} }
@Override @Override
public void close() { public void close() {
process().destroy(); process().destroy();

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2021 the original author or authors. * Copyright 2021-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,28 +16,34 @@
package org.springframework.cloud.function.grpc; package org.springframework.cloud.function.grpc;
import java.util.Collections;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.util.ClassUtils;
import io.grpc.BindableService; import io.grpc.BindableService;
import io.grpc.Server; import io.grpc.Server;
import io.grpc.ServerBuilder; import io.grpc.ServerBuilder;
import io.grpc.protobuf.services.ProtoReflectionService; import io.grpc.protobuf.services.ProtoReflectionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.util.ClassUtils;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Dave Syer * @author Dave Syer
* @author Chris Bono
* *
* @since 3.2 * @since 3.2
* *
*/ */
class GrpcServer implements SmartLifecycle { class GrpcServer implements SmartLifecycle, EnvironmentAware {
private Log logger = LogFactory.getLog(GrpcServer.class); private Log logger = LogFactory.getLog(GrpcServer.class);
@@ -49,6 +55,8 @@ class GrpcServer implements SmartLifecycle {
private Server server; private Server server;
private Environment environment;
GrpcServer(FunctionGrpcProperties grpcProperties, BindableService[] grpcMessageServices) { GrpcServer(FunctionGrpcProperties grpcProperties, BindableService[] grpcMessageServices) {
this.grpcProperties = grpcProperties; this.grpcProperties = grpcProperties;
this.grpcMessageServices = grpcMessageServices; this.grpcMessageServices = grpcMessageServices;
@@ -70,7 +78,12 @@ class GrpcServer implements SmartLifecycle {
logger.info("Starting gRPC server"); logger.info("Starting gRPC server");
this.server.start(); this.server.start();
logger.info("gRPC server is listening on port " + this.grpcProperties.getPort()); logger.info("gRPC server is listening on port " + this.server.getPort());
if (environment instanceof ConfigurableEnvironment) {
((ConfigurableEnvironment) this.environment).getPropertySources().addFirst(
new MapPropertySource("grpcServerProps", Collections.singletonMap("local.grpc.server.port", server.getPort())));
}
} }
catch (Exception e) { catch (Exception e) {
stop(); stop();
@@ -90,4 +103,9 @@ class GrpcServer implements SmartLifecycle {
public boolean isRunning() { public boolean isRunning() {
return this.server != null && !this.server.isShutdown(); return this.server != null && !this.server.isShutdown();
} }
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2021 the original author or authors. * Copyright 2021-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ import static org.junit.jupiter.api.Assertions.fail;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* * @author Chris Bono
*/ */
@Disabled @Disabled
public class GrpcInteractionTests { public class GrpcInteractionTests {
@@ -62,13 +62,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testRequestReply() { public void testRequestReply() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.grpc.port=" + port)) { "--spring.cloud.function.grpc.port=0")) {
int port = patientlyGetPort(context);
Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes()) Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes())
.setHeader("foo", "bar") .setHeader("foo", "bar")
@@ -82,13 +83,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testRequestReplyWithMonoReturn() { public void testRequestReplyWithMonoReturn() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=uppercaseMonoReturn", "--spring.cloud.function.definition=uppercaseMonoReturn",
"--spring.cloud.function.grpc.port=" + port)) { "--spring.cloud.function.grpc.port=0")) {
int port = patientlyGetPort(context);
Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes()) Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes())
.setHeader("foo", "bar") .setHeader("foo", "bar")
@@ -102,14 +104,15 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testRequestReplyWithFluxReturn() { public void testRequestReplyWithFluxReturn() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=uppercaseFluxReturn", "--spring.cloud.function.definition=uppercaseFluxReturn",
"--spring.cloud.function.grpc.port=" + port)) { "--spring.cloud.function.grpc.port=0")) {
int port = patientlyGetPort(context);
Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes()) Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes())
.setHeader("foo", "bar") .setHeader("foo", "bar")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
@@ -125,13 +128,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testRequstReplyFunctionDefinitionInMessage() { public void testRequstReplyFunctionDefinitionInMessage() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.grpc.port=" + port)) { "--spring.cloud.function.grpc.port=0")) {
int port = patientlyGetPort(context);
Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes()) Message<byte[]> message = MessageBuilder.withPayload("\"hello gRPC\"".getBytes())
.setHeader("foo", "bar") .setHeader("foo", "bar")
.setHeader("spring.cloud.function.definition", "reverse") .setHeader("spring.cloud.function.definition", "reverse")
@@ -144,14 +148,15 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testBidirectionalStreamWithImperativeFunction() { public void testBidirectionalStreamWithImperativeFunction() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.grpc.port=" + port)) { "--spring.cloud.function.grpc.port=0")) {
int port = patientlyGetPort(context);
List<Message<byte[]>> messages = new ArrayList<>(); List<Message<byte[]>> messages = new ArrayList<>();
messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar") messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar")
.build()); .build());
@@ -172,15 +177,15 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testBidirectionalStreamWithReactiveFunction() { public void testBidirectionalStreamWithReactiveFunction() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=uppercaseReactive", "--spring.cloud.function.definition=uppercaseReactive",
"--spring.cloud.function.grpc.port=" "--spring.cloud.function.grpc.port=0")) {
+ port)) {
int port = patientlyGetPort(context);
List<Message<byte[]>> messages = new ArrayList<>(); List<Message<byte[]>> messages = new ArrayList<>();
messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar") messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
@@ -204,14 +209,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testClientStreaming() { public void testClientStreaming() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=streamInStringOut", "--spring.cloud.function.definition=streamInStringOut",
"--spring.cloud.function.grpc.port=" "--spring.cloud.function.grpc.port=0")) {
+ port)) {
int port = patientlyGetPort(context);
List<Message<byte[]>> messages = new ArrayList<>(); List<Message<byte[]>> messages = new ArrayList<>();
messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar") messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar")
@@ -229,14 +234,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testServerStreaming() { public void testServerStreaming() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=stringInStreamOut", "--spring.cloud.function.definition=stringInStreamOut",
"--spring.cloud.function.grpc.port=" "--spring.cloud.function.grpc.port=0")) {
+ port)) {
int port = patientlyGetPort(context);
Message<byte[]> message = MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar").build(); Message<byte[]> message = MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar").build();
@@ -251,14 +256,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testBiStreamStreamInStringOutFailure() { public void testBiStreamStreamInStringOutFailure() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=streamInStringOut", "--spring.cloud.function.definition=streamInStringOut",
"--spring.cloud.function.grpc.port=" "--spring.cloud.function.grpc.port=0")) {
+ port)) {
int port = patientlyGetPort(context);
List<Message<byte[]>> messages = new ArrayList<>(); List<Message<byte[]>> messages = new ArrayList<>();
messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar") messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar")
@@ -276,14 +281,14 @@ public class GrpcInteractionTests {
} }
@Test @Test
public void testBiStreamStringInStreamOutFailure() { public void testBiStreamStringInStreamOutFailure() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
try (ConfigurableApplicationContext context = new SpringApplicationBuilder( try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleConfiguration.class).web(WebApplicationType.NONE).run( SampleConfiguration.class).web(WebApplicationType.NONE).run(
"--spring.jmx.enabled=false", "--spring.jmx.enabled=false",
"--spring.cloud.function.definition=stringInStreamOut", "--spring.cloud.function.definition=stringInStreamOut",
"--spring.cloud.function.grpc.port=" "--spring.cloud.function.grpc.port=0")) {
+ port)) {
int port = patientlyGetPort(context);
List<Message<byte[]>> messages = new ArrayList<>(); List<Message<byte[]>> messages = new ArrayList<>();
messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar") messages.add(MessageBuilder.withPayload("\"Ricky\"".getBytes()).setHeader("foo", "bar")
@@ -300,6 +305,17 @@ public class GrpcInteractionTests {
} }
} }
private int patientlyGetPort(ConfigurableApplicationContext context) throws InterruptedException {
Thread.sleep(500);
String port = context.getEnvironment().getProperty("local.grpc.server.port");
if (port == null) {
Thread.sleep(500);
port = context.getEnvironment().getProperty("local.grpc.server.port");
assertThat(port).as("Unable to get 'local.grpc.server.port' - server may not have started up").isNotNull();
}
return Integer.valueOf(port);
}
@EnableAutoConfiguration @EnableAutoConfiguration
public static class SampleConfiguration { public static class SampleConfiguration {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2021 the original author or authors. * Copyright 2021-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -38,24 +38,25 @@ import org.springframework.util.MimeTypeUtils;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* * @author Chris Bono
*/ */
public class MessageRoutingCallbackRSocketTests { public class MessageRoutingCallbackRSocketTests {
@Test @Test
public void testRoutingWithRoutingCallback() { public void testRoutingWithRoutingCallback() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(RoutingCallbackFunctionConfiguration.class) new SpringApplicationBuilder(RoutingCallbackFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.expected-content-type=text/plain", "--spring.cloud.function.expected-content-type=text/plain",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
int port = applicationContext.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
// imperative // imperative
rsocketRequesterBuilder.tcp("localhost", port) rsocketRequesterBuilder.tcp("localhost", port)
.route("foo") .route("foo")

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2021 the original author or authors. * Copyright 2021-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -41,20 +41,21 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* * @author Chris Bono
*/ */
public class MessagingTests { public class MessagingTests {
@Test @Test
public void testPojoToStringViaMessage() { public void testPojoToStringViaMessage() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -76,14 +77,15 @@ public class MessagingTests {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Test @Test
public void testPojoToStringViaMessageMap() { public void testPojoToStringViaMessageMap() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -107,14 +109,15 @@ public class MessagingTests {
@Test @Test
public void testPojoToStringViaMessageExpectMessage() { public void testPojoToStringViaMessageExpectMessage() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -136,14 +139,15 @@ public class MessagingTests {
@Test @Test
public void testPojoMessageToPojoViaMessage() { public void testPojoMessageToPojoViaMessage() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -167,14 +171,15 @@ public class MessagingTests {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Test @Test
public void testPojoMessageToPojoViaMap() { public void testPojoMessageToPojoViaMap() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -200,14 +205,15 @@ public class MessagingTests {
@Test @Test
public void testPojoMessageToPojoViaMessageExpectMessage() { public void testPojoMessageToPojoViaMessageExpectMessage() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -229,14 +235,15 @@ public class MessagingTests {
@Test @Test
public void testPojoMessageToPojoViaMessageExpectMessageRawPayload() { public void testPojoMessageToPojoViaMessageExpectMessageRawPayload() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -258,14 +265,15 @@ public class MessagingTests {
@Test @Test
public void testPojoMessageToPojoViaMessageExpectMessageStringPayload() { public void testPojoMessageToPojoViaMessageExpectMessageStringPayload() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -287,14 +295,15 @@ public class MessagingTests {
@Test @Test
public void testPojoToMessageMap() { public void testPojoToMessageMap() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(MessagingConfiguration.class) new SpringApplicationBuilder(MessagingConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
Person p = new Person(); Person p = new Person();
@@ -311,7 +320,9 @@ public class MessagingTests {
} }
} }
private int getLocalRsocketPort(ConfigurableApplicationContext context) {
return context.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
}
@EnableAutoConfiguration @EnableAutoConfiguration
@Configuration @Configuration

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2021 the original author or authors. * Copyright 2021-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -44,12 +44,12 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 3.1 * @since 3.1
*/ */
public class RSocketAutoConfigurationRoutingTests { public class RSocketAutoConfigurationRoutingTests {
@Test @Test
public void testRoutingWithRoute() { public void testRoutingWithRoute() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
@@ -57,8 +57,10 @@ public class RSocketAutoConfigurationRoutingTests {
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.routing-expression=headers.func_name", "--spring.cloud.function.routing-expression=headers.func_name",
"--spring.cloud.function.expected-content-type=text/plain", "--spring.cloud.function.expected-content-type=text/plain",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = applicationContext.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -97,7 +99,6 @@ public class RSocketAutoConfigurationRoutingTests {
@Test @Test
public void testRoutingWithDefinition() { public void testRoutingWithDefinition() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
@@ -106,8 +107,10 @@ public class RSocketAutoConfigurationRoutingTests {
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.routing-expression=headers.func_name", "--spring.cloud.function.routing-expression=headers.func_name",
"--spring.cloud.function.expected-content-type=text/plain", "--spring.cloud.function.expected-content-type=text/plain",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = applicationContext.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -146,7 +149,6 @@ public class RSocketAutoConfigurationRoutingTests {
@Test @Test
public void testRoutingWithDefinitionMessageFunction() { public void testRoutingWithDefinitionMessageFunction() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
@@ -155,8 +157,9 @@ public class RSocketAutoConfigurationRoutingTests {
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.routing-expression=headers.func_name", "--spring.cloud.function.routing-expression=headers.func_name",
"--spring.cloud.function.expected-content-type=text/plain", "--spring.cloud.function.expected-content-type=text/plain",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = applicationContext.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2020-2020 the original author or authors. * Copyright 2020-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -48,20 +48,22 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 3.1 * @since 3.1
*/ */
public class RSocketAutoConfigurationTests { public class RSocketAutoConfigurationTests {
@Test @Test
public void testNonExistingFunctionInRoute() { public void testNonExistingFunctionInRoute() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -77,14 +79,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testNonExistingFunctionInRouteSingleFunctionInCatalog() { public void testNonExistingFunctionInRouteSingleFunctionInCatalog() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SingleFunctionConfiguration.class) new SpringApplicationBuilder(SingleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -103,15 +106,16 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testImperativeFunctionAsRequestReplyWithDefinition() { public void testImperativeFunctionAsRequestReplyWithDefinition() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -129,15 +133,16 @@ public class RSocketAutoConfigurationTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testWithCborContentType() { public void testWithCborContentType() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -158,7 +163,6 @@ public class RSocketAutoConfigurationTests {
@Test @Test
@Disabled @Disabled
public void testImperativeFunctionAsRequestReplyWithDefinitionExplicitExpectedOutputCt() { public void testImperativeFunctionAsRequestReplyWithDefinitionExplicitExpectedOutputCt() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
@@ -166,8 +170,10 @@ public class RSocketAutoConfigurationTests {
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=uppercase", "--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.expected-content-type=application/json", "--spring.cloud.function.expected-content-type=application/json",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -184,14 +190,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testImperativeFunctionAsRequestReply() { public void testImperativeFunctionAsRequestReply() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -208,15 +215,16 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testWithRouteAndDefinition() { public void testWithRouteAndDefinition() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=echo", "--spring.cloud.function.definition=echo",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -233,14 +241,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testImperativeFunctionAsRequestReplyWithComposition() { public void testImperativeFunctionAsRequestReplyWithComposition() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -257,14 +266,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testSupplierAsRequestReply() { public void testSupplierAsRequestReply() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -281,14 +291,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testImperativeFunctionAsRequestStream() { public void testImperativeFunctionAsRequestStream() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -305,14 +316,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testImperativeFunctionAsRequestChannel() { public void testImperativeFunctionAsRequestChannel() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -330,14 +342,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testReactiveFunctionAsRequestReply() { public void testReactiveFunctionAsRequestReply() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -354,14 +367,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testReactiveFunctionAsRequestStream() { public void testReactiveFunctionAsRequestStream() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -394,14 +408,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testReactiveFunctionAsRequestChannel() { public void testReactiveFunctionAsRequestChannel() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -418,16 +433,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testRequestReplyFunctionWithDistributedComposition() { public void testRequestReplyFunctionWithDistributedComposition() {
int portA = SocketUtils.findAvailableTcpPort();
int portB = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=uppercase|concat", "--spring.cloud.function.definition=uppercase|concat",
"--spring.rsocket.server.port=" + portA); "--spring.rsocket.server.port=0");
) { ) {
int portA = getLocalRsocketPort(applicationContext);
try ( try (
ConfigurableApplicationContext applicationContext2 = ConfigurableApplicationContext applicationContext2 =
@@ -435,9 +449,11 @@ public class RSocketAutoConfigurationTests {
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=reverse>localhost:" + portA + "|wrap", "--spring.cloud.function.definition=reverse>localhost:" + portA + "|wrap",
"--spring.rsocket.server.port=" + portB); "--spring.rsocket.server.port=0");
) { ) {
int portB = getLocalRsocketPort(applicationContext2);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext2.getBean(RSocketRequester.Builder.class); applicationContext2.getBean(RSocketRequester.Builder.class);
@@ -497,17 +513,17 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testFireAndForgetConsumer() { public void testFireAndForgetConsumer() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
SampleFunctionConfiguration config = applicationContext.getBean(SampleFunctionConfiguration.class); SampleFunctionConfiguration config = applicationContext.getBean(SampleFunctionConfiguration.class);
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -560,15 +576,16 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testRoutingWithRoutingFunction() { public void testRoutingWithRoutingFunction() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.routing-expression=headers.function_definition", "--spring.cloud.function.routing-expression=headers.function_definition",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -586,14 +603,15 @@ public class RSocketAutoConfigurationTests {
@Test @Test
public void testByteArrayInOut() { public void testByteArrayInOut() {
int port = SocketUtils.findAvailableTcpPort();
try ( try (
ConfigurableApplicationContext applicationContext = ConfigurableApplicationContext applicationContext =
new SpringApplicationBuilder(SampleFunctionConfiguration.class) new SpringApplicationBuilder(SampleFunctionConfiguration.class)
.web(WebApplicationType.NONE) .web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG", .run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.rsocket.server.port=" + port); "--spring.rsocket.server.port=0");
) { ) {
int port = getLocalRsocketPort(applicationContext);
RSocketRequester.Builder rsocketRequesterBuilder = RSocketRequester.Builder rsocketRequesterBuilder =
applicationContext.getBean(RSocketRequester.Builder.class); applicationContext.getBean(RSocketRequester.Builder.class);
@@ -615,6 +633,10 @@ public class RSocketAutoConfigurationTests {
} }
} }
private int getLocalRsocketPort(ConfigurableApplicationContext context) {
return context.getEnvironment().getProperty("local.rsocket.server.port", Integer.class);
}
@EnableAutoConfiguration @EnableAutoConfiguration
@Configuration @Configuration
public static class SampleFunctionConfiguration { public static class SampleFunctionConfiguration {

View File

@@ -29,7 +29,6 @@ import reactor.test.StepVerifier;
import org.springframework.boot.WebApplicationType; import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.utils.SocketUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -95,14 +94,14 @@ public class RoutingBrokerTests {
} }
private void setup(boolean routingWithProperty) { private void setup(boolean routingWithProperty) {
int brokerProxyPort = SocketUtils.findAvailableTcpPort(); int brokerProxyPort = TestSocketUtils.findAvailableTcpPort();
int brokerClusterPort = SocketUtils.findAvailableTcpPort(); int brokerClusterPort = TestSocketUtils.findAvailableTcpPort();
// start broker // start broker
brokerContext = new SpringApplicationBuilder(SimpleConfiguration.class).web(WebApplicationType.NONE).run( brokerContext = new SpringApplicationBuilder(SimpleConfiguration.class).web(WebApplicationType.NONE).run(
"--logging.level.io.rsocket.broker=TRACE", "--logging.level.io.rsocket.broker=TRACE",
"--spring.cloud.function.rsocket.enabled=false", "--spring.cloud.function.rsocket.enabled=false",
"--io.rsocket.broker.client.enabled=false", "--io.rsocket.broker.client.enabled=false",
"--io.rsocket.broker.enabled=true", "--io.rsocket.broker.enaFunctionEndpointInitializerbled=true",
"--io.rsocket.broker.uri=tcp://localhost:" + brokerProxyPort, "--io.rsocket.broker.uri=tcp://localhost:" + brokerProxyPort,
"--io.rsocket.broker.cluster.uri=tcp://localhost:" + brokerClusterPort); "--io.rsocket.broker.cluster.uri=tcp://localhost:" + brokerClusterPort);

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2022-2022 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
*
* https://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.rsocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Random;
import javax.net.ServerSocketFactory;
/**
* Simple test utility to find a random available TCP port.
* <p>Inspired by the now removed {@code org.springframework.util.SocketUtils} and is only used in a testing capacity.
*
* @author Chris Bono
* @deprecated will soon be removed or consolidated - do not use further
*/
@Deprecated
public final class TestSocketUtils {
private static final Random random = new Random(System.nanoTime());
private TestSocketUtils() {
}
/**
* Find an available TCP port randomly selected from the range {@code 1024-65535}.
* @return an available TCP port number
* @throws IllegalStateException if no available port could be found
*/
public static int findAvailableTcpPort() {
int minPort = 1024;
int maxPort = 65535;
int portRange = maxPort - minPort;
int candidatePort;
int searchCounter = 0;
do {
if (searchCounter > portRange) {
throw new IllegalStateException(String.format(
"Could not find an available TCP port after %d attempts", searchCounter));
}
candidatePort = minPort + random.nextInt(portRange + 1);
searchCounter++;
}
while (!isPortAvailable(candidatePort));
return candidatePort;
}
private static boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
port, 1, InetAddress.getByName("localhost"));
serverSocket.close();
return true;
}
catch (Exception ex) {
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.cloud.function.web.function;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.time.Duration; import java.time.Duration;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
@@ -47,10 +48,12 @@ import org.springframework.cloud.function.web.util.FunctionWrapper;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SmartApplicationListener; import org.springframework.context.event.SmartApplicationListener;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.HttpHandler;
@@ -75,8 +78,8 @@ import static org.springframework.web.reactive.function.server.ServerResponse.st
/** /**
* @author Dave Syer * @author Dave Syer
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.0 * @since 2.0
*
*/ */
public class FunctionEndpointInitializer implements ApplicationContextInitializer<GenericApplicationContext> { public class FunctionEndpointInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
@@ -164,15 +167,19 @@ public class FunctionEndpointInitializer implements ApplicationContextInitialize
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host(address).port(port).handle(adapter); HttpServer httpServer = HttpServer.create().host(address).port(port).handle(adapter);
Thread thread = new Thread( Thread thread = new Thread(
() -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback), () -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), (server) -> callback(server, context)),
"server-startup"); "server-startup");
thread.setDaemon(false); thread.setDaemon(false);
thread.start(); thread.start();
} }
} }
private void callback(DisposableServer server) { private void callback(DisposableServer server, ApplicationContext context) {
logger.info("HTTP server started on port: " + server.port()); logger.info("HTTP server started on port: " + server.port());
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).getEnvironment().getPropertySources().addFirst(
new MapPropertySource("functionalServerProps", Collections.singletonMap("local.server.port", server.port())));
}
try { try {
double uptime = ManagementFactory.getRuntimeMXBean().getUptime(); double uptime = ManagementFactory.getRuntimeMXBean().getUptime();
logger.info("JVM running for " + uptime + "ms"); logger.info("JVM running for " + uptime + "ms");

View File

@@ -34,7 +34,7 @@ import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.context.test.FunctionalSpringBootTest; import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.cloud.function.test.FunctionalExporterTests.ApplicationConfiguration; import org.springframework.cloud.function.test.FunctionalExporterTests.ApplicationConfiguration;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.web.TestSocketUtils;
import org.springframework.cloud.function.web.source.SupplierExporter; import org.springframework.cloud.function.web.source.SupplierExporter;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@@ -70,7 +70,7 @@ public class FunctionalExporterTests {
@BeforeAll @BeforeAll
public static void init() throws Exception { public static void init() throws Exception {
headers.clear(); headers.clear();
String port = "" + SocketUtils.findAvailableTcpPort(); String port = "" + TestSocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port); System.setProperty("server.port", port);
System.setProperty("my.port", port); System.setProperty("my.port", port);
context = SpringApplication.run(RestPojoConfiguration.class, context = SpringApplication.run(RestPojoConfiguration.class,

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2022-2022 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
*
* https://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.web;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Random;
import javax.net.ServerSocketFactory;
/**
* Simple test utility to find a random available TCP port.
* <p>Inspired by the now removed {@code org.springframework.util.SocketUtils} and is only used in a testing capacity.
*
* @author Chris Bono
* @deprecated will soon be removed or consolidated - do not use further
*/
@Deprecated
public final class TestSocketUtils {
private static final Random random = new Random(System.nanoTime());
private TestSocketUtils() {
}
/**
* Find an available TCP port randomly selected from the range {@code 1024-65535}.
* @return an available TCP port number
* @throws IllegalStateException if no available port could be found
*/
public static int findAvailableTcpPort() {
int minPort = 1024;
int maxPort = 65535;
int portRange = maxPort - minPort;
int candidatePort;
int searchCounter = 0;
do {
if (searchCounter > portRange) {
throw new IllegalStateException(String.format(
"Could not find an available TCP port after %d attempts", searchCounter));
}
candidatePort = minPort + random.nextInt(portRange + 1);
searchCounter++;
}
while (!isPortAvailable(candidatePort));
return candidatePort;
}
private static boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
port, 1, InetAddress.getByName("localhost"));
serverSocket.close();
return true;
}
catch (Exception ex) {
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2019 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,14 +19,12 @@ package org.springframework.cloud.function.web.function;
import java.net.URI; import java.net.URI;
import java.util.function.Function; import java.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -35,27 +33,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1 * @since 2.1
*
*/ */
public class FunctionEndpointInitializerMVCTests { public class FunctionEndpointInitializerMVCTests {
@BeforeEach
public void init() throws Exception {
String port = "" + SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port);
}
@AfterEach
public void close() throws Exception {
System.clearProperty("server.port");
}
@Test @Test
public void testSingleFunctionMapping() throws Exception { public void testSingleFunctionMapping() throws Exception {
SpringApplication.run(ApplicationConfiguration.class); ConfigurableApplicationContext context = SpringApplication.run(ApplicationConfiguration.class, "--server.port=0");
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port"); String port = context.getEnvironment().getProperty("local.server.port");
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("STRESSED"); assertThat(response.getBody()).isEqualTo("STRESSED");
@@ -65,9 +52,9 @@ public class FunctionEndpointInitializerMVCTests {
@Test @Test
public void testCompositionFunctionMapping() throws Exception { public void testCompositionFunctionMapping() throws Exception {
SpringApplication.run(ApplicationConfiguration.class); ConfigurableApplicationContext context = SpringApplication.run(ApplicationConfiguration.class, "--server.port=0");
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port"); String port = context.getEnvironment().getProperty("local.server.port");
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("desserts"); assertThat(response.getBody()).isEqualTo("desserts");

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2021 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.SpringBootConfiguration;
@@ -32,6 +30,7 @@ import org.springframework.cloud.function.context.FunctionalSpringApplication;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.utils.SocketUtils;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@@ -42,28 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1 * @since 2.1
*
*/ */
public class FunctionEndpointInitializerTests { public class FunctionEndpointInitializerTests {
@BeforeEach
public void init() throws Exception {
String port = "" + SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port);
}
@AfterEach
public void close() throws Exception {
System.clearProperty("server.port");
}
@Test @Test
public void testNonExistingFunction() throws Exception { public void testNonExistingFunction() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class); int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/foo"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/foo"), "stressed", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
@@ -71,10 +57,8 @@ public class FunctionEndpointInitializerTests {
@Test @Test
public void testConsumerMapping() throws Exception { public void testConsumerMapping() throws Exception {
FunctionalSpringApplication.run(ConsumerConfiguration.class); int port = startServerAndWaitForPort(ConsumerConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isNull(); assertThat(response.getBody()).isNull();
@@ -83,10 +67,8 @@ public class FunctionEndpointInitializerTests {
@Test @Test
public void testSingleFunctionMapping() throws Exception { public void testSingleFunctionMapping() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class); int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("STRESSED"); assertThat(response.getBody()).isEqualTo("STRESSED");
@@ -96,10 +78,8 @@ public class FunctionEndpointInitializerTests {
@Test @Test
public void testCompositionFunctionMapping() throws Exception { public void testCompositionFunctionMapping() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class); int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class); .postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("desserts"); assertThat(response.getBody()).isEqualTo("desserts");
@@ -107,10 +87,8 @@ public class FunctionEndpointInitializerTests {
@Test @Test
public void testGetWithtFunction() throws Exception { public void testGetWithtFunction() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class); int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(2000);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.getForEntity(new URI("http://localhost:" + port + "/reverse/stressed"), String.class); .getForEntity(new URI("http://localhost:" + port + "/reverse/stressed"), String.class);
System.out.println(); System.out.println();
@@ -119,15 +97,25 @@ public class FunctionEndpointInitializerTests {
@Test @Test
public void testGetWithtSupplier() throws Exception { public void testGetWithtSupplier() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class); int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.getForEntity(new URI("http://localhost:" + port + "/supplier"), String.class); .getForEntity(new URI("http://localhost:" + port + "/supplier"), String.class);
assertThat(response.getBody()).isEqualTo("Jim Lahey"); assertThat(response.getBody()).isEqualTo("Jim Lahey");
} }
private int startServerAndWaitForPort(Class<?> primaryAppConfig) throws InterruptedException {
ConfigurableApplicationContext context = FunctionalSpringApplication.run(primaryAppConfig, "--server.port=0");
Thread.sleep(500);
String port = context.getEnvironment().getProperty("local.server.port");
if (port == null) {
Thread.sleep(500);
port = context.getEnvironment().getProperty("local.server.port");
assertThat(port).as("Unable to get 'local.server.port' - server may not have started up").isNotNull();
}
return Integer.valueOf(port);
}
@SpringBootConfiguration @SpringBootConfiguration
protected static class ConsumerConfiguration protected static class ConsumerConfiguration
implements ApplicationContextInitializer<GenericApplicationContext> { implements ApplicationContextInitializer<GenericApplicationContext> {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2019 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,14 +19,13 @@ package org.springframework.cloud.function.web.function;
import java.net.URI; import java.net.URI;
import java.util.function.Function; import java.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.utils.SocketUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -36,27 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1 * @since 2.1
*
*/ */
public class UserSubmittedTests { public class UserSubmittedTests {
@BeforeEach
public void init() throws Exception {
String port = "" + SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port);
}
@AfterEach
public void close() throws Exception {
System.clearProperty("server.port");
}
@Test @Test
public void testIssue274() throws Exception { public void testIssue274() throws Exception {
SpringApplication.run(Issue274Configuration.class); ConfigurableApplicationContext context = SpringApplication.run(Issue274Configuration.class, "--server.port=0");
int port = context.getEnvironment().getProperty("local.server.port", Integer.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200); Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/echo"), "", String.class); .postForEntity(new URI("http://localhost:" + port + "/echo"), "", String.class);
@@ -66,9 +54,9 @@ public class UserSubmittedTests {
@Test @Test
public void testIssue274WithData() throws Exception { public void testIssue274WithData() throws Exception {
SpringApplication.run(Issue274Configuration.class); ConfigurableApplicationContext context = SpringApplication.run(Issue274Configuration.class, "--server.port=0");
int port = context.getEnvironment().getProperty("local.server.port", Integer.class);
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200); Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/echo"), "hello", String.class); .postForEntity(new URI("http://localhost:" + port + "/echo"), "hello", String.class);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,8 +20,6 @@ import java.net.URI;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
@@ -44,27 +42,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* *
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* * @author Chris Bono
*/ */
public class MultipartFileTests { public class MultipartFileTests {
@BeforeEach
public void init() throws Exception {
String port = "" + SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port);
}
@AfterEach
public void close() throws Exception {
System.clearProperty("server.port");
}
@Test @Test
public void testMultipartFileUpload() throws Exception { public void testMultipartFileUpload() throws Exception {
ApplicationContext context = SpringApplication.run(TestConfiguration.class); ApplicationContext context = SpringApplication.run(TestConfiguration.class, "--server.port=0");
String port = context.getEnvironment().getProperty("local.server.port");
JsonMapper mapper = context.getBean(JsonMapper.class); JsonMapper mapper = context.getBean(JsonMapper.class);
TestRestTemplate template = new TestRestTemplate(); TestRestTemplate template = new TestRestTemplate();
String port = System.getProperty("server.port");
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new ClassPathResource("META-INF/spring.factories")); map.add("file", new ClassPathResource("META-INF/spring.factories"));
@@ -81,10 +68,10 @@ public class MultipartFileTests {
@Test @Test
public void testMultipartFilesUpload() throws Exception { public void testMultipartFilesUpload() throws Exception {
ApplicationContext context = SpringApplication.run(TestConfiguration.class); ApplicationContext context = SpringApplication.run(TestConfiguration.class, "--server.port=0");
String port = context.getEnvironment().getProperty("local.server.port");
JsonMapper mapper = context.getBean(JsonMapper.class); JsonMapper mapper = context.getBean(JsonMapper.class);
TestRestTemplate template = new TestRestTemplate(); TestRestTemplate template = new TestRestTemplate();
String port = System.getProperty("server.port");
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("fileA", new ClassPathResource("META-INF/spring.factories")); map.add("fileA", new ClassPathResource("META-INF/spring.factories"));

View File

@@ -34,7 +34,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.web.TestSocketUtils;
import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.ApplicationConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.ApplicationConfiguration;
import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.RestConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationIntegrationTests.RestConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -68,7 +68,7 @@ public class FunctionAutoConfigurationIntegrationTests {
@BeforeAll @BeforeAll
public static void init() { public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort()); System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
} }
@AfterAll @AfterAll

View File

@@ -32,7 +32,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.web.TestSocketUtils;
import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.ApplicationConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.ApplicationConfiguration;
import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.RestConfiguration; import org.springframework.cloud.function.web.source.FunctionAutoConfigurationWithRetriesIntegrationTests.RestConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -65,7 +65,7 @@ public class FunctionAutoConfigurationWithRetriesIntegrationTests {
@BeforeAll @BeforeAll
public static void init() { public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort()); System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
} }
@AfterAll @AfterAll

View File

@@ -36,6 +36,7 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.function.utils.SocketUtils; import org.springframework.cloud.function.utils.SocketUtils;
import org.springframework.cloud.function.web.RestApplication; import org.springframework.cloud.function.web.RestApplication;
import org.springframework.cloud.function.web.TestSocketUtils;
import org.springframework.cloud.function.web.source.WebAppIntegrationTests.ApplicationConfiguration; import org.springframework.cloud.function.web.source.WebAppIntegrationTests.ApplicationConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@@ -66,7 +67,7 @@ public class WebAppIntegrationTests {
@BeforeAll @BeforeAll
public static void init() { public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort()); System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
} }
@AfterAll @AfterAll