This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new 6266852fe9fc [backport camel-4.22.x] CAMEL-24903: Fix flaky
ThrottlingExceptionRoutePolicyOpenViaConfigTest (#29864)
6266852fe9fc is described below
commit 6266852fe9fc84318bcade9225e40a20c22327ab
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 13 20:42:58 2026 +0100
[backport camel-4.22.x] CAMEL-24903: Fix flaky
ThrottlingExceptionRoutePolicyOpenViaConfigTest (#29864)
Backport of #26799 to camel-4.22.x.
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
...tlingExceptionRoutePolicyOpenViaConfigTest.java | 18 ++++++++++++------
.../throttling/ThrottlingExceptionRoutePolicy.java | 22 ++++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_22.adoc | 9 +++++++++
3 files changed, 43 insertions(+), 6 deletions(-)
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
index 75d7b7b81abc..46ea90090390 100644
---
a/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/throttle/ThrottlingExceptionRoutePolicyOpenViaConfigTest.java
@@ -69,12 +69,16 @@ class ThrottlingExceptionRoutePolicyOpenViaConfigTest
extends ContextTestSupport
result.expectedMessageCount(size);
MockEndpoint.assertIsSatisfied(context, TIMEOUT_SECONDS,
TimeUnit.SECONDS);
- // set keepOpen to true
- policy.setKeepOpen(true);
-
- // trigger opening circuit
- // by sending another message
+ // send the trigger message while the circuit is still closed so it is
+ // guaranteed to be consumed before we open the circuit
template.sendBody(url, "MessageTrigger");
+ result.expectedMessageCount(size + 1);
+ MockEndpoint.assertIsSatisfied(context, TIMEOUT_SECONDS,
TimeUnit.SECONDS);
+
+ // toggle keepOpen to true: setKeepOpen() immediately suspends the
+ // consumer so no late onExchangeDone() callback can race against the
+ // trigger message (CAMEL-24903)
+ policy.setKeepOpen(true);
// wait for the circuit to open (consumer suspended)
await().atMost(10, TimeUnit.SECONDS).until(consumer::isSuspended);
@@ -85,9 +89,11 @@ class ThrottlingExceptionRoutePolicyOpenViaConfigTest
extends ContextTestSupport
template.sendBody(url, "MessageRound2 " + i);
}
- // should not close b/c keepOpen is true
+ // should not close b/c keepOpen is true; use assertPeriod to verify
no extra messages arrive
+ result.setAssertPeriod(500);
result.expectedMessageCount(size + 1);
MockEndpoint.assertIsSatisfied(context, TIMEOUT_SECONDS,
TimeUnit.SECONDS);
+ result.setAssertPeriod(0);
// set keepOpen to false
policy.setKeepOpen(false);
diff --git
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
index 3b4365a887db..033f5b1d21ad 100644
---
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
+++
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
@@ -311,6 +311,9 @@ public class ThrottlingExceptionRoutePolicy extends
RoutePolicySupport implement
protected void openCircuit(Route route) {
try {
lock.lock();
+ if (state.get() == STATE_OPEN) {
+ return;
+ }
suspendOrStopConsumer(route.getConsumer());
state.set(STATE_OPEN);
openedAt = System.currentTimeMillis();
@@ -448,8 +451,27 @@ public class ThrottlingExceptionRoutePolicy extends
RoutePolicySupport implement
return this.keepOpenBool.get();
}
+ /**
+ * Sets whether to keep the circuit breaker always open (never closes).
This is only intended for development and
+ * testing purposes.
+ * <p>
+ * When set to {@code true}, the circuit is opened immediately and
synchronously (the consumer is suspended before
+ * this method returns). Setting it back to {@code false} is deferred: the
half-open timer will attempt to close the
+ * circuit on its next tick (after {@link #getHalfOpenAfter()}
milliseconds).
+ * <p>
+ * Note: a single {@link ThrottlingExceptionRoutePolicy} instance is
designed for one route. When attached to
+ * multiple routes, only the last route bound via {@link #setRoute(Route)}
is acted upon by this setter.
+ */
public void setKeepOpen(boolean keepOpen) {
this.keepOpenBool.set(keepOpen);
+ // Immediately act on the new value so callers do not have to send a
+ // trigger message and wait for the next onExchangeDone() callback:
+ // - keepOpen=true → open the circuit right away (openCircuit is
idempotent under its lock)
+ // - keepOpen=false → the half-open timer will close the circuit on
its next tick
+ if (keepOpen && route != null) {
+ LOG.debug("Opening circuit (keepOpen set to true)");
+ openCircuit(route);
+ }
}
public int getFailureThreshold() {
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
index 15dd3fad2701..7e024c6aff40 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc
@@ -2415,3 +2415,12 @@ way as the `camel.uri` attribute, which shows the
endpoint URI as `Endpoint.toSt
is.
Dashboards or queries that use the old values may need a change.
+
+=== ThrottlingExceptionRoutePolicy
+
+`ThrottlingExceptionRoutePolicy.setKeepOpen(true)` now opens the circuit
immediately and synchronously (the consumer
+is suspended before the setter returns) rather than waiting for the next
`onExchangeDone()` callback.
+This is the intended behaviour and makes the `keepOpen` toggle deterministic,
but callers that previously
+relied on the setter being inert until an exchange arrived must be aware of
the change.
+Setting `keepOpen` back to `false` remains deferred: the half-open timer
attempts to close the circuit on
+its next tick (after `halfOpenAfter` milliseconds, default 30 s).