Skip to content

Commit

Permalink
test: improve getFreePort implementation (#4373)
Browse files Browse the repository at this point in the history
* test: improve getFreePort implementation

* dependencies
  • Loading branch information
ndr-brt authored Jul 24, 2024
1 parent f9f4d18 commit 1ce4687
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Integer> CHECK_SERVER_SOCKET = port -> {
try (var serverSocket = new ServerSocket(port)) {
serverSocket.setReuseAddress(true);
return true;
} catch (IOException ignored) {
return false;
}
};

private static final Predicate<Integer> CHECK_DATAGRAM_SOCKET = port -> {
try (var datagramSocket = new DatagramSocket(port)) {
datagramSocket.setReuseAddress(true);
return true;
} catch (IOException ignored) {
return false;
}
};

private static final Predicate<Integer> CHECK_SOCKET = port -> {
try (var socket = new Socket("localhost", port)) {
socket.setReuseAddress(true);
return false;
} catch (IOException ignored) {
return true;
}
};

private static final Predicate<Integer> IS_PORT_AVAILABLE = CHECK_SERVER_SOCKET.and(CHECK_DATAGRAM_SOCKET).and(CHECK_SOCKET);

private Ports() {
}

Expand Down

0 comments on commit 1ce4687

Please sign in to comment.