allthingssecurity commented on code in PR #30913:
URL: https://github.com/apache/camel/pull/30913#discussion_r4716905763


##########
components/camel-netty/src/main/java/org/apache/camel/component/netty/TimeoutCorrelationManagerSupport.java:
##########
@@ -291,8 +296,40 @@ protected void doShutdown() throws Exception {
      */
     private static final class CorrelationTimeoutMap extends 
DefaultTimeoutMap<String, NettyCamelState> {
 
+        // the lock of the map, also held while a state is put, so that the 
check of the state that holds its correlation
+        // id and the put are atomic
+        private final Lock mapLock;
+
         CorrelationTimeoutMap(ScheduledExecutorService executor, long 
requestMapPollTimeMillis) {
-            super(executor, requestMapPollTimeMillis);
+            this(executor, requestMapPollTimeMillis, new ReentrantLock());
+        }
+
+        private CorrelationTimeoutMap(ScheduledExecutorService executor, long 
requestMapPollTimeMillis, Lock mapLock) {
+            super(executor, requestMapPollTimeMillis, mapLock);
+            this.mapLock = mapLock;

Review Comment:
   Thanks. This PR is merged now; it is a comment-only point, so I will fold it 
into the next change in this area rather than open a separate PR.



##########
components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyTimeoutCorrelationManagerDuplicateIdTest.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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.netty;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import io.netty.channel.Channel;
+import io.netty.channel.embedded.EmbeddedChannel;
+import org.apache.camel.AsyncProducer;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * With a TimeoutCorrelationManagerSupport, a request whose correlation id is 
used by another request that still waits
+ * for its reply fails, rather than replace the state of the other request 
(which would then never be completed).
+ */
+class NettyTimeoutCorrelationManagerDuplicateIdTest extends CamelTestSupport {
+
+    @BindToRegistry("myManager")
+    private final CountingCorrelationManager myManager = new 
CountingCorrelationManager();
+
+    private final List<Socket> accepted = new CopyOnWriteArrayList<>();
+    private final BlockingQueue<Request> received = new 
LinkedBlockingQueue<>();
+    private ServerSocket server;
+    private AsyncProducer producer;
+
+    private record Request(String line, Socket socket) {
+    }
+
+    private static final class Sent {
+        final Exchange exchange;
+        final CountDownLatch done = new CountDownLatch(1);
+
+        Sent(Exchange exchange) {
+            this.exchange = exchange;
+        }
+    }
+
+    @AfterEach
+    void cleanup() throws Exception {
+        ServiceHelper.stopService(producer);
+        for (Socket socket : accepted) {
+            socket.close();
+        }
+        if (server != null) {
+            server.close();
+        }
+    }
+
+    @Test
+    void testSecondRequestWithTheIdOfAWaitingRequestFails() throws Exception {
+        // producerPoolEnabled=false: both requests use the one channel, and 
the manager correlates the replies
+        startProducer("&sync=true&producerPoolEnabled=false");
+
+        Sent first = send("abc-first");
+        Request request = received.poll(5, TimeUnit.SECONDS);
+        assertNotNull(request, "the server should get the first request");
+
+        Sent second = send("abc-second");
+        assertTrue(second.done.await(10, TimeUnit.SECONDS), "the second 
request should fail");
+        assertInstanceOf(IllegalStateException.class, 
second.exchange.getException());
+        assertEquals(1, first.done.getCount(), "the first request should still 
wait for its reply");
+
+        reply(request, "abc-reply");
+        assertTrue(first.done.await(10, TimeUnit.SECONDS), "the first request 
should get its reply");
+        assertNull(first.exchange.getException());
+        assertEquals("abc-reply", 
first.exchange.getMessage().getBody(String.class));
+        assertEquals(2, myManager.puts.get());

Review Comment:
   Thanks. This PR is merged now; it is a comment-only point, so I will fold it 
into the next change in this area rather than open a separate PR.



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