allthingssecurity commented on code in PR #26797:
URL: https://github.com/apache/camel/pull/26797#discussion_r4090538670
##########
components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaConsumer.java:
##########
@@ -173,10 +178,11 @@ protected void doRun() {
// do not poll if we are suspended or starting again after resuming
if (isSuspending() || isSuspended() || isStarting()) {
- if (shutdownPending && queue.isEmpty()) {
+ if (shutdownPending) {
Review Comment:
Agreed, done in f9c1bdea9. The suspended branch now breaks out on
`shutdownPending` only if the consumer is suspending/suspended **or** the queue
is empty. A consumer that is only starting keeps the old behaviour and waits
for the queue to drain. `getPendingExchangesSize()` already returned the queue
size for a starting consumer (the 0 is only for suspending/suspended), so both
halves are consistent now. `Seda*`, `*Suspend*`, `StopRoute*` and
`*GracefulShutdown*`: 130 tests, 0 failures, in two runs.
_Claude Code on behalf of allthingssecurity_
##########
core/camel-core/src/test/java/org/apache/camel/component/seda/SedaSuspendedRouteWithPendingStopTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Stopping a suspended seda route must not wait for the messages sent to it
while it was suspended, as a suspended
+ * consumer does not consume them.
+ */
+class SedaSuspendedRouteWithPendingStopTest extends ContextTestSupport {
+
+ private final CountDownLatch processing = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ @Test
+ void testStopSuspendedRouteWithPendingMessages() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("X");
+
+ // keep the consumer thread busy with X while the consumer is
suspended (as a route policy such as
+ // ThrottlingInflightRoutePolicy does), so it does not poll the queue
while A, B and C are sent
+ template.sendBody("seda:start", "X");
+ assertTrue(processing.await(10, TimeUnit.SECONDS), "X should be
processed");
+ SedaEndpoint seda = (SedaEndpoint)
context.getRoute("foo").getEndpoint();
+ ((SedaConsumer) context.getRoute("foo").getConsumer()).suspend();
+
+ template.sendBody("seda:start", "A");
+ template.sendBody("seda:start", "B");
+ template.sendBody("seda:start", "C");
+ release.countDown();
+ mock.assertIsSatisfied();
Review Comment:
I kept the direct `consumer.suspend()` because that's the path a
`RoutePolicy` takes: `RoutePolicySupport.suspendOrStopConsumer(Consumer)` is
`ServiceHelper.suspendService(consumer)`, which `ThrottlingInflightRoutePolicy`
uses. It suspends the consumer only, not the route through the route
controller, so no route lifecycle event is fired there either. The route stays
`Started` while its consumer is suspended, which is the case this fix is about.
`testStopContextWithSuspendedRoute` covers the `suspendRoute()` path.
_Claude Code on behalf of allthingssecurity_
##########
core/camel-core/src/test/java/org/apache/camel/component/seda/SedaSuspendedRouteWithPendingStopTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Stopping a suspended seda route must not wait for the messages sent to it
while it was suspended, as a suspended
+ * consumer does not consume them.
+ */
+class SedaSuspendedRouteWithPendingStopTest extends ContextTestSupport {
+
+ private final CountDownLatch processing = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ @Test
+ void testStopSuspendedRouteWithPendingMessages() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("X");
+
+ // keep the consumer thread busy with X while the consumer is
suspended (as a route policy such as
+ // ThrottlingInflightRoutePolicy does), so it does not poll the queue
while A, B and C are sent
+ template.sendBody("seda:start", "X");
+ assertTrue(processing.await(10, TimeUnit.SECONDS), "X should be
processed");
+ SedaEndpoint seda = (SedaEndpoint)
context.getRoute("foo").getEndpoint();
+ ((SedaConsumer) context.getRoute("foo").getConsumer()).suspend();
+
+ template.sendBody("seda:start", "A");
+ template.sendBody("seda:start", "B");
+ template.sendBody("seda:start", "C");
+ release.countDown();
+ mock.assertIsSatisfied();
+
+ // abort the stop if the graceful shutdown times out
+ boolean stopped = context.getRouteController().stopRoute("foo", 10,
TimeUnit.SECONDS, true);
+ assertTrue(stopped, "Route should be stopped without waiting for the
shutdown timeout");
+ assertFalse(context.getShutdownStrategy().isTimeoutOccurred());
+ assertEquals(ServiceStatus.Stopped,
context.getRouteController().getRouteStatus("foo"));
+
+ // the suspended consumer did not process the messages, they are kept
on the queue
+ assertEquals(3, seda.getQueue().size());
+
+ // and they are processed when the route is started again
+ mock.reset();
+ mock.expectedBodiesReceived("A", "B", "C");
+ context.getRouteController().startRoute("foo");
+ mock.assertIsSatisfied();
+ }
+
+ @Test
+ void testStopContextWithSuspendedRoute() throws Exception {
+ context.getRouteController().suspendRoute("foo");
+
+ template.sendBody("seda:start", "A");
+ template.sendBody("seda:start", "B");
+
+ context.getShutdownStrategy().setTimeout(10);
+ context.stop();
+ assertFalse(context.getShutdownStrategy().isTimeoutOccurred(),
"Graceful shutdown should not time out");
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
Review Comment:
Added in f9c1bdea9. The test keeps a reference to the queue (the endpoint
drops its reference on shutdown) and asserts that `queue.size() +
mock.getReceivedCounter() == 2` after `context.stop()`, so a regression that
drops the messages fails. It checks the sum and not the queue size alone
because `suspendRoute()` doesn't interrupt a poll that is already in progress,
so that poll may still take A.
_Claude Code on behalf of allthingssecurity_
--
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]