You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2019/12/08 18:56:50 UTC

[cxf] branch master updated: CXF-8161: fix memory leak and thread leak in JMSDestination. Adding more tests for JMSDestination first

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new dcc9c3f  CXF-8161: fix memory leak and thread leak in JMSDestination. Adding more tests for JMSDestination first
dcc9c3f is described below

commit dcc9c3f74c2fe9b8797d18296dee2e6270442481
Author: reta <dr...@gmail.com>
AuthorDate: Sun Dec 8 13:56:23 2019 -0500

    CXF-8161: fix memory leak and thread leak in JMSDestination. Adding more tests for JMSDestination first
---
 .../cxf/transport/jms/AbstractJMSTester.java       |   9 ++
 .../cxf/transport/jms/JMSDestinationTest.java      | 118 +++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
index 37acc66..a722a2d 100644
--- a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
+++ b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
@@ -25,6 +25,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Function;
 
 import javax.jms.ConnectionFactory;
 import javax.jms.DeliveryMode;
@@ -170,6 +171,7 @@ public abstract class AbstractJMSTester {
         return new JMSConduit(null, jmsConfig, bus);
     }
 
+
     protected JMSConduit setupJMSConduitWithObserver(EndpointInfo ei) throws IOException {
         JMSConduit jmsConduit = setupJMSConduit(ei);
         MessageObserver observer = new MessageObserver() {
@@ -184,6 +186,13 @@ public abstract class AbstractJMSTester {
         return jmsConduit;
     }
 
+    protected JMSDestination setupJMSDestination(EndpointInfo ei, 
+            Function<ConnectionFactory, ConnectionFactory> wrapper) throws IOException {
+        JMSConfiguration jmsConfig = JMSConfigFactory.createFromEndpointInfo(bus, ei, null);
+        jmsConfig.setConnectionFactory(wrapper.apply(cf));
+        return new JMSDestination(bus, ei, jmsConfig);
+    }
+    
     protected JMSDestination setupJMSDestination(EndpointInfo ei) throws IOException {
         JMSConfiguration jmsConfig = JMSConfigFactory.createFromEndpointInfo(bus, ei, null);
         jmsConfig.setConnectionFactory(cf);
diff --git a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
index 22a770c..54e8fd7 100644
--- a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
+++ b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSDestinationTest.java
@@ -19,7 +19,13 @@
 
 package org.apache.cxf.transport.jms;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+
 import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
 import javax.jms.DeliveryMode;
 import javax.jms.Destination;
 import javax.jms.InvalidClientIDException;
@@ -27,6 +33,7 @@ import javax.jms.JMSException;
 import javax.jms.Queue;
 import javax.jms.Topic;
 
+import org.apache.activemq.util.ServiceStopper;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.ExchangeImpl;
 import org.apache.cxf.message.Message;
@@ -47,6 +54,34 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 public class JMSDestinationTest extends AbstractJMSTester {
+    private static final class FaultyConnectionFactory implements ConnectionFactory {
+        private final ConnectionFactory delegate;
+        private final AtomicInteger latch;
+        
+        private FaultyConnectionFactory(ConnectionFactory delegate, int faults) {
+            this.delegate = delegate;
+            this.latch = new AtomicInteger(faults);
+        }
+        
+        @Override
+        public Connection createConnection() throws JMSException {
+            if (latch.getAndDecrement() == 0) {
+                return delegate.createConnection();
+            } else {
+                throw new JMSException("createConnection() failed (simulated)");
+            }
+        }
+
+        @Override
+        public Connection createConnection(String userName, String password) throws JMSException {
+            if (latch.decrementAndGet() == 0) {
+                return delegate.createConnection(userName, password);
+            } else {
+                throw new JMSException("createConnection(userName, password) failed (simulated)");
+            }
+        }
+        
+    }
 
     @Test
     public void testGetConfigurationFromWSDL() throws Exception {
@@ -376,6 +411,89 @@ public class JMSDestinationTest extends AbstractJMSTester {
         destination.shutdown();
     }
 
+    @Test
+    public void testMessageObserverExceptionHandling() throws Exception {
+        final CountDownLatch latch = new CountDownLatch(1); 
+        EndpointInfo ei = setupServiceInfo("HelloWorldPubSubService", "HelloWorldPubSubPort");
+        JMSConduit conduit = setupJMSConduitWithObserver(ei);
+
+        JMSDestination destination = setupJMSDestination(ei);
+        destination.setMessageObserver(new MessageObserver() {
+            @Override
+            public void onMessage(Message message) {
+                try {
+                    throw new RuntimeException("Error!");
+                } finally {
+                    latch.countDown();
+                }
+            }
+        });
+        
+        final Message outMessage = createMessage();
+        Thread.sleep(500L);
+
+        sendOneWayMessage(conduit, outMessage);
+        latch.await(5, TimeUnit.SECONDS);
+
+        conduit.close();
+        destination.shutdown();
+    }
+    
+    @Test
+    public void testConnectionFactoryExceptionHandling() throws Exception {
+        EndpointInfo ei = setupServiceInfo("HelloWorldPubSubService", "HelloWorldPubSubPort");
+        final Function<ConnectionFactory, ConnectionFactory> wrapper = 
+            new Function<ConnectionFactory, ConnectionFactory>() {
+                @Override
+                public ConnectionFactory apply(ConnectionFactory cf) {
+                    return new FaultyConnectionFactory(cf, 3);
+                }
+            };
+        JMSConduit conduit = setupJMSConduitWithObserver(ei);
+        JMSDestination destination = setupJMSDestination(ei, wrapper);
+        destination.getJmsConfig().setRetryInterval(1000);
+        destination.setMessageObserver(createMessageObserver());
+        
+        final Message outMessage = createMessage();
+        Thread.sleep(4000L);
+        
+        sendOneWayMessage(conduit, outMessage);
+        
+        // wait for the message to be got from the destination,
+        // create the thread to handler the Destination incoming message
+        Message inMessage = waitForReceiveDestMessage();
+        verifyReceivedMessage(inMessage);
+
+        conduit.close();
+        destination.shutdown();
+    }
+
+    @Test
+    public void testBrokerExceptionHandling() throws Exception {
+        EndpointInfo ei = setupServiceInfo("HelloWorldPubSubService", "HelloWorldPubSubPort");
+        JMSConduit conduit = setupJMSConduitWithObserver(ei);
+        JMSDestination destination = setupJMSDestination(ei);
+        destination.getJmsConfig().setRetryInterval(1000);
+        destination.setMessageObserver(createMessageObserver());
+        
+        Thread.sleep(500L);
+        broker.stopAllConnectors(new ServiceStopper());
+        
+        broker.startAllConnectors();
+        Thread.sleep(2000L);
+        
+        final Message outMessage = createMessage();
+        sendOneWayMessage(conduit, outMessage);
+        
+        // wait for the message to be got from the destination,
+        // create the thread to handler the Destination incoming message
+        Message inMessage = waitForReceiveDestMessage();
+        verifyReceivedMessage(inMessage);
+
+        conduit.close();
+        destination.shutdown();
+    }
+
     private String getQueueName(String exName) {
         if (exName == null) {
             return null;