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

davsclaus 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 c1049a15e8fc CAMEL-28074: camel-netty-http - shorter test time (#31381)
c1049a15e8fc is described below

commit c1049a15e8fc024335b33d43d6bbfe1820ea6f68
Author: Adriano Machado <[email protected]>
AuthorDate: Sat Dec 5 11:44:39 2026 -0500

    CAMEL-28074: camel-netty-http - shorter test time (#31381)
    
    The sum of the test class times goes from 100.6 s to 22.9 s on an idle
    machine (428 tests, same results).
    
    - BaseNettyTestSupport: the netty-http component uses event loops shared
      by all the tests, so a server stop no longer waits 200 ms. Tests that
      need their own event loops (workerCount, suspend closing the connection)
      keep them.
    - ProxyProtocolTest uses the shared event loops too, and its client sends
      Connection: close.
    - NettyHttpLoopErrorTest: a local route answers 404 instead of
      http://example.com, and the test checks the server gets exactly 3
      requests (CAMEL-17949); it fails after 30 s instead of hanging.
    - NettyHttpRequestTimeoutTest: the route waits on a latch instead of
      sleeping 3 s.
    - NettyHttpRedeliveryTest: the timer fires at once, and redeliveries are
      20 ms apart, with at least 30 s to start the server.
    
    _Claude Code on behalf of Adriano Machado (@ammachado)_
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../component/netty/http/BaseNettyTestSupport.java | 51 ++++++++++++++++++
 ...tyHttpChunkedRequestDisableStreamCacheTest.java |  6 +++
 .../netty/http/NettyHttpLoopErrorTest.java         | 62 ++++++++++++++--------
 .../netty/http/NettyHttpRedeliveryTest.java        |  6 ++-
 .../netty/http/NettyHttpRequestTimeoutTest.java    | 10 +++-
 .../netty/http/NettyHttpSuspendResumeTest.java     |  8 +++
 ...ttyHttpTwoRoutesBootstrapConfigurationTest.java |  6 +++
 .../component/netty/http/ProxyProtocolTest.java    |  7 +++
 8 files changed, 130 insertions(+), 26 deletions(-)

diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/BaseNettyTestSupport.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/BaseNettyTestSupport.java
index 5bb756208b68..19b426f505df 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/BaseNettyTestSupport.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/BaseNettyTestSupport.java
@@ -18,8 +18,14 @@ package org.apache.camel.component.netty.http;
 
 import java.util.Collection;
 import java.util.Properties;
+import java.util.concurrent.TimeUnit;
 
 import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.MultiThreadIoEventLoopGroup;
+import io.netty.channel.nio.NioIoHandler;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import io.netty.util.concurrent.Future;
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.test.AvailablePortFinder;
@@ -37,6 +43,16 @@ import org.slf4j.LoggerFactory;
 public class BaseNettyTestSupport extends CamelTestSupport {
     protected static final Logger LOG = 
LoggerFactory.getLogger(BaseNettyTestSupport.class);
 
+    // Event loops shared by all the tests of the JVM. A server stops the 
event loops it creates with the 2 s quiet
+    // period of Netty and waits up to shutdownTimeout (100 ms) for each of 
them, so every server stop would wait
+    // 200 ms. The server and the producer do not stop event loops they are 
given; the component stops a configured
+    // worker group, so these ignore shutdownGracefully and their daemon 
threads end with the JVM.
+    // A stopped netty-http server only closes its listening channel: the 
connections it accepted were closed by the
+    // shutdown of its own worker group. With the shared event loops they stay 
open, so a client must not reuse a
+    // connection after the server is stopped or suspended.
+    private static final EventLoopGroup SHARED_BOSS_GROUP = new 
SharedEventLoopGroup(1, "NettyTestSharedBoss");
+    private static final EventLoopGroup SHARED_WORKER_GROUP = new 
SharedEventLoopGroup(0, "NettyTestSharedWorker");
+
     @RegisterExtension
     AvailablePortFinder.Port port = AvailablePortFinder.find();
 
@@ -70,9 +86,31 @@ public class BaseNettyTestSupport extends CamelTestSupport {
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         context.getPropertiesComponent().setLocation("ref:prop");
+        if (isUseSharedEventLoopGroups()) {
+            useSharedEventLoopGroups(context);
+        }
         return context;
     }
 
+    /**
+     * Makes the netty-http component of the context use the event loops 
shared by all the tests.
+     */
+    static void useSharedEventLoopGroups(CamelContext context) {
+        NettyHttpConfiguration configuration = 
context.getComponent("netty-http", NettyHttpComponent.class)
+                .getConfiguration();
+        configuration.setBossGroup(SHARED_BOSS_GROUP);
+        configuration.setWorkerGroup(SHARED_WORKER_GROUP);
+    }
+
+    /**
+     * Whether the netty-http component uses event loops shared by all the 
tests, so that stopping a server does not
+     * wait for its event loops. Return false when the test needs the event 
loops of the endpoint options, such as
+     * workerCount.
+     */
+    protected boolean isUseSharedEventLoopGroups() {
+        return true;
+    }
+
     @BindToRegistry("prop")
     public Properties loadProp() {
 
@@ -86,4 +124,17 @@ public class BaseNettyTestSupport extends CamelTestSupport {
         return port.getPort();
     }
 
+    private static final class SharedEventLoopGroup extends 
MultiThreadIoEventLoopGroup {
+
+        SharedEventLoopGroup(int threads, String name) {
+            super(threads, new DefaultThreadFactory(name, true), 
NioIoHandler.newFactory());
+        }
+
+        @Override
+        public Future<?> shutdownGracefully(long quietPeriod, long timeout, 
TimeUnit unit) {
+            // shared by the tests: never shut down
+            return terminationFuture();
+        }
+    }
+
 }
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpChunkedRequestDisableStreamCacheTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpChunkedRequestDisableStreamCacheTest.java
index 8d8d549a389f..c017e4e6c68b 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpChunkedRequestDisableStreamCacheTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpChunkedRequestDisableStreamCacheTest.java
@@ -159,6 +159,12 @@ class NettyHttpChunkedRequestDisableStreamCacheTest 
extends BaseNettyTestSupport
         assertEquals("HTTP/1.1 200 OK", send(port2.getPort(), 
get("/hello")).status());
     }
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // the server must have one I/O thread (workerCount=1)
+        return false;
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpLoopErrorTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpLoopErrorTest.java
index e324384e60e5..819e65f0e13a 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpLoopErrorTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpLoopErrorTest.java
@@ -16,17 +16,27 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.LoggingLevel;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.direct.DirectEndpoint;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.http.base.HttpOperationFailedException;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
 
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
+// a regression of CAMEL-17949 redelivers forever: fail on a timeout instead 
of hanging
+@Timeout(value = 30, unit = TimeUnit.SECONDS)
 public class NettyHttpLoopErrorTest extends BaseNettyTestSupport {
 
     @EndpointInject("direct:input")
@@ -41,47 +51,53 @@ public class NettyHttpLoopErrorTest extends 
BaseNettyTestSupport {
                 onException(NettyHttpOperationFailedException.class)
                         .maximumRedeliveries(2)
                         .retryAttemptedLogLevel(LoggingLevel.WARN)
-                        .maximumRedeliveryDelay(1000)
+                        .redeliveryDelay(0)
                         .handled(false);
 
                 onException(HttpOperationFailedException.class)
                         .maximumRedeliveries(2)
                         .retryAttemptedLogLevel(LoggingLevel.WARN)
-                        .maximumRedeliveryDelay(1000)
+                        .redeliveryDelay(0)
                         .handled(false);
 
                 from("direct:input")
                         .routeId("test-route")
                         .toD("${body}");
+
+                // the server answers 404, so the producer fails and the 
exchange is redelivered
+                from("netty-http:http://localhost:{{port}}/return_404";)
+                        .to("mock:server")
+                        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404))
+                        .transform().constant("Not Found");
             }
         };
     }
 
     @Test
