|
| 1 | +// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("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 static com.rabbitmq.client.test.TestUtils.LatchConditions.completed; |
| 19 | +import static com.rabbitmq.client.test.TestUtils.waitAtMost; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import com.rabbitmq.client.Channel; |
| 23 | +import com.rabbitmq.client.Connection; |
| 24 | +import com.rabbitmq.client.ConnectionFactory; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
| 29 | +public class BlockedConnectionTest extends BrokerTestCase { |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @ValueSource(booleans = {true, false}) |
| 33 | + void errorInBlockListenerShouldCloseConnection(boolean nio) throws Exception { |
| 34 | + ConnectionFactory cf = TestUtils.connectionFactory(); |
| 35 | + if (nio) { |
| 36 | + cf.useNio(); |
| 37 | + } else { |
| 38 | + cf.useBlockingIo(); |
| 39 | + } |
| 40 | + Connection c = cf.newConnection(); |
| 41 | + CountDownLatch shutdownLatch = new CountDownLatch(1); |
| 42 | + c.addShutdownListener(cause -> shutdownLatch.countDown()); |
| 43 | + CountDownLatch blockedLatch = new CountDownLatch(1); |
| 44 | + c.addBlockedListener( |
| 45 | + reason -> { |
| 46 | + blockedLatch.countDown(); |
| 47 | + throw new RuntimeException("error in blocked listener!"); |
| 48 | + }, |
| 49 | + () -> {}); |
| 50 | + try { |
| 51 | + block(); |
| 52 | + Channel ch = c.createChannel(); |
| 53 | + ch.basicPublish("", "", null, "dummy".getBytes()); |
| 54 | + assertThat(blockedLatch).is(completed()); |
| 55 | + } finally { |
| 56 | + unblock(); |
| 57 | + } |
| 58 | + assertThat(shutdownLatch).is(completed()); |
| 59 | + waitAtMost(() -> !c.isOpen()); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments