GH-1038 GH-834 Remove copy of TestSocketUtils
Resolves #1038 Resolves #834
This commit is contained in:
@@ -33,6 +33,7 @@ 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;
|
||||||
import org.springframework.messaging.rsocket.RSocketRequester;
|
import org.springframework.messaging.rsocket.RSocketRequester;
|
||||||
|
import org.springframework.test.util.TestSocketUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -35,13 +35,13 @@ 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.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;
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.context.support.GenericApplicationContext;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.messaging.support.MessageBuilder;
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
|
import org.springframework.test.util.TestSocketUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -34,11 +34,11 @@ 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.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;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.util.TestSocketUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|||||||
@@ -32,11 +32,11 @@ 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.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;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.util.TestSocketUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|||||||
@@ -35,9 +35,9 @@ 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.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.test.util.TestSocketUtils;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|||||||
Reference in New Issue
Block a user