davsclaus commented on code in PR #26846:
URL: https://github.com/apache/camel/pull/26846#discussion_r4092926425


##########
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:
   Added in 71cee2efd94e: `testTransactionContextDataIsSharedWithinTransaction` 
sends two transacted exchanges through a recipient list with two recipients. It 
checks that both recipients of each exchange get the same map, and that the two 
exchanges get different maps. Without the fix it fails, because both exchanges 
get the same map.
   
   _Claude Code on behalf of Claus Ibsen_



##########
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:
   Added in 71cee2efd94e: 
`testCachedProducerReleasedWhenLaterRecipientIsInvalid` uses the default 
producer cache with an endpoint whose producer is not a singleton, so the 
producer is pooled. The test sends two messages, and each one fails on the 
second recipient. With the fix, the producer from the first message goes back 
to the pool and is reused for the second, so only 1 producer is started. 
Without the fix, a second producer is created (2 started).
   
   _Claude Code on behalf of Claus Ibsen_



-- 
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]

Reply via email to