gnodet-bot commented on code in PR #26798:
URL: https://github.com/apache/camel/pull/26798#discussion_r4094399888


##########
core/camel-core/src/test/java/org/apache/camel/component/seda/ThreadPerTaskSedaConsumerStopTest.java:
##########
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.seda;
+
+import java.util.List;
+import java.util.concurrent.AbstractExecutorService;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.DefaultThreadPoolFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A graceful stop of a virtualThreadPerTask seda consumer must wait for an 
exchange that has been polled and
+ * dispatched, but whose task has not started running yet.
+ */
+class ThreadPerTaskSedaConsumerStopTest extends ContextTestSupport {
+
+    private final CountDownLatch dispatched = new CountDownLatch(1);
+    private final CountDownLatch startTask = new CountDownLatch(1);
+    private final CountDownLatch awaitingTermination = new CountDownLatch(1);
+
+    /**
+     * Task executor which delays the start of the tasks, until the test lets 
them start.
+     */
+    private final class DelayedStartExecutorService extends 
AbstractExecutorService {
+        private final ExecutorService delegate;
+
+        DelayedStartExecutorService(ExecutorService delegate) {
+            this.delegate = delegate;
+        }
+
+        @Override
+        public void execute(Runnable task) {
+            delegate.execute(() -> {
+                try {
+                    startTask.await(20, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }
+                task.run();
+            });
+            dispatched.countDown();
+        }
+
+        @Override
+        public boolean awaitTermination(long timeout, TimeUnit unit) throws 
InterruptedException {
+            awaitingTermination.countDown();
+            return delegate.awaitTermination(timeout, unit);
+        }
+
+        @Override
+        public void shutdown() {
+            delegate.shutdown();
+        }
+
+        @Override
+        public List<Runnable> shutdownNow() {
+            return delegate.shutdownNow();
+        }
+
+        @Override
+        public boolean isShutdown() {
+            return delegate.isShutdown();
+        }
+
+        @Override
+        public boolean isTerminated() {
+            return delegate.isTerminated();
+        }
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getExecutorServiceManager().setThreadPoolFactory(new 
DefaultThreadPoolFactory() {
+            @Override
+            public ExecutorService newCachedThreadPool(ThreadFactory 
threadFactory) {
+                ExecutorService answer = 
super.newCachedThreadPool(threadFactory);
+                // the task executor of the thread-per-task seda consumer (its 
coordinator is a single thread executor)
+                if (threadFactory.newThread(() -> {
+                }).getName().endsWith("seda://v")) {
+                    answer = new DelayedStartExecutorService(answer);

Review Comment:
   ⚠️ **Wrong thread-name suffix — `DelayedStartExecutorService` is never 
installed.**
   
   `ThreadPerTaskSedaConsumer.setupTasks()` creates the task executor with:
   ```java
   newCachedThreadPool(this, getEndpoint().getEndpointUri() + "-task")
   ```
   For the route `seda:v`, the endpoint URI is `seda://v`, so the name passed 
to `CamelThreadFactory` is **`seda://v-task`**. The default Camel thread-name 
pattern then resolves to `"Camel Thread #N - seda://v-task"`. That string does 
**not** end with `"seda://v"` — the check is always false, the mock executor is 
silently skipped, and the test asserts on a route that uses the real unbounded 
executor, so `dispatched` is never signalled within 10 s and 
`assertTrue(dispatched.await(10, TimeUnit.SECONDS))` fails.
   
   Fix: match the actual suffix:
   ```suggestion
                   if (threadFactory.newThread(() -> {
                   }).getName().endsWith("seda://v-task")) {
   ```
   
   Also: calling `threadFactory.newThread(() -> {})` just to read the thread 
name creates a live (though unstarted) thread and silently increments 
`CamelThreadFactory`'s counter. The cleaner approach is to call 
`CamelThreadFactory.getName()` directly (the factory already exposes 
`getName()`), or to cast to `CamelThreadFactory` and read it without allocating 
a thread at all:
   ```java
   if (threadFactory instanceof CamelThreadFactory ctf && 
ctf.getName().endsWith("seda://v-task")) {
   ```
   This avoids the throwaway-thread side-effect and makes the intent explicit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to