Skip to content

Commit bf7b475

Browse files
committed
SocketUtils considers port range including maxPort
Issue: SPR-14211
1 parent 9bf5a5c commit bf7b475

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

spring-core/src/main/java/org/springframework/util/SocketUtils.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -192,14 +192,14 @@ public static SortedSet<Integer> findAvailableUdpPorts(int numRequested, int min
192192
}
193193

194194

195-
private static enum SocketType {
195+
private enum SocketType {
196196

197197
TCP {
198198
@Override
199199
protected boolean isPortAvailable(int port) {
200200
try {
201-
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1,
202-
InetAddress.getByName("localhost"));
201+
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
202+
port, 1, InetAddress.getByName("localhost"));
203203
serverSocket.close();
204204
return true;
205205
}
@@ -238,7 +238,7 @@ protected boolean isPortAvailable(int port) {
238238
*/
239239
private int findRandomPort(int minPort, int maxPort) {
240240
int portRange = maxPort - minPort;
241-
return minPort + random.nextInt(portRange);
241+
return minPort + random.nextInt(portRange + 1);
242242
}
243243

244244
/**
@@ -251,7 +251,7 @@ private int findRandomPort(int minPort, int maxPort) {
251251
*/
252252
int findAvailablePort(int minPort, int maxPort) {
253253
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
254-
Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'");
254+
Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equals 'minPort'");
255255
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
256256

257257
int portRange = maxPort - minPort;
@@ -260,8 +260,8 @@ int findAvailablePort(int minPort, int maxPort) {
260260
do {
261261
if (++searchCounter > portRange) {
262262
throw new IllegalStateException(String.format(
263-
"Could not find an available %s port in the range [%d, %d] after %d attempts", name(), minPort,
264-
maxPort, searchCounter));
263+
"Could not find an available %s port in the range [%d, %d] after %d attempts",
264+
name(), minPort, maxPort, searchCounter));
265265
}
266266
candidatePort = findRandomPort(minPort, maxPort);
267267
}
@@ -285,18 +285,18 @@ SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort
285285
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
286286
Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
287287
Assert.isTrue((maxPort - minPort) >= numRequested,
288-
"'numRequested' must not be greater than 'maxPort' - 'minPort'");
288+
"'numRequested' must not be greater than 'maxPort' - 'minPort'");
289289

290-
final SortedSet<Integer> availablePorts = new TreeSet<Integer>();
290+
SortedSet<Integer> availablePorts = new TreeSet<Integer>();
291291
int attemptCount = 0;
292-
while ((++attemptCount <= numRequested + 100) && (availablePorts.size() < numRequested)) {
292+
while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
293293
availablePorts.add(findAvailablePort(minPort, maxPort));
294294
}
295295

296296
if (availablePorts.size() != numRequested) {
297297
throw new IllegalStateException(String.format(
298-
"Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort,
299-
maxPort));
298+
"Could not find %d available %s ports in the range [%d, %d]",
299+
numRequested, name(), minPort, maxPort));
300300
}
301301

302302
return availablePorts;

spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,19 +35,7 @@
3535
*/
3636
public class SocketUtilsTests {
3737

38-
private void assertPortInRange(int port, int minPort, int maxPort) {
39-
assertTrue("port [" + port + "] >= " + minPort, port >= minPort);
40-
assertTrue("port [" + port + "] <= " + maxPort, port <= maxPort);
41-
}
42-
43-
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
44-
assertEquals("number of ports requested", numRequested, ports.size());
45-
for (int port : ports) {
46-
assertPortInRange(port, minPort, maxPort);
47-
}
48-
}
49-
50-
// --- TCP -----------------------------------------------------------------
38+
// TCP
5139

5240
@Test(expected = IllegalArgumentException.class)
5341
public void findAvailableTcpPortWithZeroMinPort() {
@@ -70,8 +58,7 @@ public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
7058
int port = SocketUtils.findAvailableTcpPort();
7159
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"));
7260
try {
73-
// will only look for the exact port, since random.nextInt(1) always returns 0
74-
SocketUtils.findAvailableTcpPort(port, port + 1);
61+
SocketUtils.findAvailableTcpPort(port, port);
7562
}
7663
finally {
7764
socket.close();
@@ -117,17 +104,8 @@ public void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
117104
findAvailableTcpPorts(50, 45000, 45010);
118105
}
119106

120-
private void findAvailableTcpPorts(int numRequested) {
121-
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested);
122-
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
123-
}
124107

125-
private void findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
126-
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort);
127-
assertAvailablePorts(ports, numRequested, minPort, maxPort);
128-
}
129-
130-
// --- UDP -----------------------------------------------------------------
108+
// UDP
131109

132110
@Test(expected = IllegalArgumentException.class)
133111
public void findAvailableUdpPortWithZeroMinPort() {
@@ -150,8 +128,8 @@ public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() thro
150128
int port = SocketUtils.findAvailableUdpPort();
151129
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
152130
try {
153-
// will only look for the exact port, since random.nextInt(1) always returns 0
154-
SocketUtils.findAvailableUdpPort(port, port + 1);
131+
// will only look for the exact port
132+
SocketUtils.findAvailableUdpPort(port, port);
155133
}
156134
finally {
157135
socket.close();
@@ -197,6 +175,19 @@ public void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
197175
findAvailableUdpPorts(50, 45000, 45010);
198176
}
199177

178+
179+
// Helpers
180+
181+
private void findAvailableTcpPorts(int numRequested) {
182+
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested);
183+
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
184+
}
185+
186+
private void findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
187+
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort);
188+
assertAvailablePorts(ports, numRequested, minPort, maxPort);
189+
}
190+
200191
private void findAvailableUdpPorts(int numRequested) {
201192
SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested);
202193
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
@@ -206,5 +197,16 @@ private void findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
206197
SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort);
207198
assertAvailablePorts(ports, numRequested, minPort, maxPort);
208199
}
200+
private void assertPortInRange(int port, int minPort, int maxPort) {
201+
assertTrue("port [" + port + "] >= " + minPort, port >= minPort);
202+
assertTrue("port [" + port + "] <= " + maxPort, port <= maxPort);
203+
}
204+
205+
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
206+
assertEquals("number of ports requested", numRequested, ports.size());
207+
for (int port : ports) {
208+
assertPortInRange(port, minPort, maxPort);
209+
}
210+
}
209211

210212
}

0 commit comments

Comments
 (0)