cshannon commented on code in PR #1740: URL: https://github.com/apache/activemq/pull/1740#discussion_r2908311585
########## activemq-unit-tests/src/test/java/org/apache/activemq/SyncSendPacketTimeoutTest.java: ########## @@ -0,0 +1,119 @@ +/** + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import jakarta.jms.Session; + +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests that {@link ActiveMQConnection#syncSendPacket(org.apache.activemq.command.Command)} + * applies the configured {@code requestTimeout}. + */ +public class SyncSendPacketTimeoutTest { + + private BrokerService broker; + private String brokerUrl; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.addConnector("tcp://localhost:0"); + broker.start(); + broker.waitUntilStarted(); + brokerUrl = broker.getTransportConnectors().get(0).getPublishableConnectString(); + } + + @After + public void tearDown() throws Exception { + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } + } + + @Test + public void testRequestTimeoutDefaultIsZero() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + assertEquals("Default requestTimeout should be 0", 0, connection.getRequestTimeout()); + } finally { + connection.close(); + } + } + + @Test + public void testRequestTimeoutConfiguredViaFactory() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + factory.setRequestTimeout(5000); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + assertEquals("requestTimeout should be propagated from factory", 5000, connection.getRequestTimeout()); + } finally { + connection.close(); + } + } + + @Test + public void testSyncSendPacketSucceedsWithRequestTimeout() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + factory.setRequestTimeout(5000); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + connection.start(); + // Creating a session triggers syncSendPacket internally — should succeed within timeout + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + assertNotNull("Session should be created successfully", session); + } finally { + connection.close(); + } + } + + @Test + public void testSyncSendPacketSucceedsWithoutRequestTimeout() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + // requestTimeout=0 means no timeout (default) + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { Review Comment: Nit: You could use try-with-resources for all of these tests, but not a huge deal either way. ########## activemq-unit-tests/src/test/java/org/apache/activemq/SyncSendPacketTimeoutTest.java: ########## @@ -0,0 +1,119 @@ +/** + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import jakarta.jms.Session; + +import org.apache.activemq.broker.BrokerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests that {@link ActiveMQConnection#syncSendPacket(org.apache.activemq.command.Command)} + * applies the configured {@code requestTimeout}. + */ +public class SyncSendPacketTimeoutTest { + + private BrokerService broker; + private String brokerUrl; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.addConnector("tcp://localhost:0"); + broker.start(); + broker.waitUntilStarted(); + brokerUrl = broker.getTransportConnectors().get(0).getPublishableConnectString(); + } + + @After + public void tearDown() throws Exception { + if (broker != null) { + broker.stop(); + broker.waitUntilStopped(); + } + } + + @Test + public void testRequestTimeoutDefaultIsZero() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + assertEquals("Default requestTimeout should be 0", 0, connection.getRequestTimeout()); + } finally { + connection.close(); + } + } + + @Test + public void testRequestTimeoutConfiguredViaFactory() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + factory.setRequestTimeout(5000); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + assertEquals("requestTimeout should be propagated from factory", 5000, connection.getRequestTimeout()); + } finally { + connection.close(); + } + } + + @Test + public void testSyncSendPacketSucceedsWithRequestTimeout() throws Exception { + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + factory.setRequestTimeout(5000); + ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); + try { + connection.start(); + // Creating a session triggers syncSendPacket internally — should succeed within timeout + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Review Comment: This is not actually testing syncSendPacket. Creating a session actually uses an async call [here](https://github.com/apache/activemq/blob/a2fe8919932463076671e3318c8a874e59c01f94/activemq-client/src/main/java/org/apache/activemq/ActiveMQSession.java#L259). You could add an extra line to create a consumer and that would work as createConsumer() does indeed use syncSendPacket when creating the consumer [here](https://github.com/apache/activemq/blob/a2fe8919932463076671e3318c8a874e59c01f94/activemq-client/src/main/java/org/apache/activemq/ActiveMQMessageConsumer.java#L301). This should be done anywhere else that is relevant. ```java connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Creating a consumer triggers syncSendPacket internally — should succeed within timeout session.createConsumer(session.createQueue("test")); ``` -- 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
