gnodet-bot commented on code in PR #27510: URL: https://github.com/apache/camel/pull/27510#discussion_r4209134431
########## core/camel-core/src/test/java/org/apache/camel/processor/SagaCompensationFailureTest.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.processor; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.log.ConsumingAppender; +import org.apache.camel.model.SagaCompletionMode; +import org.apache.camel.saga.CamelSagaCoordinator; +import org.apache.camel.saga.InMemorySagaService; +import org.apache.camel.spi.Registry; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * CAMEL-25419: what the saga EIP reports when a compensation cannot be done, and what the in-memory saga service logs + * when it stops while a compensation is still being retried. + */ +public class SagaCompensationFailureTest extends ContextTestSupport { + + private static final String SAGA_SERVICE_LOGGER = InMemorySagaService.class.getName(); + + private final AtomicReference<CamelSagaCoordinator> coordinator = new AtomicReference<>(); + private final AtomicReference<String> timeoutSagaId = new AtomicReference<>(); + private final AtomicInteger flakyCompensationCalls = new AtomicInteger(); + private final List<String> sagaServiceWarnings = new CopyOnWriteArrayList<>(); + + @Test + public void testFailedCompensationKeepsTheOriginalException() { + Exchange result = template.send("direct:saga-fails", e -> e.getMessage().setBody("hello")); + + Exception ex = result.getException(); + assertNotNull(ex, "the failure should propagate to the exchange"); + assertTrue(chainContains(ex, "business failure"), + "the exception that caused the compensation should not be lost, but got: " + ex); + } + + @Test + public void testFailedCompensationIsNotReportedAsCompensated() throws Exception { + Exchange result = template.send("direct:saga-fails", e -> e.getMessage().setBody("hello")); Review Comment: 💡 **Missing suppressed-exception coverage** — `testFailedCompensationKeepsTheOriginalException` verifies the original exception chain but doesn't exercise the suppressed exception that `SagaProcessor` attaches. The production code at `SagaProcessor.java` now calls `cause.addSuppressed(...)` to record the compensation failure. A test for that specifically would strengthen confidence: ```java // after the existing assertions: Throwable[] suppressed = ex.getSuppressed(); assertEquals(1, suppressed.length, "compensation failure should be attached as suppressed"); assertInstanceOf(IllegalStateException.class, suppressed[0]); assertTrue(suppressed[0].getMessage().contains("compensation failure")); ``` ########## core/camel-core/src/test/java/org/apache/camel/processor/SagaCompensationFailureTest.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.processor; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.log.ConsumingAppender; +import org.apache.camel.model.SagaCompletionMode; +import org.apache.camel.saga.CamelSagaCoordinator; +import org.apache.camel.saga.InMemorySagaService; +import org.apache.camel.spi.Registry; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * CAMEL-25419: what the saga EIP reports when a compensation cannot be done, and what the in-memory saga service logs + * when it stops while a compensation is still being retried. + */ +public class SagaCompensationFailureTest extends ContextTestSupport { + + private static final String SAGA_SERVICE_LOGGER = InMemorySagaService.class.getName(); + + private final AtomicReference<CamelSagaCoordinator> coordinator = new AtomicReference<>(); + private final AtomicReference<String> timeoutSagaId = new AtomicReference<>(); + private final AtomicInteger flakyCompensationCalls = new AtomicInteger(); + private final List<String> sagaServiceWarnings = new CopyOnWriteArrayList<>(); + + @Test + public void testFailedCompensationKeepsTheOriginalException() { + Exchange result = template.send("direct:saga-fails", e -> e.getMessage().setBody("hello")); + + Exception ex = result.getException(); + assertNotNull(ex, "the failure should propagate to the exchange"); + assertTrue(chainContains(ex, "business failure"), + "the exception that caused the compensation should not be lost, but got: " + ex); + } + + @Test + public void testFailedCompensationIsNotReportedAsCompensated() throws Exception { + Exchange result = template.send("direct:saga-fails", e -> e.getMessage().setBody("hello")); + assertNotNull(result.getException()); + + CamelSagaCoordinator saga = coordinator.get(); + assertNotNull(saga); + // the compensation could not be done: compensating the saga again must not report success + ExecutionException ex = assertThrows(ExecutionException.class, + () -> saga.compensate(result).get(5, TimeUnit.SECONDS), + "a saga whose compensation failed is reported as successfully compensated"); + assertNotNull(ex.getCause()); + } + + @Test + public void testCompensationRetryPendingWhenContextStopsIsLogged() throws Exception { + // the saga times out, the first compensation attempt fails, and a retry is scheduled Review Comment: ⚠️ **Weak assertion** — this only verifies a cause exists, not *what* it is. Since the test knows exactly what `compensation-fails` throws, consider being explicit: ```suggestion assertNotNull(ex.getCause()); assertInstanceOf(IllegalStateException.class, ex.getCause()); assertTrue(ex.getCause().getMessage().contains("compensation failure")); ``` ########## core/camel-core-processor/src/main/java/org/apache/camel/processor/saga/SagaProcessor.java: ########## @@ -110,10 +111,21 @@ protected void handleSagaCompletion( if (this.completionMode == SagaCompletionMode.AUTO) { if (exchange.getException() != null) { if (coordinator != null) { - coordinator.compensate(exchange).whenComplete((done, ex) -> ifNotException(ex, exchange, callback, () -> { - setCurrentSagaCoordinator(exchange, previousCoordinator); - callback.done(false); - })); + final Exception cause = exchange.getException(); + coordinator.compensate(exchange).whenComplete((done, ex) -> { + if (ex != null) { + // keep the exception that caused the compensation, and attach the compensation failure to it + cause.addSuppressed( + ex instanceof CompletionException && ex.getCause() != null ? ex.getCause() : ex); + exchange.setException(cause); + callback.done(false); + } else { + ifNotException(null, exchange, callback, () -> { Review Comment: 💡 **Minor** — in the `else` branch `ex` is guaranteed `null` here, so `ifNotException(null, ...)` will always execute the lambda. Calling it directly is clearer: ```suggestion } else { setCurrentSagaCoordinator(exchange, previousCoordinator); callback.done(false); ``` Low priority — but since the happy-path code is already simple, the wrapper adds noise. -- 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]
