jbonofre commented on code in PR #1740: URL: https://github.com/apache/activemq/pull/1740#discussion_r2905921351
########## activemq-unit-tests/src/test/java/org/apache/activemq/transport/ResponseCorrelatorTest.java: ########## @@ -0,0 +1,239 @@ +/** + * 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.activemq.transport; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.activemq.command.ExceptionResponse; +import org.apache.activemq.command.Response; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ResponseCorrelatorTest { + + private StubTransport stubTransport; + private ResponseCorrelator correlator; + + @Before + public void setUp() throws Exception { + stubTransport = new StubTransport(); + correlator = new ResponseCorrelator(stubTransport); + correlator.setTransportListener(new StubTransportListener()); + stubTransport.start(); + correlator.start(); + } + + @After + public void tearDown() throws Exception { + correlator.stop(); + stubTransport.stop(); + } + + @Test + public void testMaxResponseTimeoutDefaultIsZero() { + assertEquals(0, correlator.getMaxResponseTimeout()); + } + + @Test + public void testSetMaxResponseTimeout() { + correlator.setMaxResponseTimeout(5000); + assertEquals(5000, correlator.getMaxResponseTimeout()); + } + + @Test + public void testRequestWithMaxResponseTimeoutThrowsOnTimeout() throws Exception { + correlator.setMaxResponseTimeout(500); + + // Send a request but never provide a response — should time out + long start = System.currentTimeMillis(); + try { + correlator.request(new TestCommand()); + fail("Expected RequestTimedOutIOException"); + } catch (RequestTimedOutIOException e) { + // expected + } + long elapsed = System.currentTimeMillis() - start; + assertTrue("Should have timed out after ~500ms but took " + elapsed + "ms", elapsed >= 400); + } + + @Test + public void testRequestWithZeroMaxResponseTimeoutDoesNotTimeout() throws Exception { + // maxResponseTimeout=0 means no timeout (default behavior) + correlator.setMaxResponseTimeout(0); + + final CountDownLatch requestStarted = new CountDownLatch(1); + final AtomicReference<Object> result = new AtomicReference<>(); + final AtomicReference<Throwable> error = new AtomicReference<>(); + + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + executor.submit(() -> { + try { + requestStarted.countDown(); + result.set(correlator.request(new TestCommand())); + } catch (Throwable e) { + error.set(e); + } + }); + + assertTrue("Request should have started", requestStarted.await(5, TimeUnit.SECONDS)); + + // Wait a bit to confirm the thread is still blocked + Thread.sleep(500); Review Comment: That's very true, my bad. Let me improve that. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information, visit: https://activemq.apache.org/contact
