atiaomar1978-hub commented on code in PR #26028: URL: https://github.com/apache/camel/pull/26028#discussion_r3910874306
########## components/camel-master/src/test/java/org/apache/camel/component/master/MasterConsumerLeadershipTest.java: ########## @@ -0,0 +1,352 @@ +/* + * 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.master; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.Consumer; +import org.apache.camel.Endpoint; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cluster.CamelClusterMember; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.DefaultConsumer; +import org.apache.camel.support.DefaultEndpoint; +import org.apache.camel.support.cluster.AbstractCamelClusterService; +import org.apache.camel.support.cluster.AbstractCamelClusterView; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Verifies that the delegated consumer only ever runs while this node holds the leadership, also when the leadership + * changes while the start of the delegated consumer is still pending. + */ +public class MasterConsumerLeadershipTest { + + private DefaultCamelContext context; + private TestClusterService clusterService; + private ProbeComponent probe; + + @BeforeEach + void setUp() throws Exception { + probe = new ProbeComponent(); + clusterService = new TestClusterService(); + + context = new DefaultCamelContext(); + context.disableJMX(); + context.addService(clusterService); + context.addComponent("probe", probe); + + MasterComponent master = context.getComponent("master", MasterComponent.class); + // keep the retries short so an exhausted start does not dominate the test time + master.setBackOffDelay(200); + master.setBackOffMaxAttempts(2); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("master:ns:probe:test").routeId("master-route").to("mock:result"); + } + }); + + context.start(); + } + + @AfterEach + void tearDown() { + if (context != null) { + context.stop(); + } + } + + @Test + @Timeout(60) + void testLeadershipLostWhilePendingStartDoesNotStartConsumer() { + TestClusterView view = clusterService.getTestView(); + Review Comment: **Strong test coverage.** The fake cluster view + probe consumer exercises pending start, in-progress start, post-success flap, stop-during-pending, and exhausted-retry recovery without real K8s/Pub/Sub infra. This gives good confidence the production race is fixed. -- 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]
