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


The following commit(s) were added to refs/heads/main by this push:
     new eb3a080eb501 CAMEL-27545: camel-servlet - fix flaky 
HttpClientRouteSpringTest BindException
eb3a080eb501 is described below

commit eb3a080eb50102ff5f3c807bdbe7cdb2075347e1
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Fri Nov 27 14:38:40 2026 +0100

    CAMEL-27545: camel-servlet - fix flaky HttpClientRouteSpringTest 
BindException
    
    ServletCamelRouterTestSupport reserved a port with 
AvailablePortFinder.find()
    when the test instance was created, long before Undertow bound it in
    setupResources(). AvailablePortFinder only probes a port and does not keep 
it
    bound, so another surefire fork or process could take it in between, and
    Undertow.start() failed with "java.net.BindException: Address already in 
use".
    
    Let Undertow bind port 0 so the OS picks a free port at bind time, and read 
the
    bound port back from the server listener info. This removes the window 
between
    finding the port and using it, and the AvailablePortFinder extension that 
is no
    longer needed.
    
    Co-authored-by: IBM Bob 2.2.1
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
---
 .../camel/component/servlet/ServletCamelRouterTestSupport.java | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
index 865dd1c1fc60..1bb648e7302d 100644
--- 
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
+++ 
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletCamelRouterTestSupport.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
 import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
@@ -36,33 +37,30 @@ import io.undertow.server.handlers.PathHandler;
 import io.undertow.servlet.Servlets;
 import io.undertow.servlet.api.DeploymentInfo;
 import io.undertow.servlet.api.DeploymentManager;
-import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.junit6.CamelTestSupport;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
-import org.junit.jupiter.api.extension.RegisterExtension;
 
 public class ServletCamelRouterTestSupport extends CamelTestSupport {
 
     public static final String CONTEXT = "/mycontext";
     protected String contextUrl;
-    @RegisterExtension
-    AvailablePortFinder.Port portHolder = AvailablePortFinder.find();
     protected int port;
     protected DeploymentManager manager;
     protected Undertow server;
 
     @Override
     public void setupResources() throws Exception {
-        port = portHolder.getPort();
         DeploymentInfo servletBuilder = getDeploymentInfo();
         manager = Servlets.newContainer().addDeployment(servletBuilder);
         manager.deploy();
         PathHandler path = Handlers.path(Handlers.redirect(CONTEXT))
                 .addPrefixPath(CONTEXT, manager.start());
-        server = Undertow.builder().addHttpListener(port, "localhost")
+        // let the OS pick a free port when binding, and read it back, to 
avoid a race with other tests
+        server = Undertow.builder().addHttpListener(0, "localhost")
                 .setHandler(path).build();
         server.start();
+        port = ((InetSocketAddress) 
server.getListenerInfo().get(0).getAddress()).getPort();
         contextUrl = "http://localhost:"; + port + CONTEXT;
 
         testConfiguration().withAutoStartContext(true);

Reply via email to