Support identical minPort and maxPort in SocketUtils (#1612)
This commit fixes a bug where an IllegalStateException was thrown if the minPort and maxPort values supplied to SocketUtils.findAvailableTcpPort(int, int) were identical.
This commit is contained in:
parent
f9689dfe48
commit
9f36d170be
|
@ -258,7 +258,7 @@ public class SocketUtils {
|
||||||
int candidatePort;
|
int candidatePort;
|
||||||
int searchCounter = 0;
|
int searchCounter = 0;
|
||||||
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",
|
"Could not find an available %s port in the range [%d, %d] after %d attempts",
|
||||||
name(), minPort, maxPort, searchCounter));
|
name(), minPort, maxPort, searchCounter));
|
||||||
|
|
|
@ -53,6 +53,13 @@ public class SocketUtilsTests {
|
||||||
assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX);
|
assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findAvailableTcpPortWithMinPortEqualToMaxPort() {
|
||||||
|
int minMaxPort = SocketUtils.findAvailableTcpPort();
|
||||||
|
int port = SocketUtils.findAvailableTcpPort(minMaxPort, minMaxPort);
|
||||||
|
assertEquals(minMaxPort, port);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
|
||||||
int port = SocketUtils.findAvailableTcpPort();
|
int port = SocketUtils.findAvailableTcpPort();
|
||||||
|
|
Loading…
Reference in New Issue