From a083ff4d23e9ea0997efa5aa596ed5f1c3c6ee24 Mon Sep 17 00:00:00 2001 From: "Christopher L. Shannon (cshannon)" Date: Wed, 11 Jan 2023 06:21:21 -0500 Subject: [PATCH] AMQ-9193 - Improve broker shutdown in unit tests This should improve test reliability for the unit tests so brokers don't hang around after the end of a test on error. Also increase the surefire re-run count to 3 times before failing. --- Jenkinsfile | 2 +- .../java/org/apache/activemq/ra/MDBTest.java | 184 ++++++++++-------- .../activemq/ExpiredAckAsyncConsumerTest.java | 8 +- .../virtual/VirtualTopicWildcardTest.java | 6 +- .../org/apache/activemq/bugs/AMQ2801Test.java | 8 +- .../org/apache/activemq/bugs/AMQ3145Test.java | 14 +- .../org/apache/activemq/bugs/AMQ3732Test.java | 6 +- .../org/apache/activemq/bugs/AMQ6815Test.java | 4 +- .../activemq/bugs/OutOfOrderTestCase.java | 8 +- .../activemq/proxy/ProxyTestSupport.java | 6 +- .../JMXRemoveQueueThenSendIgnoredTest.java | 6 +- .../TopicSubscriptionZeroPrefetchTest.java | 12 +- 12 files changed, 157 insertions(+), 107 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 9183c43049..04c0e06dff 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -99,7 +99,7 @@ pipeline { echo 'Running tests' // all tests is very very long (10 hours on Apache Jenkins) // sh 'mvn -B -e test -pl activemq-unit-tests -Dactivemq.tests=all' - sh 'mvn -B -e -fae test -Dsurefire.rerunFailingTestsCount=2' + sh 'mvn -B -e -fae test -Dsurefire.rerunFailingTestsCount=3' } post { always { diff --git a/activemq-ra/src/test/java/org/apache/activemq/ra/MDBTest.java b/activemq-ra/src/test/java/org/apache/activemq/ra/MDBTest.java index a01079ce3a..4ece0eb611 100644 --- a/activemq-ra/src/test/java/org/apache/activemq/ra/MDBTest.java +++ b/activemq-ra/src/test/java/org/apache/activemq/ra/MDBTest.java @@ -467,102 +467,114 @@ public class MDBTest { public void testErrorOnNoMessageDeliveryBrokerZeroPrefetchConfig() throws Exception { final BrokerService brokerService = new BrokerService(); - final String brokerUrl = "vm://zeroPrefetch?create=false"; - brokerService.setBrokerName("zeroPrefetch"); - brokerService.setPersistent(false); - PolicyMap policyMap = new PolicyMap(); - PolicyEntry zeroPrefetchPolicy = new PolicyEntry(); - zeroPrefetchPolicy.setQueuePrefetch(0); - policyMap.setDefaultEntry(zeroPrefetchPolicy); - brokerService.setDestinationPolicy(policyMap); - brokerService.start(); - final AtomicReference errorMessage = new AtomicReference(); - final var appender = new AbstractAppender("test", new AbstractFilter() {}, new MessageLayout(), false, new Property[0]) { - @Override - public void append(LogEvent event) { - if (event.getLevel().isMoreSpecificThan(Level.ERROR)) { - System.err.println("Event :" + event.getMessage().getFormattedMessage()); - errorMessage.set(event.getMessage().getFormattedMessage()); + try { + final String brokerUrl = "vm://zeroPrefetch?create=false"; + brokerService.setBrokerName("zeroPrefetch"); + brokerService.setPersistent(false); + PolicyMap policyMap = new PolicyMap(); + PolicyEntry zeroPrefetchPolicy = new PolicyEntry(); + zeroPrefetchPolicy.setQueuePrefetch(0); + policyMap.setDefaultEntry(zeroPrefetchPolicy); + brokerService.setDestinationPolicy(policyMap); + brokerService.start(); + + final AtomicReference errorMessage = new AtomicReference(); + final var appender = new AbstractAppender("test", new AbstractFilter() { + }, new MessageLayout(), false, new Property[0]) { + @Override + public void append(LogEvent event) { + if (event.getLevel().isMoreSpecificThan(Level.ERROR)) { + System.err.println("Event :" + event.getMessage().getFormattedMessage()); + errorMessage.set(event.getMessage().getFormattedMessage()); + } } - } - }; - appender.start(); - - final var logger = org.apache.logging.log4j.core.Logger.class.cast(LogManager.getRootLogger()); - logger.addAppender(appender); - logger.get().addAppender(appender, Level.INFO, new AbstractFilter() {}); - - ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); - Connection connection = factory.createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - MessageConsumer advisory = session.createConsumer(AdvisorySupport.getConsumerAdvisoryTopic(new ActiveMQQueue("TEST"))); - - ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); - adapter.setServerUrl(brokerUrl); - adapter.start(new StubBootstrapContext()); - - final CountDownLatch messageDelivered = new CountDownLatch(1); - - final StubMessageEndpoint endpoint = new StubMessageEndpoint() { - @Override - public void onMessage(Message message) { - super.onMessage(message); - messageDelivered.countDown(); }; - }; + appender.start(); - ActiveMQActivationSpec activationSpec = new ActiveMQActivationSpec(); - activationSpec.setDestinationType(Queue.class.getName()); - activationSpec.setDestination("TEST"); - activationSpec.setResourceAdapter(adapter); - activationSpec.validate(); + final var logger = org.apache.logging.log4j.core.Logger.class.cast( + LogManager.getRootLogger()); + logger.addAppender(appender); + logger.get().addAppender(appender, Level.INFO, new AbstractFilter() { + }); - MessageEndpointFactory messageEndpointFactory = new MessageEndpointFactory() { - @Override - public MessageEndpoint createEndpoint(XAResource resource) throws UnavailableException { - endpoint.xaresource = resource; - return endpoint; + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl); + Connection connection = factory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageConsumer advisory = session.createConsumer( + AdvisorySupport.getConsumerAdvisoryTopic(new ActiveMQQueue("TEST"))); + + ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); + adapter.setServerUrl(brokerUrl); + adapter.start(new StubBootstrapContext()); + + final CountDownLatch messageDelivered = new CountDownLatch(1); + + final StubMessageEndpoint endpoint = new StubMessageEndpoint() { + @Override + public void onMessage(Message message) { + super.onMessage(message); + messageDelivered.countDown(); + } + + ; + }; + + ActiveMQActivationSpec activationSpec = new ActiveMQActivationSpec(); + activationSpec.setDestinationType(Queue.class.getName()); + activationSpec.setDestination("TEST"); + activationSpec.setResourceAdapter(adapter); + activationSpec.validate(); + + MessageEndpointFactory messageEndpointFactory = new MessageEndpointFactory() { + @Override + public MessageEndpoint createEndpoint(XAResource resource) + throws UnavailableException { + endpoint.xaresource = resource; + return endpoint; + } + + @Override + public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException { + return true; + } + }; + + // Activate an Endpoint + adapter.endpointActivation(messageEndpointFactory, activationSpec); + + ActiveMQMessage msg = (ActiveMQMessage) advisory.receive(4000); + if (msg != null) { + assertEquals("Prefetch size hasn't been set", 0, + ((ConsumerInfo) msg.getDataStructure()).getPrefetchSize()); + } else { + fail("Consumer hasn't been created"); } - @Override - public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException { - return true; - } - }; + // Send the broker a message to that endpoint + MessageProducer producer = session.createProducer(new ActiveMQQueue("TEST")); + producer.send(session.createTextMessage("Hello!")); - // Activate an Endpoint - adapter.endpointActivation(messageEndpointFactory, activationSpec); + connection.close(); - ActiveMQMessage msg = (ActiveMQMessage)advisory.receive(4000); - if (msg != null) { - assertEquals("Prefetch size hasn't been set", 0, ((ConsumerInfo)msg.getDataStructure()).getPrefetchSize()); - } else { - fail("Consumer hasn't been created"); + // Wait for the message to be delivered. + assertFalse(messageDelivered.await(5000, TimeUnit.MILLISECONDS)); + + // Shut the Endpoint down. + adapter.endpointDeactivation(messageEndpointFactory, activationSpec); + adapter.stop(); + + assertNotNull("We got an error message", errorMessage.get()); + assertTrue("correct message: " + errorMessage.get(), + errorMessage.get().contains("zero")); + + logger.removeAppender(appender); + logger.get().removeAppender("test"); + } finally { + brokerService.stop(); } - - // Send the broker a message to that endpoint - MessageProducer producer = session.createProducer(new ActiveMQQueue("TEST")); - producer.send(session.createTextMessage("Hello!")); - - connection.close(); - - // Wait for the message to be delivered. - assertFalse(messageDelivered.await(5000, TimeUnit.MILLISECONDS)); - - // Shut the Endpoint down. - adapter.endpointDeactivation(messageEndpointFactory, activationSpec); - adapter.stop(); - - assertNotNull("We got an error message", errorMessage.get()); - assertTrue("correct message: " + errorMessage.get(), errorMessage.get().contains("zero")); - - logger.removeAppender(appender); - logger.get().removeAppender("test"); - - brokerService.stop(); } @Test diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/ExpiredAckAsyncConsumerTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/ExpiredAckAsyncConsumerTest.java index c3aeef632d..2a3c08d909 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/ExpiredAckAsyncConsumerTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/ExpiredAckAsyncConsumerTest.java @@ -99,8 +99,12 @@ public class ExpiredAckAsyncConsumerTest { @After public void tearDown() throws Exception { - connectionConsumer.close(); - connection.close(); + try { + connectionConsumer.close(); + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } broker.stop(); broker.waitUntilStopped(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/virtual/VirtualTopicWildcardTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/virtual/VirtualTopicWildcardTest.java index c3978b73ea..53ebac4564 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/broker/virtual/VirtualTopicWildcardTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/broker/virtual/VirtualTopicWildcardTest.java @@ -63,7 +63,11 @@ public class VirtualTopicWildcardTest { @After public void afer() throws Exception { - connection.close(); + try { + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } brokerService.stop(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ2801Test.java b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ2801Test.java index a1d0bc1cdf..ccc2571aaa 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ2801Test.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ2801Test.java @@ -105,8 +105,12 @@ public class AMQ2801Test @After public void tearDown() throws Exception { - conn1.close(); - conn2.close(); + try { + conn1.close(); + conn2.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } if (broker != null) { broker.stop(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3145Test.java b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3145Test.java index 81128bdac0..a5804dd7c0 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3145Test.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3145Test.java @@ -72,12 +72,16 @@ public class AMQ3145Test { @After public void tearDown() throws Exception { - if (consumer != null) { - consumer.close(); + try { + if (consumer != null) { + consumer.close(); + } + session.close(); + connection.stop(); + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped } - session.close(); - connection.stop(); - connection.close(); broker.stop(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3732Test.java b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3732Test.java index 601901be83..1c620910f4 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3732Test.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ3732Test.java @@ -71,7 +71,11 @@ public class AMQ3732Test { @After public void stopBroker() throws Exception { - connection.close(); + try { + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } broker.stop(); broker.waitUntilStopped(); diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ6815Test.java b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ6815Test.java index ad278be0bf..a98c62a2ff 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ6815Test.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/AMQ6815Test.java @@ -65,8 +65,10 @@ public class AMQ6815Test { @After public void tearDown() throws Exception { - if (connection != null) { + try { connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped } brokerService.stop(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/OutOfOrderTestCase.java b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/OutOfOrderTestCase.java index 34e3866749..3aa3b1f5b0 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/OutOfOrderTestCase.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/bugs/OutOfOrderTestCase.java @@ -68,8 +68,12 @@ public class OutOfOrderTestCase extends TestCase { } protected void tearDown() throws Exception { - session.close(); - connection.close(); + try { + session.close(); + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } brokerService.stop(); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/proxy/ProxyTestSupport.java b/activemq-unit-tests/src/test/java/org/apache/activemq/proxy/ProxyTestSupport.java index 2c884ec7ca..9cfd3e6664 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/proxy/ProxyTestSupport.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/proxy/ProxyTestSupport.java @@ -83,7 +83,11 @@ public class ProxyTestSupport extends BrokerTestSupport { protected void tearDown() throws Exception { for (Iterator iter = connections.iterator(); iter.hasNext();) { StubConnection connection = iter.next(); - connection.stop(); + try { + connection.stop(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } iter.remove(); } remoteBroker.stop(); diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/JMXRemoveQueueThenSendIgnoredTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/JMXRemoveQueueThenSendIgnoredTest.java index 7c301f535b..88a4363cd0 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/JMXRemoveQueueThenSendIgnoredTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/JMXRemoveQueueThenSendIgnoredTest.java @@ -149,7 +149,11 @@ public class JMXRemoveQueueThenSendIgnoredTest { @After public void tearDown() throws Exception { - connection.close(); + try { + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } brokerService.stop(); } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TopicSubscriptionZeroPrefetchTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TopicSubscriptionZeroPrefetchTest.java index 7f6a8b5d19..ba074f1dc2 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TopicSubscriptionZeroPrefetchTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/usecases/TopicSubscriptionZeroPrefetchTest.java @@ -285,10 +285,14 @@ public class TopicSubscriptionZeroPrefetchTest { @After public void tearDown() throws Exception { - consumer.close(); - producer.close(); - session.close(); - connection.close(); + try { + consumer.close(); + producer.close(); + session.close(); + connection.close(); + } catch (Exception e) { + //swallow any error so broker can still be stopped + } brokerService.stop(); }