Fix minor logic error in SocketUtils

This commit is contained in:
Sam Brannen 2017-12-06 17:00:29 -08:00
parent 9f36d170be
commit 767ea9db83
2 changed files with 32 additions and 11 deletions

View File

@ -258,12 +258,13 @@ public class SocketUtils {
int candidatePort;
int searchCounter = 0;
do {
if (searchCounter++ > portRange) {
if (searchCounter > portRange) {
throw new IllegalStateException(String.format(
"Could not find an available %s port in the range [%d, %d] after %d attempts",
name(), minPort, maxPort, searchCounter));
}
candidatePort = findRandomPort(minPort, maxPort);
searchCounter++;
}
while (!isPortAvailable(candidatePort));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,10 +22,14 @@ import java.net.ServerSocket;
import java.util.SortedSet;
import javax.net.ServerSocketFactory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.util.SocketUtils.*;
import static org.springframework.util.SocketUtils.PORT_RANGE_MIN;
import static org.springframework.util.SocketUtils.PORT_RANGE_MAX;
/**
* Unit tests for {@link SocketUtils}.
@ -35,15 +39,20 @@ import static org.springframework.util.SocketUtils.*;
*/
public class SocketUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
// TCP
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableTcpPortWithZeroMinPort() {
exception.expect(IllegalArgumentException.class);
SocketUtils.findAvailableTcpPort(0);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableTcpPortWithNegativeMinPort() {
exception.expect(IllegalArgumentException.class);
SocketUtils.findAvailableTcpPort(-500);
}
@ -60,11 +69,15 @@ public class SocketUtilsTests {
assertEquals(minMaxPort, port);
}
@Test(expected = IllegalStateException.class)
@Test
public void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"));
try {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("Could not find an available TCP port"));
exception.expectMessage(endsWith("after 1 attempts"));
// will only look for the exact port
SocketUtils.findAvailableTcpPort(port, port);
}
finally {
@ -106,21 +119,24 @@ public class SocketUtilsTests {
findAvailableTcpPorts(50, 40000, 45000);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
exception.expect(IllegalArgumentException.class);
findAvailableTcpPorts(50, 45000, 45010);
}
// UDP
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableUdpPortWithZeroMinPort() {
exception.expect(IllegalArgumentException.class);
SocketUtils.findAvailableUdpPort(0);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableUdpPortWithNegativeMinPort() {
exception.expect(IllegalArgumentException.class);
SocketUtils.findAvailableUdpPort(-500);
}
@ -130,11 +146,14 @@ public class SocketUtilsTests {
assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX);
}
@Test(expected = IllegalStateException.class)
@Test
public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception {
int port = SocketUtils.findAvailableUdpPort();
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
try {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("Could not find an available UDP port"));
exception.expectMessage(endsWith("after 1 attempts"));
// will only look for the exact port
SocketUtils.findAvailableUdpPort(port, port);
}
@ -177,8 +196,9 @@ public class SocketUtilsTests {
findAvailableUdpPorts(50, 40000, 45000);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
exception.expect(IllegalArgumentException.class);
findAvailableUdpPorts(50, 45000, 45010);
}