22
22
23
23
import javax .net .ServerSocketFactory ;
24
24
25
- import org .springframework .util .Assert ;
26
-
27
25
/**
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.
30
28
*
31
29
* <p>{@code SocketUtils} was removed from the public API in {@code spring-core}
32
30
* in Spring Framework 6.0 and reintroduced as {@code TestSocketUtils}, which is
47
45
* @author Arjen Poutsma
48
46
* @author Gunnar Hillert
49
47
* @author Gary Russell
50
- * @since 4 .0
48
+ * @since 6 .0
51
49
*/
52
50
public abstract class TestSocketUtils {
53
51
54
52
/**
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.
57
54
*/
58
55
private static final int PORT_RANGE_MIN = 1024 ;
59
56
60
57
/**
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.
63
59
*/
64
60
private static final int PORT_RANGE_MAX = 65535 ;
65
61
62
+ private static final int PORT_RANGE = PORT_RANGE_MAX - PORT_RANGE_MIN ;
63
+
64
+ private static final int MAX_ATTEMPTS = 1_000 ;
66
65
67
66
private static final Random random = new Random (System .nanoTime ());
68
67
69
68
70
69
/**
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].
73
71
* @return an available TCP port number
74
72
* @throws IllegalStateException if no available port could be found
75
73
*/
76
74
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 ;
94
75
int candidatePort ;
95
76
int searchCounter = 0 ;
96
77
do {
97
- if (searchCounter > portRange ) {
78
+ if (searchCounter > MAX_ATTEMPTS ) {
98
79
throw new IllegalStateException (String .format (
99
80
"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 ));
101
82
}
102
- candidatePort = findRandomPort ( minPort , maxPort );
83
+ candidatePort = PORT_RANGE_MIN + random . nextInt ( PORT_RANGE + 1 );
103
84
searchCounter ++;
104
85
}
105
86
while (!isPortAvailable (candidatePort ));
@@ -108,20 +89,7 @@ private static int findAvailablePort(int minPort, int maxPort) {
108
89
}
109
90
110
91
/**
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}.
125
93
*/
126
94
private static boolean isPortAvailable (int port ) {
127
95
try {
0 commit comments