Elffuss BraidHub

kikocis/EDC public

test: avoid getFreePort collisions (#4366)

Changeset21e3aa7eaedaa6d41dd5b4b2c7338e72f8d19104d03b366c0bb8374aab7cd59d
Authorndr_brt <andrea.bertagnolli@gmail.com> · 2024-07-17 17:04 +02:00
CommitterGitHub <noreply@github.com> · 2024-07-17 17:04 +02:00
Parents2f3034f0fbdf

Browse the files at this changeset

2 files changed

modified core/common/lib/util-lib/src/main/java/org/eclipse/edc/util/io/Ports.java +23 −27
@@ -16,9 +16,9 @@
1616
1717 import java.io.IOException;
1818 import java.net.ServerSocket;
19+import java.util.HashSet;
1920 import java.util.Random;
20-
21-import static java.lang.String.format;
21+import java.util.Set;
2222
2323 /**
2424 * Utilities for assigning ports.
@@ -26,22 +26,22 @@
2626 public final class Ports {
2727 public static final int MAX_TCP_PORT = 65_535;
2828 private static final Random RANDOM = new Random();
29+ private static final Set<Integer> ALREADY_RETURNED = new HashSet<>();
2930
3031 /**
31- * Gets a free port in the range 1024 - 65535 by trying them in ascending order.
32+ * Gets a free port in the range 1024 - 65535 by trying them randomly.
3233 *
33- * @return the first free port
34+ * @return the lower bound.
3435 * @throws IllegalArgumentException if no free port is available
3536 */
3637 public static int getFreePort() {
37- var rnd = 1024 + RANDOM.nextInt(MAX_TCP_PORT - 1024);
38- return getFreePort(rnd);
38+ return getFreePort(1024);
3939 }
4040
4141 /**
42- * Gets a free port in the range lowerBound - 65535 by trying them in ascending order.
42+ * Gets a free port in the range lowerBound - 65535 by trying them randomly.
4343 *
44- * @return the first free port
44+ * @return the lower bound.
4545 * @throws IllegalArgumentException if no free port is available
4646 */
4747 public static int getFreePort(int lowerBound) {
@@ -52,7 +52,7 @@
5252 }
5353
5454 /**
55- * Gets a free port in the range lowerBound - upperBound by trying them in ascending order.
55+ * Gets a free port in the range lowerBound - upperBound randomly. Will not return ports already returned.
5656 *
5757 * @return the first free port
5858 * @throws IllegalArgumentException if no free port is available or if the bounds are invalid.
@@ -65,25 +65,21 @@
6565 if (upperBound > MAX_TCP_PORT) {
6666 throw new IllegalArgumentException("Upper bound must be < " + MAX_TCP_PORT);
6767 }
68- var port = lowerBound;
69- boolean found = false;
70-
71- while (!found && port <= upperBound) {
72- try (ServerSocket serverSocket = new ServerSocket(port)) {
73- serverSocket.setReuseAddress(true);
74- port = serverSocket.getLocalPort();
75-
76- found = true;
77- } catch (IOException e) {
78- found = false;
79- port++;
80- }
81- }
8268
83- if (!found) {
84- throw new IllegalArgumentException(format("No free ports in the range [%d - %d]", lowerBound, upperBound));
85- }
86- return port;
69+ do {
70+ var tryPort = lowerBound + RANDOM.nextInt(upperBound - lowerBound);
71+ if (!ALREADY_RETURNED.contains(tryPort)) {
72+ ALREADY_RETURNED.add(tryPort);
73+
74+ try (var serverSocket = new ServerSocket(tryPort)) {
75+ serverSocket.setReuseAddress(true);
76+
77+ return tryPort;
78+ } catch (IOException ignored) {
79+ // port already used by external service, try another one
80+ }
81+ }
82+ } while (true);
8783 }
8884
8985 private Ports() {
modified core/common/lib/util-lib/src/test/java/org/eclipse/edc/util/io/PortsTest.java +14 −2
@@ -19,6 +19,7 @@
1919 import java.io.IOException;
2020 import java.net.ServerSocket;
2121
22+import static java.util.stream.IntStream.range;
2223 import static org.assertj.core.api.Assertions.assertThat;
2324 import static org.assertj.core.api.Assertions.assertThatThrownBy;
2425
@@ -51,12 +52,23 @@
5152 assertThatThrownBy(() -> Ports.getFreePort(5000, 0)).isInstanceOf(IllegalArgumentException.class);
5253 }
5354
55+ @Test
56+ void getFreePortAvoidDuplicated() {
57+ var lowerBound = 6000;
58+ var portsToBeGenerated = 10;
59+ var ports = range(0, portsToBeGenerated)
60+ .mapToObj(i -> Ports.getFreePort(lowerBound, lowerBound + portsToBeGenerated))
61+ .distinct().toList();
62+
63+ assertThat(ports.size()).isEqualTo(portsToBeGenerated);
64+ }
65+
5466 @Test
5567 void getFreePort_whenOccupied() throws IOException {
5668 var port = Ports.getFreePort(MIN_PORT);
5769
5870 try (var socket = new ServerSocket(port)) {
59- assertThat(Ports.getFreePort(MIN_PORT)).describedAs("Next free port").isGreaterThan(socket.getLocalPort());
71+ assertThat(Ports.getFreePort(MIN_PORT)).describedAs("Next free port").isNotEqualTo(socket.getLocalPort());
6072 }
6173 }
62-}
No newline at the end of the file
74+}