From 1ce4687ac4fe97e676ea04a1518668238b632b34 Mon Sep 17 00:00:00 2001 From: ndr_brt Date: Wed, 24 Jul 2024 10:55:08 +0200 Subject: [PATCH] test: improve getFreePort implementation (#4373) * test: improve getFreePort implementation * dependencies --- DEPENDENCIES | 6 +-- .../java/org/eclipse/edc/util/io/Ports.java | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index 7286abbd5d1..ca60161753c 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -44,7 +44,7 @@ maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.2, Apache-2.0, approve maven/mavencentral/com.fasterxml.uuid/java-uuid-generator/4.1.0, Apache-2.0, approved, clearlydefined maven/mavencentral/com.github.cliftonlabs/json-simple/3.0.2, Apache-2.0, approved, clearlydefined maven/mavencentral/com.github.docker-java/docker-java-api/3.4.0, Apache-2.0, approved, clearlydefined -maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.4.0, Apache-2.0, restricted, clearlydefined +maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.4.0, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #15745 maven/mavencentral/com.github.docker-java/docker-java-transport/3.4.0, Apache-2.0, approved, clearlydefined maven/mavencentral/com.github.java-json-tools/btf/1.3, Apache-2.0 OR LGPL-3.0-only, approved, #15201 maven/mavencentral/com.github.java-json-tools/jackson-coreutils-equivalence/1.0, LGPL-3.0 OR Apache-2.0, approved, clearlydefined @@ -225,7 +225,7 @@ maven/mavencentral/org.apache.maven.doxia/doxia-module-xdoc/1.12.0, Apache-2.0, maven/mavencentral/org.apache.maven.doxia/doxia-sink-api/1.12.0, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apache.velocity.tools/velocity-tools-generic/3.1, Apache-2.0, approved, #9331 maven/mavencentral/org.apache.velocity/velocity-engine-core/2.3, Apache-2.0, approved, #2478 -maven/mavencentral/org.apache.velocity/velocity-engine-scripting/2.3, Apache-2.0, restricted, clearlydefined +maven/mavencentral/org.apache.velocity/velocity-engine-scripting/2.3, Apache-2.0 AND BSD-3-Clause, approved, #15744 maven/mavencentral/org.apache.xbean/xbean-reflect/3.7, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apiguardian/apiguardian-api/1.1.2, Apache-2.0, approved, clearlydefined maven/mavencentral/org.assertj/assertj-core/3.26.3, Apache-2.0, approved, #14886 @@ -349,7 +349,7 @@ maven/mavencentral/org.testcontainers/jdbc/1.20.0, MIT, approved, clearlydefined maven/mavencentral/org.testcontainers/junit-jupiter/1.20.0, MIT, approved, clearlydefined maven/mavencentral/org.testcontainers/kafka/1.20.0, MIT, approved, clearlydefined maven/mavencentral/org.testcontainers/postgresql/1.20.0, MIT, approved, clearlydefined -maven/mavencentral/org.testcontainers/testcontainers/1.20.0, MIT, restricted, clearlydefined +maven/mavencentral/org.testcontainers/testcontainers/1.20.0, None, restricted, #15747 maven/mavencentral/org.testcontainers/vault/1.20.0, MIT, approved, clearlydefined maven/mavencentral/org.xerial.snappy/snappy-java/1.1.10.5, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #9098 maven/mavencentral/org.xmlresolver/xmlresolver/5.2.2, Apache-2.0, approved, clearlydefined diff --git a/core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java b/core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java index 92c66f2b224..55cfd3c47b3 100644 --- a/core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java +++ b/core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java @@ -15,10 +15,13 @@ package org.eclipse.edc.util.io; import java.io.IOException; +import java.net.DatagramSocket; import java.net.ServerSocket; +import java.net.Socket; import java.util.HashSet; import java.util.Random; import java.util.Set; +import java.util.function.Predicate; /** * Utilities for assigning ports. @@ -67,21 +70,46 @@ public static int getFreePort(int lowerBound, int upperBound) { } do { - var tryPort = lowerBound + RANDOM.nextInt(upperBound - lowerBound); - if (!ALREADY_RETURNED.contains(tryPort)) { - ALREADY_RETURNED.add(tryPort); + var port = lowerBound + RANDOM.nextInt(upperBound - lowerBound); + if (!ALREADY_RETURNED.contains(port)) { + ALREADY_RETURNED.add(port); - try (var serverSocket = new ServerSocket(tryPort)) { - serverSocket.setReuseAddress(true); - - return tryPort; - } catch (IOException ignored) { - // port already used by external service, try another one + if (IS_PORT_AVAILABLE.test(port)) { + return port; } } } while (true); } + private static final Predicate CHECK_SERVER_SOCKET = port -> { + try (var serverSocket = new ServerSocket(port)) { + serverSocket.setReuseAddress(true); + return true; + } catch (IOException ignored) { + return false; + } + }; + + private static final Predicate CHECK_DATAGRAM_SOCKET = port -> { + try (var datagramSocket = new DatagramSocket(port)) { + datagramSocket.setReuseAddress(true); + return true; + } catch (IOException ignored) { + return false; + } + }; + + private static final Predicate CHECK_SOCKET = port -> { + try (var socket = new Socket("localhost", port)) { + socket.setReuseAddress(true); + return false; + } catch (IOException ignored) { + return true; + } + }; + + private static final Predicate IS_PORT_AVAILABLE = CHECK_SERVER_SOCKET.and(CHECK_DATAGRAM_SOCKET).and(CHECK_SOCKET); + private Ports() { }