gnodet-bot commented on code in PR #26603: URL: https://github.com/apache/camel/pull/26603#discussion_r4050131770
########## components/camel-microprofile/camel-microprofile-fault-tolerance/src/test/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceCallCountersTest.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.microprofile.faulttolerance; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Exchange; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.eclipse.microprofile.faulttolerance.exceptions.BulkheadException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The fallback, timed out and bulkhead rejected counters, which the circuit breaker listeners do not tell apart. + */ +public class FaultToleranceCallCountersTest extends CamelTestSupport { + + private final CountDownLatch permitAcquired = new CountDownLatch(1); + + @Test + public void testFallbackAndRejectedCounters() throws Exception { + getMockEndpoint("mock:down").expectedMessageCount(3); + + // two failures open the breaker, the third call is rejected without being attempted + template.sendBody("direct:down", "Hello World"); + template.sendBody("direct:down", "Hello World"); + template.sendBody("direct:down", "Hello World"); + + MockEndpoint.assertIsSatisfied(context); + + FaultToleranceProcessor cb = context.getProcessor("cbDown", FaultToleranceProcessor.class); + assertEquals(3, cb.getNumberOfFallbackCalls()); + assertEquals(1, cb.getNumberOfNotPermittedCalls()); + assertEquals(0, cb.getNumberOfTimedOutCalls()); + assertEquals(0, cb.getNumberOfBulkheadRejectedCalls()); + + cb.transitionToCloseState(); + assertEquals(0, cb.getNumberOfFallbackCalls()); + } + + @Test + public void testTimedOutCounter() throws Exception { + getMockEndpoint("mock:slow").expectedBodiesReceived("Fallback response"); + + template.sendBody("direct:slow", "Hello World"); + + MockEndpoint.assertIsSatisfied(context); + + FaultToleranceProcessor cb = context.getProcessor("cbSlow", FaultToleranceProcessor.class); + assertEquals(1, cb.getNumberOfFallbackCalls()); + assertEquals(1, cb.getNumberOfTimedOutCalls()); + assertEquals(0, cb.getNumberOfNotPermittedCalls()); + } + + @Test + public void testBulkheadRejectedWithFallbackCounter() throws Exception { + getMockEndpoint("mock:bulkhead").expectedMessageCount(2); + + // the first call holds the only bulkhead permit while it is slow, + // so the second call is rejected by the bulkhead and answered by the fallback + template.asyncSendBody("direct:bulkhead", "Hello World"); + assertTrue(permitAcquired.await(5, TimeUnit.SECONDS), "first call should be inside the bulkhead"); + template.sendBody("direct:bulkhead", "Hello World"); + + MockEndpoint.assertIsSatisfied(context); + + // the rejected call reached the fallback with the bulkhead exception (and came back first, + // before the slow call finished), so it was rejected, not queued + long rejected = getMockEndpoint("mock:bulkhead").getExchanges().stream() + .filter(e -> e.getProperty(Exchange.EXCEPTION_CAUGHT) instanceof BulkheadException).count(); + assertEquals(1, rejected, "exactly one of the two calls should have been rejected by the bulkhead"); + FaultToleranceProcessor cb = context.getProcessor("cbBulkhead", FaultToleranceProcessor.class); + assertEquals(1, cb.getNumberOfBulkheadRejectedCalls(), + "the rejected call is counted from inside the fallback path"); + assertEquals(1, cb.getNumberOfFallbackCalls()); + assertEquals(0, cb.getNumberOfNotPermittedCalls()); + assertEquals(0, cb.getNumberOfTimedOutCalls()); + } + + @Test + public void testTimedOutWithoutFallbackCounter() throws Exception { + // no onFallback: the timeout fails the exchange, and the counter is kept on that path too + Exchange result = template.request("direct:slowNoFallback", e -> e.getMessage().setBody("Hello World")); + + assertTrue(result.isFailed(), "the timed out call should fail the exchange"); + FaultToleranceProcessor cb = context.getProcessor("cbSlowNoFallback", FaultToleranceProcessor.class); + assertEquals(1, cb.getNumberOfTimedOutCalls()); + assertEquals(0, cb.getNumberOfFallbackCalls()); + } + + @Test + public void testBulkheadRejectedWithoutFallbackCounter() throws Exception { Review Comment: **Nit:** The comment "SmallRye needs a waiting queue of at least 1; the second call is still rejected, not queued" misrepresents the reason. `FaultToleranceProcessor` calls `typedGuard.call(task)` synchronously (`TypedGuard<Exchange>` returning `Exchange`, not `CompletionStage<Exchange>`). For synchronous actions, SmallRye FT uses a semaphore bulkhead where `queueSize()` has no effect — calls beyond the concurrency limit are immediately rejected regardless of queue size. The `bulkheadWaitingTaskQueue(1)` setting is silently ignored by the semaphore bulkhead, so the second call is rejected for the right reason but the explanation is wrong. A future developer could be misled into thinking they need to keep the queue ≥ 1 to make this work. ```suggestion // FaultToleranceProcessor uses TypedGuard.call() which is a synchronous action in SmallRye FT terms. // Synchronous actions use a semaphore bulkhead — queueSize has no effect, so the second concurrent // call is rejected immediately regardless of bulkheadWaitingTaskQueue. assertInstanceOf(BulkheadException.class, result.getException()); ``` -- 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]
