You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kw...@apache.org on 2015/07/29 11:20:02 UTC

svn commit: r1693225 - in /qpid/java/trunk/systests/src: main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java test/java/org/apache/qpid/client/AsynchMessageListenerTest.java

Author: kwall
Date: Wed Jul 29 09:20:02 2015
New Revision: 1693225

URL: http://svn.apache.org/r1693225
Log:
NO-JIRA: [Java System Tests] Make uncaught exceptions occuring before test completion fail the test.

If a test leaves a thread running and it causes an exception, the test failure may be reported as a
failure of the next test.

Modified:
    qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java
    qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java
    qpid/java/trunk/systests/src/test/java/org/apache/qpid/client/AsynchMessageListenerTest.java

Modified: qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java?rev=1693225&r1=1693224&r2=1693225&view=diff
==============================================================================
--- qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java (original)
+++ qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/InternalBrokerHolder.java Wed Jul 29 09:20:02 2015
@@ -22,6 +22,7 @@ package org.apache.qpid.test.utils;
 
 import java.security.PrivilegedAction;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.security.auth.Subject;
 
@@ -35,15 +36,17 @@ import org.apache.qpid.server.util.Actio
 
 public class InternalBrokerHolder implements BrokerHolder
 {
-    private static final Logger LOGGER = LoggerFactory.getLogger(InternalBrokerHolder.class);
+    private final static Logger LOGGER = LoggerFactory.getLogger(InternalBrokerHolder.class);
+    private final static UncaughtExceptionHandler UNCAUGHT_EXCEPTION_HANDLER = new UncaughtExceptionHandler();
+    private final QpidBrokerTestCase _testCase;
+    private final Set<Integer> _portsUsedByBroker;
 
-    private Broker _broker;
+    private volatile Broker _broker;
 
-    private Set<Integer> _portsUsedByBroker;
-
-    public InternalBrokerHolder(Set<Integer> portsUsedByBroker)
+    public InternalBrokerHolder(Set<Integer> portsUsedByBroker, final QpidBrokerTestCase testCase)
     {
         _portsUsedByBroker = portsUsedByBroker;
+        _testCase = testCase;
     }
 
     @Override
@@ -51,17 +54,7 @@ public class InternalBrokerHolder implem
     {
         if (Thread.getDefaultUncaughtExceptionHandler() == null)
         {
-            Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
-            {
-                @Override
-                public void uncaughtException(final Thread t, final Throwable e)
-                {
-                    System.err.print("Thread terminated due to uncaught exception");
-                    e.printStackTrace();
-
-                    LOGGER.error("Uncaught exception from thread " + t.getName(), e);
-                }
-            });
+            Thread.setDefaultUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);
         }
 
         LOGGER.info("Starting internal broker (same JVM)");
@@ -84,7 +77,7 @@ public class InternalBrokerHolder implem
                 }
                 catch(IllegalStateException e)
                 {
-                    System.out.println("IllegalStateException occurred on broker shutdown in test " + options.getConfigProperties().get("test.name"));
+                    System.out.println("IllegalStateException occurred on broker shutdown in test " + _testCase.getName());
                     throw e;
                 }
             }
@@ -112,6 +105,11 @@ public class InternalBrokerHolder implem
         });
         waitUntilPortsAreFree();
 
+        _testCase.assertEquals(
+                "One or more uncaught exceptions occurred prior to end of this test. Check test logs.",
+                0,
+                UNCAUGHT_EXCEPTION_HANDLER.getAndResetCount());
+
         LOGGER.info("Broker instance shutdown");
     }
 
@@ -139,4 +137,29 @@ public class InternalBrokerHolder implem
         return "InternalBrokerHolder [_portsUsedByBroker=" + _portsUsedByBroker + "]";
     }
 
+    private static class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler
+    {
+        private final AtomicInteger _count = new AtomicInteger(0);
+
+        @Override
+        public void uncaughtException(final Thread t, final Throwable e)
+        {
+            System.err.print("Thread terminated due to uncaught exception");
+            e.printStackTrace();
+
+            LOGGER.error("Uncaught exception from thread {}", t.getName(), e);
+            _count.getAndIncrement();
+        }
+
+        public int getAndResetCount()
+        {
+            int count;
+            do
+            {
+                count = _count.get();
+            }
+            while(!_count.compareAndSet(count, 0));
+            return count;
+        }
+    }
 }

Modified: qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java?rev=1693225&r1=1693224&r2=1693225&view=diff
==============================================================================
--- qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java (original)
+++ qpid/java/trunk/systests/src/main/java/org/apache/qpid/test/utils/QpidBrokerTestCase.java Wed Jul 29 09:20:02 2015
@@ -1278,7 +1278,6 @@ public class QpidBrokerTestCase extends
             options.setManagementModePassword(MANAGEMENT_MODE_PASSWORD);
         }
         options.setStartupLoggedToSystemOut(false);
