Skip to content

Commit 92d9aee

Browse files
committed
Polish TestSocketUtils
1 parent 389747f commit 92d9aee

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

spring-core/src/testFixtures/java/org/springframework/core/testfixture/net/TestSocketUtils.java

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222

2323
import javax.net.ServerSocketFactory;
2424

25-
import org.springframework.util.Assert;
26-
2725
/**
28-
* Simple utility methods for finding available ports on {@code localhost} for
29-
* use in integration testing scenarios.
26+
* Simple utility for finding available TCP ports on {@code localhost} for use in
27+
* integration testing scenarios.
3028
*
3129
* <p>{@code SocketUtils} was removed from the public API in {@code spring-core}
3230
* in Spring Framework 6.0 and reintroduced as {@code TestSocketUtils}, which is
@@ -47,59 +45,42 @@
4745
* @author Arjen Poutsma
4846
* @author Gunnar Hillert
4947
* @author Gary Russell
50-
* @since 4.0
48+
* @since 6.0
5149
*/
5250
public abstract class TestSocketUtils {
5351

5452
/**
55-
* The default minimum value for port ranges used when finding an available
56-
* socket port.
53+
* The minimum value for port ranges used when finding an available TCP port.
5754
*/
5855
private static final int PORT_RANGE_MIN = 1024;
5956

6057
/**
61-
* The default maximum value for port ranges used when finding an available
62-
* socket port.
58+
* The maximum value for port ranges used when finding an available TCP port.
6359
*/
6460
private static final int PORT_RANGE_MAX = 65535;
6561

62+
private static final int PORT_RANGE = PORT_RANGE_MAX - PORT_RANGE_MIN;
63+
64+
private static final int MAX_ATTEMPTS = 1_000;
6665

6766
private static final Random random = new Random(System.nanoTime());
6867

6968

7069
/**
71-
* Find an available TCP port randomly selected from the range
72-
* [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}].
70+
* Find an available TCP port randomly selected from the range [1024, 65535].
7371
* @return an available TCP port number
7472
* @throws IllegalStateException if no available port could be found
7573
*/
7674
public static int findAvailableTcpPort() {
77-
return findAvailablePort(PORT_RANGE_MIN, PORT_RANGE_MAX);
78-
}
79-
80-
/**
81-
* Find an available port for this {@code SocketType}, randomly selected
82-
* from the range [{@code minPort}, {@code maxPort}].
83-
* @param minPort the minimum port number
84-
* @param maxPort the maximum port number
85-
* @return an available port number for this socket type
86-
* @throws IllegalStateException if no available port could be found
87-
*/
88-
private static int findAvailablePort(int minPort, int maxPort) {
89-
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
90-
Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equal to 'minPort'");
91-
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
92-
93-
int portRange = maxPort - minPort;
9475
int candidatePort;
9576
int searchCounter = 0;
9677
do {
97-
if (searchCounter > portRange) {
78+
if (searchCounter > MAX_ATTEMPTS) {
9879
throw new IllegalStateException(String.format(
9980
"Could not find an available TCP port in the range [%d, %d] after %d attempts",
100-
minPort, maxPort, searchCounter));
81+
PORT_RANGE_MIN, PORT_RANGE_MAX, MAX_ATTEMPTS));
10182
}
102-
candidatePort = findRandomPort(minPort, maxPort);
83+
candidatePort = PORT_RANGE_MIN + random.nextInt(PORT_RANGE + 1);
10384
searchCounter++;
10485
}
10586
while (!isPortAvailable(candidatePort));
@@ -108,20 +89,7 @@ private static int findAvailablePort(int minPort, int maxPort) {
10889
}
10990

11091
/**
111-
* Find a pseudo-random port number within the range
112-
* [{@code minPort}, {@code maxPort}].
113-
* @param minPort the minimum port number
114-
* @param maxPort the maximum port number
115-
* @return a random port number within the specified range
116-
*/
117-
private static int findRandomPort(int minPort, int maxPort) {
118-
int portRange = maxPort - minPort;
119-
return minPort + random.nextInt(portRange + 1);
120-
}
121-
122-
/**
123-
* Determine if the specified port for this {@code SocketType} is
124-
* currently available on {@code localhost}.
92+
* Determine if the specified TCP port is currently available on {@code localhost}.
12593
*/
12694
private static boolean isPortAvailable(int port) {
12795
try {

0 commit comments

Comments
 (0)