From 767ea9db8370945a364b94d770b81d05d0209aef Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 6 Dec 2017 17:00:29 -0800 Subject: [PATCH] Fix minor logic error in SocketUtils --- .../org/springframework/util/SocketUtils.java | 3 +- .../util/SocketUtilsTests.java | 40 ++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/SocketUtils.java b/spring-core/src/main/java/org/springframework/util/SocketUtils.java index 5d8df68a67..b57c414108 100644 --- a/spring-core/src/main/java/org/springframework/util/SocketUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SocketUtils.java @@ -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)); diff --git a/spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java b/spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java index b2a45d8a20..17e1da3da6 100644 --- a/spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/SocketUtilsTests.java @@ -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); }