This is an automated email from the ASF dual-hosted git repository.

apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit d65d539f0aa6f1d9b84e94bfe2b47065489117ec
Author: Urmila Unni <[email protected]>
AuthorDate: Mon Oct 5 14:52:03 2026 +0530

    CAMEL-24762: Move findUdpPort into a standalone helper
    
    Avoids leaking a reserved port from CoAPTestSupport's static PORT.
    
    Co-authored-by: Claude <[email protected]>
---
 .../camel/coap/CoAPComponentTLSTestBase.java       | 16 +++++-----
 .../camel/coap/CoAPRestComponentTestBase.java      |  2 +-
 .../{CoAPTestSupport.java => CoAPTestPorts.java}   | 33 +++++---------------
 .../org/apache/camel/coap/CoAPTestSupport.java     | 36 +---------------------
 4 files changed, 17 insertions(+), 70 deletions(-)

diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTestBase.java
 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTestBase.java
index 64fa7d3e3fb9..3902aca4e76c 100644
--- 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTestBase.java
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTestBase.java
@@ -52,21 +52,21 @@ import org.junit.jupiter.params.provider.ValueSource;
 abstract class CoAPComponentTLSTestBase extends CamelTestSupport {
 
     @RegisterExtension
-    static AvailablePortFinder.Port PORT = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT2 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT2 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT3 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT3 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT4 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT4 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT5 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT5 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT6 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT6 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT7 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT7 = CoAPTestPorts.findUdpPort();
     @RegisterExtension
-    static AvailablePortFinder.Port PORT8 = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port PORT8 = CoAPTestPorts.findUdpPort();
 
     @ParameterizedTest
     @ValueSource(strings = { "direct:start", "direct:selfsigned", 
/*"direct:clientauth",*/ "direct:ciphersuites" })
diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTestBase.java
 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTestBase.java
index 47565e487de5..7fb42ee1d932 100644
--- 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTestBase.java
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTestBase.java
@@ -39,7 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 
 abstract class CoAPRestComponentTestBase extends CamelTestSupport {
     @RegisterExtension
-    static AvailablePortFinder.Port coapport = CoAPTestSupport.findUdpPort();
+    static AvailablePortFinder.Port coapport = CoAPTestPorts.findUdpPort();
 
     @Produce("direct:start")
     protected ProducerTemplate sender;
diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
 b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestPorts.java
similarity index 64%
copy from 
components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
copy to 
components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestPorts.java
index 8808f5bc1de3..209b5abe0936 100644
--- 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestPorts.java
@@ -22,21 +22,16 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.camel.test.AvailablePortFinder;
-import org.apache.camel.test.junit6.CamelTestSupport;
-import org.apache.camel.util.FileUtil;
-import org.eclipse.californium.core.CoapClient;
-import org.eclipse.californium.elements.config.Configuration;
-import org.junit.jupiter.api.extension.RegisterExtension;
 
-public class CoAPTestSupport extends CamelTestSupport {
+/**
+ * Finds test ports that are free for UDP as well as TCP. {@link 
AvailablePortFinder} only probes TCP, but CoAP over UDP
+ * and DTLS binds a UDP socket on all interfaces, so a port that is free for 
TCP can still be taken for UDP.
+ */
+final class CoAPTestPorts {
 
-    @RegisterExtension
-    static AvailablePortFinder.Port PORT = findUdpPort();
+    private CoAPTestPorts() {
+    }
 
-    /**
-     * Finds a port that is free for UDP as well as TCP. {@link 
AvailablePortFinder} only probes TCP, but CoAP over UDP
-     * and DTLS binds a UDP socket on all interfaces, so a port that is free 
for TCP can still be taken for UDP.
-     */
     static AvailablePortFinder.Port findUdpPort() {
         List<AvailablePortFinder.Port> rejected = new ArrayList<>();
         try {
@@ -61,18 +56,4 @@ public class CoAPTestSupport extends CamelTestSupport {
             return false;
         }
     }
-
-    @Override
-    public void doPostSetup() {
-        Configuration.createStandardWithoutFile();
-    }
-
-    protected CoapClient createClient(String path) {
-        return createClient(path, PORT.getPort());
-    }
-
-    protected CoapClient createClient(String path, int port) {
-        String url = String.format("coap://localhost:%d/%s", port, 
FileUtil.stripLeadingSeparator(path));
-        return new CoapClient(url);
-    }
 }
diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
index 8808f5bc1de3..8f0d5a03d6a9 100644
--- 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPTestSupport.java
@@ -16,11 +16,6 @@
  */
 package org.apache.camel.coap;
 
-import java.net.DatagramSocket;
-import java.net.SocketException;
-import java.util.ArrayList;
-import java.util.List;
-
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.junit6.CamelTestSupport;
 import org.apache.camel.util.FileUtil;
@@ -31,36 +26,7 @@ import org.junit.jupiter.api.extension.RegisterExtension;
 public class CoAPTestSupport extends CamelTestSupport {
 
     @RegisterExtension
-    static AvailablePortFinder.Port PORT = findUdpPort();
-
-    /**
-     * Finds a port that is free for UDP as well as TCP. {@link 
AvailablePortFinder} only probes TCP, but CoAP over UDP
-     * and DTLS binds a UDP socket on all interfaces, so a port that is free 
for TCP can still be taken for UDP.
-     */
-    static AvailablePortFinder.Port findUdpPort() {
-        List<AvailablePortFinder.Port> rejected = new ArrayList<>();
-        try {
-            for (int i = 0; i < 100; i++) {
-                AvailablePortFinder.Port port = AvailablePortFinder.find();
-                if (isUdpPortFree(port.getPort())) {
-                    return port;
-                }
-                // keep the rejected port reserved until we are done, so 
find() does not return it again
-                rejected.add(port);
-            }
-            throw new IllegalStateException("Could not find a port that is 
free for both TCP and UDP");
-        } finally {
-            rejected.forEach(AvailablePortFinder.Port::release);
-        }
-    }
-
-    private static boolean isUdpPortFree(int port) {
-        try (DatagramSocket socket = new DatagramSocket(port)) {
-            return true;
-        } catch (SocketException e) {
-            return false;
-        }
-    }
+    static AvailablePortFinder.Port PORT = CoAPTestPorts.findUdpPort();
 
     @Override
     public void doPostSetup() {

Reply via email to