From 753d2ef884530ce07b848a40ac6f37d9fd4d7568 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Thu, 8 Feb 2024 15:23:43 +0530 Subject: [PATCH] refactor (jkube-kit/common) : Refactor `IoUtil.getFreeRandomPort` to always generate a valid free random port (#2501) + Use ServerSocket to establish a connection instead of Socket which might also throw ConnectException for a socket already in use. + Modify IoUtilTes.findOpenPortWithSmallAttemptsCount to use `@RepeatedTest` annotation to run it 500 times Signed-off-by: Rohan Kumar --- .../org/eclipse/jkube/kit/common/util/IoUtil.java | 11 ++++------- .../org/eclipse/jkube/kit/common/util/IoUtilTest.java | 6 +++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java index 6a8c18dbce..6e31ce78e2 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/util/IoUtil.java @@ -17,8 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.ConnectException; -import java.net.Socket; +import java.net.ServerSocket; import java.net.URL; import java.nio.file.Files; import java.util.Random; @@ -128,12 +127,10 @@ public static int getFreeRandomPort() { public static int getFreeRandomPort(int min, int max, int attempts) { for (int i=0; i < attempts; i++) { int port = min + RANDOM.nextInt(max - min + 1); - try (Socket ignored = new Socket("localhost", port)) { // NOSONAR - // Port is open for communication, meaning it's used up, try again - } catch (ConnectException e) { + try (ServerSocket ignored = new ServerSocket(port)) { return port; - } catch (IOException e) { - throw new IllegalStateException("Error while trying to check open ports", e); + } catch (Exception e) { + // NOOP } } throw new IllegalStateException("Cannot find a free random port in the range [" + min + ", " + max + "] after " + attempts + " attempts"); diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java index da4ca67e6b..6b098639ec 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/IoUtilTest.java @@ -22,7 +22,10 @@ import org.eclipse.jkube.kit.common.TestHttpStaticServer; import org.eclipse.jkube.kit.common.assertj.FileAssertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnJre; +import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.api.io.TempDir; import static org.apache.commons.io.FilenameUtils.separatorsToSystem; @@ -64,7 +67,7 @@ void findOpenPortWhenPortsAreBusy() throws IOException { assertThat(port2).isGreaterThan(port); } - @Test + @RepeatedTest(500) void findOpenPortWithSmallAttemptsCount() throws IOException { int port = IoUtil.getFreeRandomPort(30000, 60000, 30); try (ServerSocket ss = new ServerSocket(port)) { @@ -81,6 +84,7 @@ void findOpenPortWithLargeAttemptsCount() throws IOException { } @Test + @DisabledOnJre(value = JRE.JAVA_8, disabledReason = "ServerSocket isn't throwing Bind exception for a port already in use on Eclipse CI with JDK8") void invokeExceptionWhenCouldntFindPort() throws IOException { // find an open port to occupy