|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.activemq; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | + |
| 23 | +import jakarta.jms.JMSException; |
| 24 | +import jakarta.jms.MessageConsumer; |
| 25 | +import jakarta.jms.Session; |
| 26 | + |
| 27 | +import org.apache.activemq.broker.Broker; |
| 28 | +import org.apache.activemq.broker.BrokerFilter; |
| 29 | +import org.apache.activemq.broker.BrokerPlugin; |
| 30 | +import org.apache.activemq.broker.BrokerService; |
| 31 | +import org.apache.activemq.broker.ConnectionContext; |
| 32 | +import org.apache.activemq.broker.TransportConnector; |
| 33 | +import org.apache.activemq.broker.region.Subscription; |
| 34 | +import org.apache.activemq.command.ActiveMQQueue; |
| 35 | +import org.apache.activemq.command.ConsumerInfo; |
| 36 | +import org.apache.activemq.transport.RequestTimedOutIOException; |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests that {@link ActiveMQConnection#syncSendPacket(org.apache.activemq.command.Command)} |
| 43 | + * applies the configured {@code requestTimeout}. |
| 44 | + */ |
| 45 | +public class SyncSendPacketTimeoutTest { |
| 46 | + |
| 47 | + private BrokerService broker; |
| 48 | + private String brokerUrl; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() throws Exception { |
| 52 | + broker = new BrokerService(); |
| 53 | + broker.setPersistent(false); |
| 54 | + broker.setUseJmx(false); |
| 55 | + broker.addConnector("tcp://localhost:0"); |
| 56 | + broker.start(); |
| 57 | + broker.waitUntilStarted(); |
| 58 | + brokerUrl = broker.getTransportConnectors().get(0).getPublishableConnectString(); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void tearDown() throws Exception { |
| 63 | + if (broker != null) { |
| 64 | + broker.stop(); |
| 65 | + broker.waitUntilStopped(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testRequestTimeoutDefaultIsZero() throws Exception { |
| 71 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 72 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 73 | + assertEquals("Default requestTimeout should be 0", 0, connection.getRequestTimeout()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRequestTimeoutConfiguredViaFactory() throws Exception { |
| 79 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 80 | + factory.setRequestTimeout(5000); |
| 81 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 82 | + assertEquals("requestTimeout should be propagated from factory", 5000, connection.getRequestTimeout()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testSyncSendPacketSucceedsWithRequestTimeout() throws Exception { |
| 88 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 89 | + factory.setRequestTimeout(5000); |
| 90 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 91 | + connection.start(); |
| 92 | + // Creating a session triggers syncSendPacket internally — should succeed within |
| 93 | + // timeout |
| 94 | + try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 95 | + // Creating a consumer triggers syncSendPacket internally — should succeed |
| 96 | + // within timeout |
| 97 | + MessageConsumer consumer = session.createConsumer(session.createQueue("TEST.QUEUE"))) { |
| 98 | + assertNotNull("Session should be created successfully", session); |
| 99 | + assertNotNull("Consumer should be created successfully", consumer); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testSyncSendPacketSucceedsWithoutRequestTimeout() throws Exception { |
| 106 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 107 | + // requestTimeout=0 means no timeout (default) |
| 108 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 109 | + connection.start(); |
| 110 | + try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 111 | + // Creating a consumer triggers syncSendPacket internally — should succeed |
| 112 | + // no timeout |
| 113 | + MessageConsumer consumer = session.createConsumer(session.createQueue("TEST.QUEUE"))) { |
| 114 | + assertNotNull("Session should be created successfully with no timeout", session); |
| 115 | + assertNotNull("Consumer should be created successfully", consumer); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testRequestTimeoutConfiguredViaUrl() throws Exception { |
| 122 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl + "?jms.requestTimeout=3000"); |
| 123 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 124 | + assertEquals("requestTimeout should be set via URL parameter", 3000, connection.getRequestTimeout()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testSyncSendPacketFailFromTimeout() throws Exception { |
| 130 | + // Restart the broker with a plugin that delays addConsumer responses |
| 131 | + broker.stop(); |
| 132 | + broker.waitUntilStopped(); |
| 133 | + |
| 134 | + broker = new BrokerService(); |
| 135 | + broker.setPersistent(false); |
| 136 | + broker.setUseJmx(false); |
| 137 | + broker.setPlugins(new BrokerPlugin[]{new BrokerPlugin() { |
| 138 | + @Override |
| 139 | + public Broker installPlugin(Broker broker) { |
| 140 | + return new BrokerFilter(broker) { |
| 141 | + @Override |
| 142 | + public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception { |
| 143 | + // Only delay consumers on our test queue, not advisory consumers |
| 144 | + if (info.getDestination().getPhysicalName().equals("test")) { |
| 145 | + Thread.sleep(5000); |
| 146 | + } |
| 147 | + return super.addConsumer(context, info); |
| 148 | + } |
| 149 | + }; |
| 150 | + } |
| 151 | + }}); |
| 152 | + broker.addConnector("tcp://localhost:0"); |
| 153 | + broker.start(); |
| 154 | + broker.waitUntilStarted(); |
| 155 | + brokerUrl = broker.getTransportConnectors().get(0).getPublishableConnectString(); |
| 156 | + |
| 157 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 158 | + factory.setWatchTopicAdvisories(false); |
| 159 | + factory.setRequestTimeout(500); |
| 160 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 161 | + connection.start(); |
| 162 | + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 163 | + |
| 164 | + Exception exception = null; |
| 165 | + try { |
| 166 | + session.createConsumer(session.createQueue("test")); |
| 167 | + fail("Expected JMSException due to request timeout"); |
| 168 | + } catch (JMSException expected) { |
| 169 | + exception = expected; |
| 170 | + } |
| 171 | + assertNotNull("Should have caught a JMSException", exception); |
| 172 | + assertEquals(RequestTimedOutIOException.class, |
| 173 | + TransportConnector.getRootCause(exception).getClass()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testSyncSendPacketOverrideDefaultRequestTimeout() throws Exception { |
| 179 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); |
| 180 | + try (ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection()) { |
| 181 | + connection.start(); |
| 182 | + ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 183 | + // After session creation set the timeout default to be very short to test that |
| 184 | + // overriding directly works |
| 185 | + connection.setRequestTimeout(1); |
| 186 | + ConsumerInfo info = new ConsumerInfo(session.getSessionInfo(), |
| 187 | + session.getNextConsumerId().getValue()); |
| 188 | + info.setDestination(new ActiveMQQueue("test")); |
| 189 | + // Send info packet with timeout override |
| 190 | + assertNotNull("Consumer should be created successfully with no timeout", |
| 191 | + connection.syncSendPacket(info, 5000)); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments