Skip to content

Commit

Permalink
Polish SocketUtilsTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 14, 2022
1 parent b766a49 commit 743a96b
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ void canBeInstantiated() {

@Test
void findAvailableTcpPortWithZeroMinPort() {
assertThatIllegalArgumentException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableTcpPort(0));
assertThatIllegalArgumentException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(0));
}

@Test
void findAvailableTcpPortWithNegativeMinPort() {
assertThatIllegalArgumentException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableTcpPort(-500));
assertThatIllegalArgumentException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(-500));
}

@Test
Expand All @@ -80,8 +80,8 @@ void findAvailableTcpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exce
try (ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"))) {
assertThat(socket).isNotNull();
// will only look for the exact port
assertThatIllegalStateException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableTcpPort(port, port))
assertThatIllegalStateException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableTcpPort(port, port))
.withMessageStartingWith("Could not find an available TCP port")
.withMessageEndingWith("after 1 attempts");
}
Expand Down Expand Up @@ -123,23 +123,22 @@ void find50AvailableTcpPortsInRange() {

@Test
void findAvailableTcpPortsWithRequestedNumberGreaterThanSizeOfRange() {
assertThatIllegalArgumentException().isThrownBy(() ->
findAvailableTcpPorts(50, 45000, 45010));
assertThatIllegalArgumentException().isThrownBy(() -> findAvailableTcpPorts(50, 45000, 45010));
}


// UDP

@Test
void findAvailableUdpPortWithZeroMinPort() {
assertThatIllegalArgumentException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableUdpPort(0));
assertThatIllegalArgumentException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(0));
}

@Test
void findAvailableUdpPortWithNegativeMinPort() {
assertThatIllegalArgumentException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableUdpPort(-500));
assertThatIllegalArgumentException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(-500));
}

@Test
Expand All @@ -155,8 +154,8 @@ void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exce
try (DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"))) {
assertThat(socket).isNotNull();
// will only look for the exact port
assertThatIllegalStateException().isThrownBy(() ->
org.springframework.util.SocketUtils.findAvailableUdpPort(port, port))
assertThatIllegalStateException().isThrownBy(
() -> org.springframework.util.SocketUtils.findAvailableUdpPort(port, port))
.withMessageStartingWith("Could not find an available UDP port")
.withMessageEndingWith("after 1 attempts");
}
Expand Down Expand Up @@ -198,8 +197,7 @@ void find50AvailableUdpPortsInRange() {

@Test
void findAvailableUdpPortsWithRequestedNumberGreaterThanSizeOfRange() {
assertThatIllegalArgumentException().isThrownBy(() ->
findAvailableUdpPorts(50, 45000, 45010));
assertThatIllegalArgumentException().isThrownBy(() -> findAvailableUdpPorts(50, 45000, 45010));
}


Expand All @@ -226,13 +224,13 @@ private void findAvailableUdpPorts(int numRequested, int minPort, int maxPort) {
SortedSet<Integer> ports = org.springframework.util.SocketUtils.findAvailableUdpPorts(numRequested, minPort, maxPort);
assertAvailablePorts(ports, numRequested, minPort, maxPort);
}

private void assertPortInRange(int port, int minPort, int maxPort) {
assertThat(port >= minPort).as("port [" + port + "] >= " + minPort).isTrue();
assertThat(port <= maxPort).as("port [" + port + "] <= " + maxPort).isTrue();
assertThat(port).as("port").isBetween(minPort, 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) {
assertPortInRange(port, minPort, maxPort);
}
Expand Down

0 comments on commit 743a96b

Please sign in to comment.