Polish SocketUtilsTests
This commit is contained in:
parent
b766a49a4d
commit
743a96b75e
|
|
@ -50,14 +50,14 @@ class SocketUtilsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableTcpPortWithZeroMinPort() {
|
void findAvailableTcpPortWithZeroMinPort() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableTcpPort(0));
|
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableTcpPortWithNegativeMinPort() {
|
void findAvailableTcpPortWithNegativeMinPort() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableTcpPort(-500));
|
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(-500));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -80,8 +80,8 @@ class SocketUtilsTests {
|
||||||
try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"))) {
|
try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"))) {
|
||||||
assertThat(socket).isNotNull();
|
assertThat(socket).isNotNull();
|
||||||
// will only look for the exact port
|
// will only look for the exact port
|
||||||
assertThatIllegalStateException().isThrownBy(() ->
|
assertThatIllegalStateException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableTcpPort(port, port))
|
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(port, port))
|
||||||
.withMessageStartingWith("Could not find an available TCP port")
|
.withMessageStartingWith("Could not find an available TCP port")
|
||||||
.withMessageEndingWith("after 1 attempts");
|
.withMessageEndingWith("after 1 attempts");
|
||||||
}
|
}
|
||||||
|
|
@ -123,8 +123,7 @@ class SocketUtilsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(() -> findAvailableTcpPorts(50, 45000, 45010));
|
||||||
findAvailableTcpPorts(50, 45000, 45010));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -132,14 +131,14 @@ class SocketUtilsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableUdpPortWithZeroMinPort() {
|
void findAvailableUdpPortWithZeroMinPort() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableUdpPort(0));
|
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableUdpPortWithNegativeMinPort() {
|
void findAvailableUdpPortWithNegativeMinPort() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableUdpPort(-500));
|
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(-500));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -155,8 +154,8 @@ class SocketUtilsTests {
|
||||||
try (DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"))) {
|
try (DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"))) {
|
||||||
assertThat(socket).isNotNull();
|
assertThat(socket).isNotNull();
|
||||||
// will only look for the exact port
|
// will only look for the exact port
|
||||||
assertThatIllegalStateException().isThrownBy(() ->
|
assertThatIllegalStateException().isThrownBy(
|
||||||
org.springframework.util.SocketUtils.findAvailableUdpPort(port, port))
|
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(port, port))
|
||||||
.withMessageStartingWith("Could not find an available UDP port")
|
.withMessageStartingWith("Could not find an available UDP port")
|
||||||
.withMessageEndingWith("after 1 attempts");
|
.withMessageEndingWith("after 1 attempts");
|
||||||
}
|
}
|
||||||
|
|
@ -198,8 +197,7 @@ class SocketUtilsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException().isThrownBy(() -> findAvailableUdpPorts(50, 45000, 45010));
|
||||||
findAvailableUdpPorts(50, 45000, 45010));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -226,13 +224,13 @@ class SocketUtilsTests {
|
||||||
SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort);
|
SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort);
|
||||||
assertAvailablePorts(ports, numRequested, minPort, maxPort);
|
assertAvailablePorts(ports, numRequested, minPort, maxPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertPortInRange(int port, int minPort, int maxPort) {
|
private void assertPortInRange(int port, int minPort, int maxPort) {
|
||||||
assertThat(port >= minPort).as("port [" + port + "] >= " + minPort).isTrue();
|
assertThat(port).as("port").isBetween(minPort, maxPort);
|
||||||
assertThat(port <= maxPort).as("port [" + port + "] <= " + maxPort).isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
|
private void assertAvailablePorts(SortedSet<Integer> ports, int numRequested, int minPort, int maxPort) {
|
||||||
assertThat(ports.size()).as("number of ports requested").isEqualTo(numRequested);
|
assertThat(ports).as("number of ports requested").hasSize(numRequested);
|
||||||
for (int port : ports) {
|
for (int port : ports) {
|
||||||
assertPortInRange(port, minPort, maxPort);
|
assertPortInRange(port, minPort, maxPort);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue