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 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");
* 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.time.Duration;
import java.util.Collections;
import java.util.Set;
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.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerCodecConfigurer;
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 Oleg Zhurakousky
* @author Chris Bono
* @since 2.0
*
*/
public class FunctionEndpointInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
@@ -164,15 +167,19 @@ public class FunctionEndpointInitializer implements ApplicationContextInitialize
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host(address).port(port).handle(adapter);
Thread thread = new Thread(
() -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback),
() -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), (server) -> callback(server, context)),
"server-startup");
thread.setDaemon(false);
thread.start();
}
}
private void callback(DisposableServer server) {
private void callback(DisposableServer server, ApplicationContext context) {
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 {
double uptime = ManagementFactory.getRuntimeMXBean().getUptime();
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.test.FunctionalSpringBootTest;
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.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
@@ -70,7 +70,7 @@ public class FunctionalExporterTests {
@BeforeAll
public static void init() throws Exception {
headers.clear();
String port = "" + SocketUtils.findAvailableTcpPort();
String port = "" + TestSocketUtils.findAvailableTcpPort();
System.setProperty("server.port", port);
System.setProperty("my.port", port);
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");
* 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.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.http.ResponseEntity;
@@ -35,27 +33,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1
*
*/
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
public void testSingleFunctionMapping() throws Exception {
SpringApplication.run(ApplicationConfiguration.class);
ConfigurableApplicationContext context = SpringApplication.run(ApplicationConfiguration.class, "--server.port=0");
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
String port = context.getEnvironment().getProperty("local.server.port");
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("STRESSED");
@@ -65,9 +52,9 @@ public class FunctionEndpointInitializerMVCTests {
@Test
public void testCompositionFunctionMapping() throws Exception {
SpringApplication.run(ApplicationConfiguration.class);
ConfigurableApplicationContext context = SpringApplication.run(ApplicationConfiguration.class, "--server.port=0");
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
String port = context.getEnvironment().getProperty("local.server.port");
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
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");
* 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.Supplier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.utils.SocketUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpStatus;
@@ -42,28 +41,15 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1
*
*/
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
public void testNonExistingFunction() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class);
int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/foo"), "stressed", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
@@ -71,10 +57,8 @@ public class FunctionEndpointInitializerTests {
@Test
public void testConsumerMapping() throws Exception {
FunctionalSpringApplication.run(ConsumerConfiguration.class);
int port = startServerAndWaitForPort(ConsumerConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isNull();
@@ -83,10 +67,8 @@ public class FunctionEndpointInitializerTests {
@Test
public void testSingleFunctionMapping() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class);
int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("STRESSED");
@@ -96,10 +78,8 @@ public class FunctionEndpointInitializerTests {
@Test
public void testCompositionFunctionMapping() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class);
int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
assertThat(response.getBody()).isEqualTo("desserts");
@@ -107,10 +87,8 @@ public class FunctionEndpointInitializerTests {
@Test
public void testGetWithtFunction() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class);
int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(2000);
ResponseEntity<String> response = testRestTemplate
.getForEntity(new URI("http://localhost:" + port + "/reverse/stressed"), String.class);
System.out.println();
@@ -119,15 +97,25 @@ public class FunctionEndpointInitializerTests {
@Test
public void testGetWithtSupplier() throws Exception {
FunctionalSpringApplication.run(ApplicationConfiguration.class);
int port = startServerAndWaitForPort(ApplicationConfiguration.class);
TestRestTemplate testRestTemplate = new TestRestTemplate();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.getForEntity(new URI("http://localhost:" + port + "/supplier"), String.class);
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
protected static class ConsumerConfiguration
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");
* 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.util.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -36,27 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Oleg Zhurakousky
* @author Chris Bono
* @since 2.1
*
*/
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
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();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.postForEntity(new URI("http://localhost:" + port + "/echo"), "", String.class);
@@ -66,9 +54,9 @@ public class UserSubmittedTests {
@Test
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();
String port = System.getProperty("server.port");
Thread.sleep(200);
ResponseEntity<String> response = testRestTemplate
.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");
* 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.function.Function;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
@@ -44,27 +42,16 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Oleg Zhurakousky
*
* @author Chris Bono
*/
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
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);
TestRestTemplate template = new TestRestTemplate();
String port = System.getProperty("server.port");
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new ClassPathResource("META-INF/spring.factories"));
@@ -81,10 +68,10 @@ public class MultipartFileTests {
@Test
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);
TestRestTemplate template = new TestRestTemplate();
String port = System.getProperty("server.port");
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
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.WebEnvironment;
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.RestConfiguration;
import org.springframework.context.annotation.Bean;
@@ -68,7 +68,7 @@ public class FunctionAutoConfigurationIntegrationTests {
@BeforeAll
public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort());
System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
}
@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.WebEnvironment;
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.RestConfiguration;
import org.springframework.context.annotation.Bean;
@@ -65,7 +65,7 @@ public class FunctionAutoConfigurationWithRetriesIntegrationTests {
@BeforeAll
public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort());
System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
}
@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.cloud.function.utils.SocketUtils;
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.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
@@ -66,7 +67,7 @@ public class WebAppIntegrationTests {
@BeforeAll
public static void init() {
System.setProperty("server.port", "" + SocketUtils.findAvailableTcpPort());
System.setProperty("server.port", "" + TestSocketUtils.findAvailableTcpPort());
}
@AfterAll