gnodet-bot commented on code in PR #26846: URL: https://github.com/apache/camel/pull/26846#discussion_r4092867370
########## core/camel-core/src/test/java/org/apache/camel/processor/RecipientListInvalidEndpointReleaseTest.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.camel.Consumer; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.DefaultEndpoint; +import org.apache.camel.support.DefaultProducer; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * When a recipient cannot be resolved, the producers already acquired for the recipients before it must be released. + */ +public class RecipientListInvalidEndpointReleaseTest extends ContextTestSupport { + + private final AtomicInteger started = new AtomicInteger(); + private final AtomicInteger stopped = new AtomicInteger(); + + @Test + public void testProducersReleasedWhenLaterRecipientIsInvalid() { + assertThrows(Exception.class, () -> template.sendBody("direct:start", "Hello")); + + assertEquals(1, started.get()); + assertEquals(1, stopped.get(), "the producer of the prototype recipient should be released and stopped"); + } + + @Override + protected RouteBuilder createRouteBuilder() { + context.addComponent("track", new DefaultComponent() { + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) { + return new DefaultEndpoint(uri, this) { + @Override + public Producer createProducer() { + return new DefaultProducer(this) { + @Override + public void process(Exchange exchange) { + // noop + } + + @Override + protected void doStart() { + started.incrementAndGet(); + } + + @Override + protected void doStop() { + stopped.incrementAndGet(); + } + }; + } + + @Override + public Consumer createConsumer(Processor processor) { + throw new UnsupportedOperationException(); + } + }; + } + }); + + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").recipientList(constant("track:a,unknownxyz:b")).cacheSize(-1); + } + }; Review Comment: **Test only exercises prototype endpoints (`cacheSize=-1`).** The catch block in `createProcessorExchangePairs` calls `releaseIfNotBegun()` on all pairs created before the failing recipient. `releaseIfNotBegun()` calls `producerCache.releaseProducer(endpoint, producer)` regardless of `prototypeEndpoint` — so the fix applies equally to cached producers. But this test hard-codes `cacheSize(-1)`, leaving the cached-producer path untested. Add a second `@Test` (or parameterize) without `cacheSize(-1)` to cover cached producer release. ########## core/camel-core/src/test/java/org/apache/camel/processor/RecipientListTransactedContextDataTest.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +/** + * Each transaction must get its own transaction context data when a transacted exchange goes through a recipient list. + */ +public class RecipientListTransactedContextDataTest extends ContextTestSupport { + + private final List<Map<?, ?>> data = new ArrayList<>(); + + @Test + public void testTransactionContextDataIsNotShared() { + template.sendBody("direct:start", "A"); + template.sendBody("direct:start", "B"); + + assertEquals(2, data.size()); + assertNotNull(data.get(0)); + assertNotNull(data.get(1)); + assertNotSame(data.get(0), data.get(1), "two transactions must not share the transaction context data"); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .process(e -> e.getExchangeExtension().setTransacted(true)) + .recipientList(constant("direct:a")); Review Comment: **Missing intra-transaction sharing test.** The test verifies cross-transaction isolation (two sequential exchanges get different maps, i.e. `assertNotSame`) but doesn't verify the other half of the stated invariant: that within *one* transacted exchange, all recipient copies share the **same** map instance. The fix comment says `// each exchange (transaction) has its own transaction context data, shared by its copies`. "Shared by its copies" is the part under test here. Add a second `@Test` that sends a single message to `recipientList(constant("direct:a,direct:b"))` and asserts `assertSame` on the `TRANSACTION_CONTEXT_DATA` map retrieved inside each route. Without this test, a regression that breaks intra-transaction sharing (e.g. someone refactoring the `txData` propagation to pass `null` through the private overload) would go undetected. -- 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]