-        options.setConfigProperty("test.name", getClass().getSimpleName() + "-" + getName());
         return options;
     }
 
@@ -1336,7 +1335,7 @@ public class QpidBrokerTestCase extends
             if (brokerType.equals(BrokerHolder.BrokerType.INTERNAL) && !testCase.existingInternalBroker())
             {
                 testCase.setSystemProperty(BrokerProperties.PROPERTY_USE_CUSTOM_RMI_SOCKET_FACTORY, "false");
-                holder = new InternalBrokerHolder(portsUsedByBroker);
+                holder = new InternalBrokerHolder(portsUsedByBroker, testCase);
             }
             else if (!brokerType.equals(BrokerHolder.BrokerType.EXTERNAL))
             {

Modified: qpid/java/trunk/systests/src/test/java/org/apache/qpid/client/AsynchMessageListenerTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/systests/src/test/java/org/apache/qpid/client/AsynchMessageListenerTest.java?rev=1693225&r1=1693224&r2=1693225&view=diff
==============================================================================
--- qpid/java/trunk/systests/src/test/java/org/apache/qpid/client/AsynchMessageListenerTest.java (original)
+++ qpid/java/trunk/systests/src/test/java/org/apache/qpid/client/AsynchMessageListenerTest.java Wed Jul 29 09:20:02 2015
@@ -25,7 +25,6 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.jms.Connection;
-import javax.jms.ExceptionListener;
 import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.MessageConsumer;
@@ -34,7 +33,6 @@ import javax.jms.Queue;
 import javax.jms.Session;
 
 import org.apache.qpid.test.utils.QpidBrokerTestCase;
-import org.apache.qpid.util.LogMonitor;
 
 /**
  * Tests the behaviour of JMS asynchronous message listeners as provided by
@@ -232,80 +230,6 @@ public class AsynchMessageListenerTest e
         assertEquals("Unexpected number of messages received", MSG_COUNT, countingMessageListener.getReceivedCount());
     }
 
-    /**
-     * Tests the case where the message listener throws an java.lang.Error.
-     * TODO - a useful test?.
-     */
-    public void testMessageListenerThrowsError() throws Exception
-    {
-        int expectedMessages = 1;  // The error will kill the dispatcher so only one message will be delivered.
-        final CountDownLatch awaitMessages = new CountDownLatch(expectedMessages);
-        final AtomicInteger receivedCount = new AtomicInteger(0);
-        final String javaLangErrorMessageText = "MessageListener failed with java.lang.Error";
-        CountingExceptionListener countingExceptionListener = new CountingExceptionListener();
-        _consumerConnection.setExceptionListener(countingExceptionListener);
-
-        _consumer.setMessageListener(new MessageListener()
-        {
-            @Override
-            public void onMessage(Message message)
-            {
-                try
-                {
-                    throw new Error(javaLangErrorMessageText);
-                }
-                finally
-                {
-                    receivedCount.incrementAndGet();
-                    awaitMessages.countDown();
-                }
-            }
-        });
-
-        awaitMessages.await(AWAIT_MESSAGE_TIMEOUT, TimeUnit.MILLISECONDS);
-
-        assertEquals("Unexpected number of messages received", expectedMessages, receivedCount.get());
-        assertEquals("onException should NOT have been called", 0, countingExceptionListener.getErrorCount());
-
-        // Check that Error has been written to the application log.
-
-        LogMonitor _monitor = new LogMonitor(getOutputFile());
-        assertTrue("The expected message not written to log file.",
-                _monitor.waitForMessage(javaLangErrorMessageText, LOGMONITOR_TIMEOUT));
-
-        if (_consumerConnection != null)
-        {
-            try
-            {
-                _consumerConnection.close();
-            }
-            catch (JMSException e)
-            {
-                // Ignore connection close errors for this test.
-            }
-            finally
-            {
-                _consumerConnection = null;
-            }
-        }
-    }
-
-    private final class CountingExceptionListener implements ExceptionListener
-    {
-        private final AtomicInteger _errorCount = new AtomicInteger();
-
-        @Override
-        public void onException(JMSException arg0)
-        {
-            _errorCount.incrementAndGet();
-        }
-
-        public int getErrorCount()
-        {
-            return _errorCount.intValue();
-        }
-    }
-
     private final class CountingMessageListener implements MessageListener
     {
         private volatile CountDownLatch _awaitMessages;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org