|
| 1 | +// Copyright (c) 2018-Present Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import com.rabbitmq.client.AMQP; |
| 19 | +import com.rabbitmq.client.Channel; |
| 20 | +import com.rabbitmq.client.Connection; |
| 21 | +import com.rabbitmq.client.ConnectionFactory; |
| 22 | +import com.rabbitmq.client.Consumer; |
| 23 | +import com.rabbitmq.client.DefaultConsumer; |
| 24 | +import com.rabbitmq.client.Envelope; |
| 25 | +import com.rabbitmq.client.impl.StrictExceptionHandler; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | +import static org.junit.Assert.assertThat; |
| 33 | +import static org.junit.Assert.fail; |
| 34 | + |
| 35 | +public class StrictExceptionHandlerTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void tooLongClosingMessage() throws Exception { |
| 39 | + ConnectionFactory cf = TestUtils.connectionFactory(); |
| 40 | + final CountDownLatch latch = new CountDownLatch(1); |
| 41 | + cf.setExceptionHandler(new StrictExceptionHandler() { |
| 42 | + @Override |
| 43 | + public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) { |
| 44 | + try { |
| 45 | + super.handleConsumerException(channel, exception, consumer, consumerTag, methodName); |
| 46 | + } catch (IllegalArgumentException e) { |
| 47 | + fail("No exception should caught"); |
| 48 | + } |
| 49 | + latch.countDown(); |
| 50 | + } |
| 51 | + }); |
| 52 | + Connection c = null; |
| 53 | + try { |
| 54 | + c = cf.newConnection(); |
| 55 | + Channel channel = c.createChannel(); |
| 56 | + String queue = channel.queueDeclare().getQueue(); |
| 57 | + channel.basicConsume(queue, |
| 58 | + new VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName( |
| 59 | + channel |
| 60 | + )); |
| 61 | + channel.basicPublish("", queue, null, new byte[0]); |
| 62 | + assertThat(latch.await(5, TimeUnit.SECONDS), is(true)); |
| 63 | + } finally { |
| 64 | + if (c != null) { |
| 65 | + c.close(); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName |
| 71 | + extends DefaultConsumer { |
| 72 | + |
| 73 | + public VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName( |
| 74 | + Channel channel) { |
| 75 | + super(channel); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) { |
| 80 | + throw new RuntimeException(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments