Repository: qpid-broker-j Updated Branches: refs/heads/master efb7ec0c1 -> f96d50b9f
QPID-6933: [System Tests] Refactor exception listener tests as JMS 1.1 system test Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/f98b029e Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/f98b029e Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/f98b029e Branch: refs/heads/master Commit: f98b029ee6e3daba7fc702fcf46abb9c0a44f2a7 Parents: efb7ec0 Author: Alex Rudyy <[email protected]> Authored: Wed Dec 27 19:51:12 2017 +0000 Committer: Alex Rudyy <[email protected]> Committed: Wed Dec 27 19:51:12 2017 +0000 ---------------------------------------------------------------------- .../connection/ExceptionListenerTest.java | 161 +++++++++++++++++++ .../connection/ExceptionListenerTest.java | 123 +------------- 2 files changed, 164 insertions(+), 120 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/f98b029e/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/connection/ExceptionListenerTest.java ---------------------------------------------------------------------- diff --git a/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/connection/ExceptionListenerTest.java b/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/connection/ExceptionListenerTest.java new file mode 100644 index 0000000..2dd23f1 --- /dev/null +++ b/systests/qpid-systests-jms_1.1/src/test/java/org/apache/qpid/systests/jms_1_1/connection/ExceptionListenerTest.java @@ -0,0 +1,161 @@ +/* + * 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.qpid.systests.jms_1_1.connection; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeThat; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import javax.jms.Connection; +import javax.jms.ExceptionListener; +import javax.jms.IllegalStateException; +import javax.jms.JMSException; + +import org.junit.Test; + +import org.apache.qpid.systests.JmsTestBase; + +public class ExceptionListenerTest extends JmsTestBase +{ + @Test + public void testExceptionListenerHearsBrokerShutdown() throws Exception + { + assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true))); + + final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); + final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>(); + Connection connection = getConnection(); + try + { + connection.setExceptionListener(exception -> { + exceptionHolder.set(exception); + exceptionReceivedLatch.countDown(); + }); + + getBrokerAdmin().restart(); + + assertTrue("Exception was not propagated into exception listener in timely manner", + exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS)); + assertNotNull("Unexpected exception", exceptionHolder.get()); + } + finally + { + connection.close(); + } + } + + @Test + public void testExceptionListenerClosesConnectionIsAllowed() throws Exception + { + assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true))); + + final Connection connection = getConnection(); + try + { + final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); + final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>(); + final AtomicReference<Throwable> unexpectedExceptionHolder = new AtomicReference<>(); + final ExceptionListener listener = exception -> { + exceptionHolder.set(exception); + try + { + connection.close(); + // PASS + } + catch (Throwable t) + { + unexpectedExceptionHolder.set(t); + } + finally + { + exceptionReceivedLatch.countDown(); + } + }; + connection.setExceptionListener(listener); + + getBrokerAdmin().restart(); + + assertTrue("Exception was not propagated into exception listener in timely manner", + exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS)); + assertNotNull("Unexpected exception", exceptionHolder.get()); + assertNull("Connection#close() should not have thrown exception", unexpectedExceptionHolder.get()); + } + finally + { + connection.close(); + } + } + + @Test + public void testExceptionListenerStopsConnection_ThrowsIllegalStateException() throws Exception + { + assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true))); + + final Connection connection = getConnection(); + try + { + final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); + final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>(); + final AtomicReference<Throwable> unexpectedExceptionHolder = new AtomicReference<>(); + final ExceptionListener listener = exception -> { + exceptionHolder.set(exception); + try + { + connection.stop(); + fail("Exception not thrown"); + } + catch (IllegalStateException ise) + { + // PASS + } + catch (Throwable t) + { + unexpectedExceptionHolder.set(t); + } + finally + { + exceptionReceivedLatch.countDown(); + } + }; + connection.setExceptionListener(listener); + + getBrokerAdmin().restart(); + + assertTrue("Exception was not propagated into exception listener in timely manner", + exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS)); + assertNotNull("Unexpected exception", exceptionHolder.get()); + assertNull("Connection#stop() should not have thrown exception", unexpectedExceptionHolder.get()); + } + finally + { + connection.close(); + } + } + +} http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/f98b029e/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ExceptionListenerTest.java ---------------------------------------------------------------------- diff --git a/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ExceptionListenerTest.java b/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ExceptionListenerTest.java index 4f1bacf..047c39e 100644 --- a/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ExceptionListenerTest.java +++ b/systests/src/test/java/org/apache/qpid/test/unit/client/connection/ExceptionListenerTest.java @@ -25,12 +25,10 @@ import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import javax.jms.Connection; import javax.jms.ExceptionListener; -import javax.jms.IllegalStateException; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; @@ -41,130 +39,15 @@ import javax.jms.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.qpid.AMQConnectionClosedException; import org.apache.qpid.jms.ConnectionURL; import org.apache.qpid.test.utils.QpidBrokerTestCase; -import org.apache.qpid.transport.ConnectionException; - +/* + * AMQP 0.x client specific test + */ public class ExceptionListenerTest extends QpidBrokerTestCase { private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionListenerTest.class); - private volatile Throwable _lastExceptionListenerException = null; - - public void testExceptionListenerHearsBrokerShutdown() throws Exception - { - final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); - final AtomicInteger exceptionCounter = new AtomicInteger(0); - final ExceptionListener listener = new ExceptionListener() - { - @Override - public void onException(JMSException exception) - { - exceptionCounter.incrementAndGet(); - _lastExceptionListenerException = exception; - exceptionReceivedLatch.countDown(); - } - }; - - Connection connection = getConnection(); - connection.setExceptionListener(listener); - - stopDefaultBroker(); - - exceptionReceivedLatch.await(10, TimeUnit.SECONDS); - - assertEquals("Unexpected number of exceptions received", 1, exceptionCounter.intValue()); - LOGGER.debug("exception was", _lastExceptionListenerException); - if(!isBroker10()) - { - assertNotNull("Exception should have cause", _lastExceptionListenerException.getCause()); - Class<? extends Exception> expectedExceptionClass = isBroker010() ? ConnectionException.class : AMQConnectionClosedException.class; - assertEquals(expectedExceptionClass, _lastExceptionListenerException.getCause().getClass()); - } - } - - /** - * It is reasonable for an application to perform Connection#close within the exception - * listener. This test verifies that close is allowed, and proceeds without generating - * further exceptions. - */ - public void testExceptionListenerClosesConnection_IsAllowed() throws Exception - { - final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); - final Connection connection = getConnection(); - final ExceptionListener listener = new ExceptionListener() - { - @Override - public void onException(JMSException exception) - { - try - { - connection.close(); - // PASS - } - catch (Throwable t) - { - _lastExceptionListenerException = t; - } - finally - { - exceptionReceivedLatch.countDown(); - } - } - }; - connection.setExceptionListener(listener); - - - stopDefaultBroker(); - - boolean exceptionReceived = exceptionReceivedLatch.await(10, TimeUnit.SECONDS); - assertTrue("Exception listener did not hear exception within timeout", exceptionReceived); - assertNull("Connection#close() should not have thrown exception", _lastExceptionListenerException); - } - - /** - * Spring's SingleConnectionFactory installs an ExceptionListener that calls stop() - * and ignores any IllegalStateException that result. This test serves to test this - * scenario. - */ - public void testExceptionListenerStopsConnection_ThrowsIllegalStateException() throws Exception - { - final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1); - final Connection connection = getConnection(); - final ExceptionListener listener = new ExceptionListener() - { - @Override - public void onException(JMSException exception) - { - try - { - connection.stop(); - fail("Exception not thrown"); - } - catch (IllegalStateException ise) - { - // PASS - } - catch (Throwable t) - { - _lastExceptionListenerException = t; - } - finally - { - exceptionReceivedLatch.countDown(); - } - } - }; - connection.setExceptionListener(listener); - - stopDefaultBroker(); - - boolean exceptionReceived = exceptionReceivedLatch.await(10, TimeUnit.SECONDS); - assertTrue("Exception listener did not hear exception within timeout", exceptionReceived); - assertNull("Connection#stop() should not have thrown unexpected exception", _lastExceptionListenerException); - } - /** * This test reproduces a deadlock that was the subject of a support call. A Spring based * application was using SingleConnectionFactory. It installed an ExceptionListener that --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
