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 1955a66aaec0 CAMEL-28073: camel-netty - shorter test time (#31382)
1955a66aaec0 is described below

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

    CAMEL-28073: camel-netty - shorter test time (#31382)
    
    The tests spent most of their time waiting, not working. Locally the
    module goes from 77 s to 31 s (sum of the test methods 74 s -> 24.5 s,
    CPU 86 s -> 46 s), which matters most on the loaded 4 vCPU CI runner.
    
    - BaseNettyTest gives the netty component event loops shared by all the
      tests of the JVM, whose shutdownGracefully does nothing. Tests that need
      the event loops of the endpoint opt out with isUseSharedEventLoopGroups().
    - Event loop groups that tests own are shut down with no quiet period,
      after the CamelContext has stopped.
    - Seda consumers use pollTimeout=100.
    - Fixed waits are replaced by latches, delays or receive timeouts
      (NettyRequestTimeoutTest, NettyProducerSyncFalseReplyTest, the UDP send
      tests, NettyConsumerClientModeStopTest).
    - NettyUDPSyncTest keeps covering the UDP consumer and producer with event
      loops of their own.
    
    _Claude Code on behalf of Adriano Machado (@ammachado)_
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/netty/BaseNettyTest.java       | 43 ++++++++++++++++++
 .../netty/NettyConsumerClientModeStopTest.java     | 21 ++++++---
 .../netty/NettyCustomCorrelationManagerTest.java   |  4 +-
 .../component/netty/NettyInOutFromSedaTest.java    |  4 +-
 .../component/netty/NettyManualEndpointTest.java   |  7 +++
 .../netty/NettyProducerAsyncEndpointTest.java      |  3 +-
 .../netty/NettyProducerSyncFalseReplyTest.java     | 53 ++++++++++++++++++++--
 .../component/netty/NettyRequestTimeoutTest.java   | 25 +++++-----
 .../component/netty/NettySuspendResumeTest.java    |  7 +++
 .../camel/component/netty/NettyTCPChainedTest.java |  7 +++
 .../camel/component/netty/NettyTCPSyncUDSTest.java |  6 +++
 .../netty/NettyUDPByteArrayProviderTest.java       |  8 +++-
 .../camel/component/netty/NettyUDPSyncTest.java    |  6 +++
 .../component/netty/NettyUdpConnectedSendTest.java |  8 +++-
 .../netty/NettyUdpConnectionlessSendTest.java      |  8 +++-
 .../NettyUdpWithInOutUsingPlainSocketTest.java     |  3 +-
 ...ttyUseSharedWorkerThreadPoolManyRoutesTest.java | 13 +++++-
 .../netty/NettyUseSharedWorkerThreadPoolTest.java  | 13 +++++-
 18 files changed, 202 insertions(+), 37 deletions(-)

diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/BaseNettyTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/BaseNettyTest.java
index 66f93461a3f6..4cc2a8fbdbcf 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/BaseNettyTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/BaseNettyTest.java
@@ -18,9 +18,15 @@ package org.apache.camel.component.netty;
 
 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.ResourceLeakDetector;
+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;
@@ -39,6 +45,14 @@ public abstract class BaseNettyTest extends CamelTestSupport 
{
 
     private static final Logger LOG = 
LoggerFactory.getLogger(BaseNettyTest.class);
 
+    // Event loops shared by all the tests of the JVM. A TCP 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, and every producer and server (TCP, UDP and clientMode) would 
create its own event loops. The servers and
+    // the producers do not stop event loops they are given; the component 
stops a configured worker group, so the
+    // shared groups ignore shutdownGracefully and their daemon threads end 
with the JVM.
+    private static final EventLoopGroup SHARED_BOSS_GROUP = new 
SharedEventLoopGroup(1, "NettyTestSharedBoss");
+    private static final EventLoopGroup SHARED_WORKER_GROUP = new 
SharedEventLoopGroup(0, "NettyTestSharedWorker");
+
     @RegisterExtension
     protected AvailablePortFinder.Port port = AvailablePortFinder.find();
 
@@ -71,9 +85,24 @@ public abstract class BaseNettyTest extends CamelTestSupport 
{
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         context.getPropertiesComponent().setLocation("ref:prop");
+        if (isUseSharedEventLoopGroups()) {
+            NettyConfiguration configuration = context.getComponent("netty", 
NettyComponent.class).getConfiguration();
+            configuration.setBossGroup(SHARED_BOSS_GROUP);
+            configuration.setWorkerGroup(SHARED_WORKER_GROUP);
+        }
         return context;
     }
 
+    /**
+     * Whether the netty 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 or
+     * nativeTransport (which a configured group ignores), tests how an 
endpoint stops its own event loops, or registers
+     * a netty component of its own (createCamelContext has already created 
the default one).
+     */
+    protected boolean isUseSharedEventLoopGroups() {
+        return true;
+    }
+
     @BindToRegistry("prop")
     public Properties loadProperties() {
 
@@ -104,4 +133,18 @@ public abstract class BaseNettyTest extends 
CamelTestSupport {
         return data;
     }
 
+    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. The no-arg 
shutdownGracefully() delegates to this method. The
+            // returned future never completes, so do not wait on it
+            return terminationFuture();
+        }
+    }
+
 }
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyConsumerClientModeStopTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyConsumerClientModeStopTest.java
index 0e00533629f8..dc9dfe99fa1f 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyConsumerClientModeStopTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyConsumerClientModeStopTest.java
@@ -47,6 +47,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  */
 class NettyConsumerClientModeStopTest extends BaseNettyTest {
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // tests how a consumer stops its own worker group, and uses 
workerCount=1
+        return false;
+    }
+
     @BindToRegistry("sharedWorkerGroup")
     private final EventLoopGroup sharedWorkerGroup
             = new 
NettyWorkerPoolBuilder().withWorkerCount(2).withName("SharedClientWorker").build();
@@ -55,6 +61,9 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest {
     private final EventLoopGroup sharedSingleWorkerGroup
             = new 
NettyWorkerPoolBuilder().withWorkerCount(1).withName("SharedSingleClientWorker").build();
 
+    // how long a test waits to see that no connection is opened: at least 5 
reconnect intervals (100 ms or less here)
+    private static final long NO_CONNECTION_WINDOW_MILLIS = 500;
+
     // holds the event loop of the consumer once its reconnect has started to 
connect
     @BindToRegistry("holdReconnect")
     private final ServerInitializerFactory holdReconnect = new 
HoldReconnectInitializerFactory();
@@ -105,8 +114,8 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest 
{
         // the server drops the connection (if the stop did not close it)
         first.close().sync();
 
-        // ten reconnect intervals
-        assertNull(connections.poll(1, TimeUnit.SECONDS), "a connection opened 
after the stop");
+        // five reconnect intervals
+        assertNull(connections.poll(NO_CONNECTION_WINDOW_MILLIS, 
TimeUnit.MILLISECONDS), "a connection opened after the stop");
     }
 
     @Test
@@ -127,7 +136,7 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest 
{
             assertTrue(late.closeFuture().await(10, TimeUnit.SECONDS),
                     "the connection of the reconnect in progress at the stop 
is closed");
         }
-        assertNull(connections.poll(1, TimeUnit.SECONDS), "a connection opened 
after the stop");
+        assertNull(connections.poll(NO_CONNECTION_WINDOW_MILLIS, 
TimeUnit.MILLISECONDS), "a connection opened after the stop");
     }
 
     @Test
@@ -145,7 +154,7 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest 
{
         mock.expectedBodiesReceived("Hello");
         second.writeAndFlush("Hello\n");
         mock.assertIsSatisfied();
-        assertNull(connections.poll(1, TimeUnit.SECONDS), "a third 
connection");
+        assertNull(connections.poll(NO_CONNECTION_WINDOW_MILLIS, 
TimeUnit.MILLISECONDS), "a third connection");
     }
 
     @Test
@@ -183,7 +192,7 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest 
{
         second.writeAndFlush("World\n");
         mock.assertIsSatisfied();
         // one connection per consumer: no reconnect chain was duplicated
-        assertNull(connections.poll(1, TimeUnit.SECONDS), "a third 
connection");
+        assertNull(connections.poll(NO_CONNECTION_WINDOW_MILLIS, 
TimeUnit.MILLISECONDS), "a third connection");
     }
 
     @Test
@@ -211,7 +220,7 @@ class NettyConsumerClientModeStopTest extends BaseNettyTest 
{
         context.getRouteController().stopRoute("client");
 
         assertTrue(first.closeFuture().await(10, TimeUnit.SECONDS), "the 
connection of the stopped consumer is closed");
-        assertNull(connections.poll(1, TimeUnit.SECONDS), "a connection opened 
after the stop");
+        assertNull(connections.poll(NO_CONNECTION_WINDOW_MILLIS, 
TimeUnit.MILLISECONDS), "a connection opened after the stop");
     }
 
     private final class HoldReconnectInitializerFactory extends 
ServerInitializerFactory {
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCorrelationManagerTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCorrelationManagerTest.java
index b84e3520e5ef..246a3df789e4 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCorrelationManagerTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCorrelationManagerTest.java
@@ -54,7 +54,9 @@ public class NettyCustomCorrelationManagerTest extends 
BaseNettyTest {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                from("seda:start").log("before ${body}").to(
+                // a short poll timeout: the consumer sleeps up to pollTimeout 
while the CamelContext starts, and its stop
+                // waits for the poll in progress
+                from("seda:start?pollTimeout=100").log("before ${body}").to(
                         
"netty:tcp://localhost:{{port}}?textline=true&sync=true&producerPoolEnabled=false&correlationManager=#myManager")
                         .log("after ${body}").to("mock:result");
 
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyInOutFromSedaTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyInOutFromSedaTest.java
index dc574cbf3b14..5600408183c0 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyInOutFromSedaTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyInOutFromSedaTest.java
@@ -39,7 +39,9 @@ public class NettyInOutFromSedaTest extends BaseNettyTest {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                from("seda:start")
+                // a short poll timeout: the consumer sleeps up to pollTimeout 
while the CamelContext starts, and its stop
+                // waits for the poll in progress
+                from("seda:start?pollTimeout=100")
                         .log("before ${body}")
                         
.to("netty:tcp://localhost:{{port}}?textline=true&sync=true")
                         .log("after ${body}")
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyManualEndpointTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyManualEndpointTest.java
index d5d4a358b639..ccef0f63f724 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyManualEndpointTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyManualEndpointTest.java
@@ -31,6 +31,13 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 public class NettyManualEndpointTest extends BaseNettyTest {
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // registers its own "netty" component, which addComponent refuses 
once createCamelContext has created the
+        // default one
+        return false;
+    }
+
     private NettyEndpoint endpoint;
 
     @Test
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerAsyncEndpointTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerAsyncEndpointTest.java
index c074af430ad7..ef9119c369b9 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerAsyncEndpointTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerAsyncEndpointTest.java
@@ -68,7 +68,8 @@ public class NettyProducerAsyncEndpointTest extends 
BaseNettyTest {
                         .to("mock:result");
 
                 from("netty:tcp://localhost:{{port}}?textline=true&sync=true")
-                        .delay(1000)
+                        // a short delay; the producer continues routing on 
the netty I/O thread that reads the reply
+                        .delay(100)
                         .validate(body().isInstanceOf(String.class))
                         .transform(body().regexReplaceAll("Hello", "Bye"));
             }
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerSyncFalseReplyTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerSyncFalseReplyTest.java
index c4dade508389..2f3a2bdaad28 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerSyncFalseReplyTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyProducerSyncFalseReplyTest.java
@@ -16,14 +16,19 @@
  */
 package org.apache.camel.component.netty;
 
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import org.apache.camel.BindToRegistry;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
-import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * With sync=false the producer continues routing as soon as the request is 
written, so a reply that the remote server
@@ -31,6 +36,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
  */
 class NettyProducerSyncFalseReplyTest extends BaseNettyTest {
 
+    // counted down once the client pipeline has handled the reply of the 
server
+    private final CountDownLatch replyHandled = new CountDownLatch(1);
+
+    @BindToRegistry("replyHandledFactory")
+    private final ClientInitializerFactory replyHandledFactory = new 
ReplyHandledInitializerFactory();
+
     @Test
     void theReplyOfTheServerIsNotSetOnTheExchange() throws Exception {
         getMockEndpoint("mock:server").expectedBodiesReceived("Hello World");
@@ -61,12 +72,14 @@ class NettyProducerSyncFalseReplyTest extends BaseNettyTest 
{
             @Override
             public void configure() {
                 from("direct:start")
-                        .to("netty:tcp://localhost:{{port}}?sync=false")
+                        
.to("netty:tcp://localhost:{{port}}?sync=false&clientInitializerFactory=#replyHandledFactory")
                         // continue on another thread so the netty I/O thread 
is free to read the reply of the server
                         .threads(1)
-                        // the body must stay the request, also after the 
reply of the server has arrived
-                        .process(exchange -> await().during(1, 
TimeUnit.SECONDS).atMost(5, TimeUnit.SECONDS)
-                                .until(() -> "Hello 
World".equals(exchange.getMessage().getBody(String.class))))
+                        // the body must stay the request, also after the 
client has handled the reply of the server
+                        .process(exchange -> {
+                            assertTrue(replyHandled.await(5, 
TimeUnit.SECONDS), "the client should read the reply");
+                            assertEquals("Hello World", 
exchange.getMessage().getBody(String.class));
+                        })
                         .to("mock:result");
 
                 from("netty:tcp://localhost:{{port}}?sync=true")
@@ -75,4 +88,34 @@ class NettyProducerSyncFalseReplyTest extends BaseNettyTest {
             }
         };
     }
+
+    /**
+     * The default client pipeline, with a handler before the one of the 
producer that counts down {@link #replyHandled}
+     * once the producer has handled the reply.
+     */
+    private final class ReplyHandledInitializerFactory extends 
ClientInitializerFactory {
+
+        @Override
+        public ClientInitializerFactory createPipelineFactory(NettyProducer 
producer) {
+            return new DefaultClientInitializerFactory(producer) {
+                @Override
+                protected void initChannel(Channel ch) throws Exception {
+                    super.initChannel(ch);
+                    ch.pipeline().addBefore("handler", "replyHandled", new 
ChannelInboundHandlerAdapter() {
+                        @Override
+                        public void channelRead(ChannelHandlerContext ctx, 
Object msg) {
+                            // the handler of the producer handles the reply 
within this call
+                            ctx.fireChannelRead(msg);
+                            replyHandled.countDown();
+                        }
+                    });
+                }
+            };
+        }
+
+        @Override
+        protected void initChannel(Channel ch) {
+            throw new UnsupportedOperationException("only the factories 
created by createPipelineFactory are used");
+        }
+    }
 }
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutTest.java
index 417a2ae1d9b3..1ed184d2e63c 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutTest.java
@@ -16,21 +16,30 @@
  */
 package org.apache.camel.component.netty;
 
+import java.util.concurrent.TimeUnit;
+
 import io.netty.handler.timeout.ReadTimeoutException;
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf;
+import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class NettyRequestTimeoutTest extends BaseNettyTest {
 
+    @AfterEach
+    void awaitTheServer() {
+        // a timed out request leaves the server processing it; let it finish, 
otherwise the stop of the CamelContext
+        // waits for it in steps of 1 second
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
context.getInflightRepository().size() == 0);
+    }
+
     @Test
     public void testRequestTimeoutOK() {
         String out = 
template.requestBody("netty:tcp://localhost:{{port}}?textline=true&sync=true&requestTimeout=500",
@@ -95,16 +104,8 @@ public class NettyRequestTimeoutTest extends BaseNettyTest {
             @Override
             public void configure() {
                 from("netty:tcp://localhost:{{port}}?textline=true&sync=true")
-                        .process(new Processor() {
-                            @Override
-                            public void process(Exchange exchange) throws 
Exception {
-                                String body = 
exchange.getIn().getBody(String.class);
-
-                                if (body.contains("Camel")) {
-                                    Thread.sleep(200);
-                                }
-                            }
-                        })
+                        // a slow reply to the requests of the timeout tests
+                        .delay(simple("${iif(${body} contains 
'Camel',200,0)}"))
                         .transform().constant("Bye World");
 
             }
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettySuspendResumeTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettySuspendResumeTest.java
index af905bece81a..dc7d38da63c2 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettySuspendResumeTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettySuspendResumeTest.java
@@ -25,6 +25,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class NettySuspendResumeTest extends BaseNettyTest {
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // with the shared event loops, the request after the suspend gets no 
error when the whole module runs (it
+        // passes alone): the extra wait of stopping the event loops of the 
endpoint hides a race not yet explained
+        return false;
+    }
+
     @Test
     public void testSuspendResume() throws Exception {
         getMockEndpoint("mock:result").expectedBodiesReceived("Camel", 
"Again");
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPChainedTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPChainedTest.java
index e2b44152b09c..842529da7feb 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPChainedTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPChainedTest.java
@@ -40,6 +40,13 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
  */
 public class NettyTCPChainedTest extends BaseNettyTest {
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // uses workerCount=1, so both calls share one event loop (the case of 
the class javadoc); a configured
+        // workerGroup ignores workerCount
+        return false;
+    }
+
     @EndpointInject("mock:result")
     protected MockEndpoint resultEndpoint;
 
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPSyncUDSTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPSyncUDSTest.java
index 8feae2411f56..9c366dcd4732 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPSyncUDSTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTCPSyncUDSTest.java
@@ -37,6 +37,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
                           disabledReason = "Most CI environments use temporary 
build directories whose path is too long for the Netty native code leading to 
failure")
 public class NettyTCPSyncUDSTest extends BaseNettyTest {
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // uses nativeTransport, whose channels cannot use the shared NIO 
event loops
+        return false;
+    }
+
     @Test
     public void test() {
         String response = template.requestBody(
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPByteArrayProviderTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPByteArrayProviderTest.java
index bcdc77300f6c..bc71e3eec594 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPByteArrayProviderTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPByteArrayProviderTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.netty;
 
 import java.net.InetSocketAddress;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.Channel;
@@ -33,6 +34,7 @@ import io.netty.handler.codec.bytes.ByteArrayDecoder;
 import org.apache.camel.builder.RouteBuilder;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -65,7 +67,7 @@ public class NettyUDPByteArrayProviderTest extends 
BaseNettyTest {
     }
 
     public void stop() {
-        group.shutdownGracefully().syncUninterruptibly();
+        group.shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
     }
 
     @Test
@@ -75,8 +77,10 @@ public class NettyUDPByteArrayProviderTest extends 
BaseNettyTest {
         for (int i = 0; i < SEND_COUNT; ++i) {
             template.sendBody("direct:in", fromHexString(SEND_STRING));
         }
+        // the receiver reads the datagrams on its own event loop: wait for 
them before stopping it
+        await().atMost(10, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertTrue(receivedCount > 0, "We should 
have received some datagrams"));
         stop();
-        assertTrue(receivedCount > 0, "We should have received some 
datagrams");
     }
 
     @Override
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPSyncTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPSyncTest.java
index 0ee4247f0ea9..4a5499cb15a1 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPSyncTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUDPSyncTest.java
@@ -29,6 +29,12 @@ public class NettyUDPSyncTest extends BaseNettyTest {
     private static final String REQUEST
             = "After the Battle of Thermopylae in 480 BC - Simonides of Ceos 
(c. 556 BC-468 BC), Greek lyric poet wrote ";
 
+    @Override
+    protected boolean isUseSharedEventLoopGroups() {
+        // the UDP consumer and producer create and stop event loops of their 
own, which this test keeps covered
+        return false;
+    }
+
     @Test
     public void testUDPStringInOutWithNettyConsumer() {
         for (int i = 0; i < 5; i++) {
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectedSendTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectedSendTest.java
index 49edeaacb228..48b8e9a80bc9 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectedSendTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectedSendTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.netty;
 
 import java.net.InetSocketAddress;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.Channel;
@@ -36,6 +37,7 @@ import org.junit.jupiter.api.MethodOrderer;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestMethodOrder;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 //We need to run the tests with fix order
@@ -69,7 +71,7 @@ public class NettyUdpConnectedSendTest extends BaseNettyTest {
     public void stop() {
         // Just make sure we shutdown the listener after the call
         serverChannel.close().syncUninterruptibly();
-        group.shutdownGracefully().syncUninterruptibly();
+        group.shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
     }
 
     @Test
@@ -79,8 +81,10 @@ public class NettyUdpConnectedSendTest extends BaseNettyTest 
{
         for (int i = 0; i < SEND_COUNT; ++i) {
             template.sendBody("direct:in", SEND_STRING);
         }
+        // the receiver reads the datagrams on its own event loop: wait for 
them before stopping it
+        await().atMost(10, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertTrue(receivedCount > 0, "We should 
have received some datagrams"));
         stop();
-        assertTrue(receivedCount > 0, "We should have received some 
datagrams");
     }
 
     @Test
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectionlessSendTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectionlessSendTest.java
index 3409f97d550f..d60ba5235127 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectionlessSendTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpConnectionlessSendTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.netty;
 
 import java.net.InetSocketAddress;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.Channel;
@@ -33,6 +34,7 @@ import io.netty.util.CharsetUtil;
 import org.apache.camel.builder.RouteBuilder;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -62,7 +64,7 @@ public class NettyUdpConnectionlessSendTest extends 
BaseNettyTest {
     }
 
     public void stop() {
-        group.shutdownGracefully().syncUninterruptibly();
+        group.shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
     }
 
     @Test
@@ -72,8 +74,10 @@ public class NettyUdpConnectionlessSendTest extends 
BaseNettyTest {
         for (int i = 0; i < SEND_COUNT; ++i) {
             template.sendBody("direct:in", SEND_STRING);
         }
+        // the receiver reads the datagrams on its own event loop: wait for 
them before stopping it
+        await().atMost(10, TimeUnit.SECONDS)
+                .untilAsserted(() -> assertTrue(receivedCount > 0, "We should 
have received some datagrams"));
         stop();
-        assertTrue(receivedCount > 0, "We should have received some 
datagrams");
 
     }
 
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpWithInOutUsingPlainSocketTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpWithInOutUsingPlainSocketTest.java
index 3ad6589b0f15..3f965edfabb5 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpWithInOutUsingPlainSocketTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUdpWithInOutUsingPlainSocketTest.java
@@ -51,7 +51,8 @@ public class NettyUdpWithInOutUsingPlainSocketTest extends 
BaseNettyTest {
         LOG.debug("+++ Sending data +++");
         socket.send(packet);
 
-        Thread.sleep(1000);
+        // receive blocks until the reply arrives; the timeout only keeps a 
lost reply from hanging the test
+        socket.setSoTimeout(10000);
 
         byte[] buf = new byte[128];
         DatagramPacket receive = new DatagramPacket(buf, buf.length, address, 
getPort());
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolManyRoutesTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolManyRoutesTest.java
index cd7d5208016d..43be19e96953 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolManyRoutesTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolManyRoutesTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.netty;
 
+import java.util.concurrent.TimeUnit;
+
 import io.netty.channel.EventLoopGroup;
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.builder.RouteBuilder;
@@ -64,9 +66,16 @@ public class NettyUseSharedWorkerThreadPoolManyRoutesTest 
extends BaseNettyTest
 
         LOG.info("Created threads {}", delta);
         assertTrue(delta < 50, "There should not be created so many threads: " 
+ delta);
+    }
 
-        sharedBoosGroup.shutdownGracefully().awaitUninterruptibly();
-        sharedWorkerGroup.shutdownGracefully().awaitUninterruptibly();
+    @Override
+    protected void cleanupResources() throws Exception {
+        // cleanupResources runs before the CamelContext is stopped: stop it 
first, as its consumers use these event
+        // loops until then (the later stop of the context does nothing)
+        context.stop();
+        sharedBoosGroup.shutdownGracefully(0, 1, 
TimeUnit.SECONDS).awaitUninterruptibly();
+        sharedWorkerGroup.shutdownGracefully(0, 1, 
TimeUnit.SECONDS).awaitUninterruptibly();
+        super.cleanupResources();
     }
 
     @Override
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolTest.java
index 186ad4b2c74f..29a4afcd0461 100644
--- 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolTest.java
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyUseSharedWorkerThreadPoolTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.netty;
 
+import java.util.concurrent.TimeUnit;
+
 import io.netty.channel.EventLoopGroup;
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.builder.RouteBuilder;
@@ -69,9 +71,16 @@ public class NettyUseSharedWorkerThreadPoolTest extends 
BaseNettyTest {
         }
 
         MockEndpoint.assertIsSatisfied(context);
+    }
 
-        sharedWorkerServerGroup.shutdownGracefully().sync().await();
-        sharedWorkerClientGroup.shutdownGracefully().sync().await();
+    @Override
+    protected void cleanupResources() throws Exception {
+        // cleanupResources runs before the CamelContext is stopped: stop it 
first, as its consumers and producers use
+        // these event loops until then (the later stop of the context does 
nothing)
+        context.stop();
+        sharedWorkerServerGroup.shutdownGracefully(0, 1, 
TimeUnit.SECONDS).sync();
+        sharedWorkerClientGroup.shutdownGracefully(0, 1, 
TimeUnit.SECONDS).sync();
+        super.cleanupResources();
     }
 
     @Override

Reply via email to