-    void testRetryWithExchangeInOnly() {
-        boolean isException = false;
-        try {
-            template.sendBody(directEndpoint, ExchangePattern.InOnly, 
"netty-http:http://example.com/return_404";);
-        } catch (Exception e) {
-            isException = true;
-        }
-
-        assertTrue(isException);
-
+    void testRetryWithExchangeInOnly() throws Exception {
+        assertRedeliveredTwiceThenFails(ExchangePattern.InOnly);
     }
 
     @Test
-    void testRetryWithExchangeInOut() {
-
-        boolean isException = false;
-        try {
-            template.sendBody(directEndpoint, ExchangePattern.InOut, 
"netty-http:http://example.com/return_404";);
-        } catch (Exception e) {
-            isException = true;
-        }
-
-        assertTrue(isException);
+    void testRetryWithExchangeInOut() throws Exception {
+        assertRedeliveredTwiceThenFails(ExchangePattern.InOut);
+    }
 
+    // CAMEL-17949: a failed response must not reset the redelivery counter, 
otherwise the exchange is redelivered
+    // forever. The netty-http fix keeps the Camel headers on the response, 
but since CAMEL-23469 the error handler
+    // also keeps its own counter, so this test fails only when both stop 
counting
+    private void assertRedeliveredTwiceThenFails(ExchangePattern pattern) 
throws Exception {
+        MockEndpoint server = getMockEndpoint("mock:server");
+        // the first attempt and 2 redeliveries
+        server.expectedMessageCount(3);
+
+        String url = "netty-http:http://localhost:"; + getPort() + 
"/return_404";
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBody(directEndpoint, pattern, url));
+        NettyHttpOperationFailedException cause = 
assertInstanceOf(NettyHttpOperationFailedException.class, e.getCause());
+        assertEquals(404, cause.getStatusCode());
+
+        MockEndpoint.assertIsSatisfied(context);
     }
 
 }
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRedeliveryTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRedeliveryTest.java
index fe3f3db6f017..f7e67d1d3489 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRedeliveryTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRedeliveryTest.java
@@ -52,8 +52,10 @@ public class NettyHttpRedeliveryTest extends 
BaseNettyTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() {
+                // 1500 redeliveries 20 ms apart: the producer keeps retrying 
for at least 30 seconds, long enough for
+                // the test to start the server after the 5th failure
                 onException(Exception.class)
-                        
.maximumRedeliveries(50).redeliveryDelay(100).onExceptionOccurred(
+                        
.maximumRedeliveries(1500).redeliveryDelay(20).onExceptionOccurred(
                                 new Processor() {
                                     @Override
                                     public void process(Exchange exchange) {
@@ -64,7 +66,7 @@ public class NettyHttpRedeliveryTest extends 
BaseNettyTestSupport {
                                     }
                                 });
 
-                from("timer:foo?repeatCount=1").routeId("foo")
+                from("timer:foo?repeatCount=1&delay=0").routeId("foo")
                         
.to("netty-http:http://0.0.0.0:{{port}}/bar?keepAlive=false&disconnect=true&connectTimeout=100ms";)
                         .to("mock:result");
 
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRequestTimeoutTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRequestTimeoutTest.java
index 1ec41da9ba5b..26734b850dcc 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRequestTimeoutTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpRequestTimeoutTest.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.netty.http;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 import io.netty.handler.timeout.ReadTimeoutException;
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
@@ -29,6 +32,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class NettyHttpRequestTimeoutTest extends BaseNettyTestSupport {
 
+    // the route holds the reply until the client has timed out and is 
released right after, so the context stop does
+    // not wait for an inflight exchange
+    private final CountDownLatch timedOut = new CountDownLatch(1);
+
     @Test
     public void testRequestTimeout() {
         CamelExecutionException e = assertThrows(CamelExecutionException.class,
@@ -37,6 +44,7 @@ public class NettyHttpRequestTimeoutTest extends 
BaseNettyTestSupport {
                 "Should have thrown exception");
         ReadTimeoutException cause = 
assertIsInstanceOf(ReadTimeoutException.class, e.getCause());
         assertNotNull(cause);
+        timedOut.countDown();
     }
 
     @Override
@@ -51,7 +59,7 @@ public class NettyHttpRequestTimeoutTest extends 
BaseNettyTestSupport {
                                 String body = 
exchange.getIn().getBody(String.class);
 
                                 if (body.contains("Camel")) {
-                                    Thread.sleep(3000);
+                                    timedOut.await(10, TimeUnit.SECONDS);
                                 }
                             }
                         })
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java
index 6718c7a3c00a..60b95fe5039b 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java
@@ -65,6 +65,14 @@ public class NettyHttpSuspendResumeTest extends 
BaseNettyTestSupport {
                 });
     }
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // suspending the consumer must close the connection of the first 
request (by stopping the event loops of the
+        // server); with the shared event loops the connection stays open and 
the next request gets a 404 instead of
+        // "Cannot connect"
+        return false;
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTwoRoutesBootstrapConfigurationTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTwoRoutesBootstrapConfigurationTest.java
index 62eadbe46402..49e260e28652 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTwoRoutesBootstrapConfigurationTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpTwoRoutesBootstrapConfigurationTest.java
@@ -43,6 +43,12 @@ public class NettyHttpTwoRoutesBootstrapConfigurationTest 
extends BaseNettyTestS
         return bootstrapConfiguration;
     }
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // the server must use the workerCount of the bootstrap configuration
+        return false;
+    }
+
     @Test
     public void testTwoRoutes() throws Exception {
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
diff --git 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
index 2ea5f04e55bc..7c2834a4038d 100644
--- 
a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
+++ 
b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ProxyProtocolTest.java
@@ -71,6 +71,7 @@ public class ProxyProtocolTest {
     public void createContext(final Function<RouteBuilder, RouteDefinition> 
variant, final String url) throws Exception {
         this.url = url;
         context = new DefaultCamelContext();
+        BaseNettyTestSupport.useSharedEventLoopGroups(context);
 
         context.addRoutes(new RouteBuilder() {
             @Override
@@ -236,6 +237,9 @@ public class ProxyProtocolTest {
         final Proxy proxy = new Proxy(Proxy.Type.HTTP, new 
InetSocketAddress("localhost", proxyPort.getPort()));
 
         final HttpURLConnection connection = (HttpURLConnection) new 
URL(url).openConnection(proxy);
+        // a new connection per request: the servers of the previous test were 
stopped, but a connection they accepted
+        // stays open on the shared event loops and would not be answered
+        connection.setRequestProperty("Connection", "close");
         // when debugging comment out the following two lines otherwise
         // the test will terminate regardless of the execution being
         // paused at the breakpoint
@@ -250,6 +254,9 @@ public class ProxyProtocolTest {
         final Proxy proxy = new Proxy(Proxy.Type.HTTP, new 
InetSocketAddress("localhost", proxyPort.getPort()));
 
         final HttpURLConnection connection = (HttpURLConnection) new 
URL(url).openConnection(proxy);
+        // a new connection per request: the servers of the previous test were 
stopped, but a connection they accepted
+        // stays open on the shared event loops and would not be answered
+        connection.setRequestProperty("Connection", "close");
         connection.addRequestProperty("Content-Type", contentType);
         connection.setDoOutput(true);
         // when debugging comment out the following two lines otherwise

Reply via email to