gnodet-bot commented on code in PR #26769: URL: https://github.com/apache/camel/pull/26769#discussion_r4081951786
########## core/camel-core/src/test/java/org/apache/camel/processor/FailoverStickyWrapAroundTest.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Sticky failover (without round robin) must also try the endpoints before the last known good endpoint, once each. + */ +public class FailoverStickyWrapAroundTest extends ContextTestSupport { + + private final Set<String> down = ConcurrentHashMap.newKeySet(); + + @Test + public void testFailoverStickyContinuesFromFirstEndpoint() throws Exception { + // a and b are down, so c becomes the last known good endpoint + down.add("a"); + down.add("b"); + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + getMockEndpoint("mock:c").expectedBodiesReceived("Hello World"); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + + // now c is down and a and b are up: sticky starts from c, and must then continue from a + resetMocks(); + down.clear(); + down.add("c"); + getMockEndpoint("mock:a").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:b").expectedMessageCount(0); + getMockEndpoint("mock:c").expectedBodiesReceived("Bye World"); + template.sendBody("direct:start", "Bye World"); + assertMockEndpointsSatisfied(); + + // and a is now the last known good endpoint + resetMocks(); + getMockEndpoint("mock:a").expectedBodiesReceived("Hi World"); + getMockEndpoint("mock:b").expectedMessageCount(0); + getMockEndpoint("mock:c").expectedMessageCount(0); + template.sendBody("direct:start", "Hi World"); + assertMockEndpointsSatisfied(); + } + Review Comment: ⚠️ **Incomplete assertions in the setup phase.** The first phase of this test (lines 61-64) establishes `lastGoodIndex` for the all-down scenario that follows. It sets `a` and `b` down so `c` becomes the last good endpoint — but it only asserts that `mock:c` received exactly one message. It does NOT assert that `mock:a` and `mock:b` each received exactly one failed attempt before arriving at `c`. This matters because the all-down scenario in the second phase depends on `lastGoodIndex = 2` (i.e., `c`). If a regression caused the sticky wrap to break silently and the balancer short-circuited to `c` directly without going through `a` and `b`, this setup phase would still pass. Add the missing assertions: ```suggestion down.add("a"); down.add("b"); getMockEndpoint("mock:a").expectedMessageCount(1); getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:c").expectedMessageCount(1); template.sendBody("direct:start", "Hello World"); assertMockEndpointsSatisfied(); ``` ########## core/camel-core/src/test/java/org/apache/camel/processor/FailoverStickyWrapAroundTest.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Sticky failover (without round robin) must also try the endpoints before the last known good endpoint, once each. + */ +public class FailoverStickyWrapAroundTest extends ContextTestSupport { + + private final Set<String> down = ConcurrentHashMap.newKeySet(); + + @Test + public void testFailoverStickyContinuesFromFirstEndpoint() throws Exception { + // a and b are down, so c becomes the last known good endpoint + down.add("a"); + down.add("b"); + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + getMockEndpoint("mock:c").expectedBodiesReceived("Hello World"); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + + // now c is down and a and b are up: sticky starts from c, and must then continue from a + resetMocks(); + down.clear(); + down.add("c"); + getMockEndpoint("mock:a").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:b").expectedMessageCount(0); + getMockEndpoint("mock:c").expectedBodiesReceived("Bye World"); + template.sendBody("direct:start", "Bye World"); + assertMockEndpointsSatisfied(); + + // and a is now the last known good endpoint + resetMocks(); + getMockEndpoint("mock:a").expectedBodiesReceived("Hi World"); + getMockEndpoint("mock:b").expectedMessageCount(0); + getMockEndpoint("mock:c").expectedMessageCount(0); + template.sendBody("direct:start", "Hi World"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testFailoverStickyTriesEachEndpointOnceWhenAllDown() throws Exception { + down.add("a"); + down.add("b"); + getMockEndpoint("mock:c").expectedMessageCount(1); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + + resetMocks(); + down.add("c"); + getMockEndpoint("mock:a").expectedMessageCount(1); + getMockEndpoint("mock:b").expectedMessageCount(1); + getMockEndpoint("mock:c").expectedMessageCount(1); + assertThrows(CamelExecutionException.class, () -> template.sendBody("direct:start", "Bye World")); + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .loadBalance().failover(-1, false, false, true) + .to("direct:a", "direct:b", "direct:c"); + + from("direct:a").to("mock:a").process(e -> failIfDown("a")); + from("direct:b").to("mock:b").process(e -> failIfDown("b")); + from("direct:c").to("mock:c").process(e -> failIfDown("c")); + } + }; + } + + private void failIfDown(String name) { + if (down.contains(name)) { + throw new IllegalArgumentException(name + " is down"); + } + } +} Review Comment: ⚠️ **Missing test: `maximumFailoverAttempts` interaction with sticky wrap-around.** The production code has two independent exit conditions that can both fire during a sticky wrap-around attempt: 1. The new `index == start` guard (all endpoints tried once) 2. The existing `attempts > maximumFailoverAttempts` guard There is no test verifying that when `maximumFailoverAttempts` is, say, 1, the balancer stops after 1 attempt even if wrap-around is enabled. Conversely, there is no test verifying that the `attempts` guard fires correctly with wrap-around (i.e., that the attempt counter is incremented for the wrapped endpoints too). Without a test, a subtle ordering bug between the two exit conditions (e.g., wrap fires before the attempts check and resets progress) would go undetected. Please add a test such as: ```java @Test public void testFailoverStickyRespectsMaximumFailoverAttempts() throws Exception { // a is down; b becomes lastGoodIndex down.add("a"); template.sendBody("direct:start", "init"); // lastGoodIndex = 1 (b) // now b is also down; with maximumFailoverAttempts=1 only 1 failover is allowed // use a route configured with failover(-1 -> 1, false, false, true) // verify that c is NOT tried (only 1 attempt beyond the starting point) // ... } ``` (The exact assertions depend on how the test infrastructure is wired; the point is that the bounded-attempts + wrap-around interaction needs explicit coverage.) -- 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]
