Browse Source

SocketUtils considers port range including maxPort

Issue: SPR-14211
pull/1045/head
Juergen Hoeller 10 years ago
parent
commit
bf7b475267
  1. 26
      spring-core/src/main/java/org/springframework/util/SocketUtils.java
  2. 58
      spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -192,14 +192,14 @@ public class SocketUtils {
} }
private static enum SocketType { private enum SocketType {
TCP { TCP {
@Override @Override
protected boolean isPortAvailable(int port) { protected boolean isPortAvailable(int port) {
try { try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port, 1, ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
InetAddress.getByName("localhost")); port, 1, InetAddress.getByName("localhost"));
serverSocket.close(); serverSocket.close();
return true; return true;
} }
@ -238,7 +238,7 @@ public class SocketUtils {
*/ */
private int findRandomPort(int minPort, int maxPort) { private int findRandomPort(int minPort, int maxPort) {
int portRange = maxPort - minPort; int portRange = maxPort - minPort;
return minPort + random.nextInt(portRange); return minPort + random.nextInt(portRange + 1);
} }
/** /**
@ -251,7 +251,7 @@ public class SocketUtils {
*/ */
int findAvailablePort(int minPort, int maxPort) { int findAvailablePort(int minPort, int maxPort) {
Assert.isTrue(minPort > 0, "'minPort' must be greater than 0"); Assert.isTrue(minPort > 0, "'minPort' must be greater than 0");
Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'"); Assert.isTrue(maxPort >= minPort, "'maxPort' must be greater than or equals 'minPort'");
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX); Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
int portRange = maxPort - minPort; int portRange = maxPort - minPort;
@ -260,8 +260,8 @@ public class SocketUtils {
do { do {
if (++searchCounter > portRange) { if (++searchCounter > portRange) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"Could not find an available %s port in the range [%d, %d] after %d attempts", name(), minPort, "Could not find an available %s port in the range [%d, %d] after %d attempts",
maxPort, searchCounter)); name(), minPort, maxPort, searchCounter));
} }
candidatePort = findRandomPort(minPort, maxPort); candidatePort = findRandomPort(minPort, maxPort);
} }
@ -285,18 +285,18 @@ public class SocketUtils {
Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX); Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX);
Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0"); Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0");
Assert.isTrue((maxPort - minPort) >= numRequested, Assert.isTrue((maxPort - minPort) >= numRequested,
"'numRequested' must not be greater than 'maxPort' - 'minPort'"); "'numRequested' must not be greater than 'maxPort' - 'minPort'");
final SortedSet<Integer> availablePorts = new TreeSet<Integer>(); SortedSet<Integer> availablePorts = new TreeSet<Integer>();
int attemptCount = 0; int attemptCount = 0;
while ((++attemptCount <= numRequested + 100) && (availablePorts.size() < numRequested)) { while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) {
availablePorts.add(findAvailablePort(minPort, maxPort)); availablePorts.add(findAvailablePort(minPort, maxPort));
} }
if (availablePorts.size() != numRequested) { if (availablePorts.size() != numRequested) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort, "Could not find %d available %s ports in the range [%d, %d]",
maxPort)); numRequested, name(), minPort, maxPort));
} }
return availablePorts; return availablePorts;

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,19 +35,7 @@ import static org.springframework.util.SocketUtils.*;
*/ */
public class SocketUtilsTests { public class SocketUtilsTests {
private void assertPortInRange(int port, int minPort, int maxPort) { // TCP
assertTrue("port [" + port + "] >= " + minPort, port >= minPort);
assertTrue("port [" + port + "] <= " + maxPort, port <= maxPort);
}
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
assertEquals("number of ports requested", numRequested, ports.size());
for (int port : ports) {
assertPortInRange(port, minPort, maxPort);
}
}
// --- TCP -----------------------------------------------------------------
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void findAvailableTcpPortWithZeroMinPort() { public void findAvailableTcpPortWithZeroMinPort() {
@ -70,8 +58,7 @@ public class SocketUtilsTests {
int port = SocketUtils.findAvailableTcpPort(); int port = SocketUtils.findAvailableTcpPort();
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost")); ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"));
try { try {
// will only look for the exact port, since random.nextInt(1) always returns 0 SocketUtils.findAvailableTcpPort(port, port);
SocketUtils.findAvailableTcpPort(port, port + 1);
} }
finally { finally {
socket.close(); socket.close();
@ -117,17 +104,8 @@ public class SocketUtilsTests {
findAvailableTcpPorts(50, 45000, 45010); findAvailableTcpPorts(50, 45000, 45010);
} }
private void findAvailableTcpPorts(int numRequested) {
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested);
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
}
private void findAvailableTcpPorts(int numRequested, int minPort, int maxPort) { // UDP
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort);
assertAvailablePorts(ports, numRequested, minPort, maxPort);
}
// --- UDP -----------------------------------------------------------------
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void findAvailableUdpPortWithZeroMinPort() { public void findAvailableUdpPortWithZeroMinPort() {
@ -150,8 +128,8 @@ public class SocketUtilsTests {
int port = SocketUtils.findAvailableUdpPort(); int port = SocketUtils.findAvailableUdpPort();
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost")); DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
try { try {
// will only look for the exact port, since random.nextInt(1) always returns 0 // will only look for the exact port
SocketUtils.findAvailableUdpPort(port, port + 1); SocketUtils.findAvailableUdpPort(port, port);
} }
finally { finally {
socket.close(); socket.close();
@ -197,6 +175,19 @@ public class SocketUtilsTests {
findAvailableUdpPorts(50, 45000, 45010); findAvailableUdpPorts(50, 45000, 45010);
} }
// Helpers
private void findAvailableTcpPorts(int numRequested) {
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested);
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
}
private void findAvailableTcpPorts(int numRequested, int minPort, int maxPort) {
SortedSet<Integer> ports = SocketUtils.findAvailableTcpPorts(numRequested, minPort, maxPort);
assertAvailablePorts(ports, numRequested, minPort, maxPort);
}
private void findAvailableUdpPorts(int numRequested) { private void findAvailableUdpPorts(int numRequested) {
SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested); SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested);
assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX); assertAvailablePorts(ports, numRequested, PORT_RANGE_MIN, PORT_RANGE_MAX);
@ -206,5 +197,16 @@ public class SocketUtilsTests {
SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort); SortedSet<Integer> ports = SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort);
assertAvailablePorts(ports, numRequested, minPort, maxPort); assertAvailablePorts(ports, numRequested, minPort, maxPort);
} }
private void assertPortInRange(int port, int minPort, int maxPort) {
assertTrue("port [" + port + "] >= " + minPort, port >= minPort);
assertTrue("port [" + port + "] <= " + maxPort, port <= maxPort);
}
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
assertEquals("number of ports requested", numRequested, ports.size());
for (int port : ports) {
assertPortInRange(port, minPort, maxPort);
}
}
} }

Loading…
Cancel
Save