|
| 1 | +// Copyright (c) 2017 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.Channel; |
| 19 | +import com.rabbitmq.client.Connection; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.concurrent.CountDownLatch; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +public class LambdaListenerCallbackTest extends BrokerTestCase { |
| 29 | + |
| 30 | + protected void releaseResources() throws IOException { |
| 31 | + try { |
| 32 | + unblock(); |
| 33 | + } catch (InterruptedException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test public void shutdownListener() throws Exception { |
| 39 | + CountDownLatch latch = new CountDownLatch(2); |
| 40 | + try(Connection connection = TestUtils.connectionFactory().newConnection()) { |
| 41 | + connection.addShutdownListener(cause -> latch.countDown()); |
| 42 | + Channel channel = connection.createChannel(); |
| 43 | + channel.addShutdownListener(cause -> latch.countDown()); |
| 44 | + } |
| 45 | + assertTrue("Connection closed, shutdown listeners should have been called", latch.await(1, TimeUnit.SECONDS)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test public void confirmListener() throws Exception { |
| 49 | + channel.confirmSelect(); |
| 50 | + CountDownLatch latch = new CountDownLatch(1); |
| 51 | + channel.addConfirmListener( |
| 52 | + (deliveryTag, multiple) -> latch.countDown(), |
| 53 | + (deliveryTag, multiple) -> {} |
| 54 | + ); |
| 55 | + channel.basicPublish("", "whatever", null, "dummy".getBytes()); |
| 56 | + assertTrue("Should have received publisher confirm", latch.await(1, TimeUnit.SECONDS)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test public void returnListener() throws Exception { |
| 60 | + CountDownLatch latch = new CountDownLatch(1); |
| 61 | + channel.addReturnListener(returnMessage -> latch.countDown()); |
| 62 | + channel.basicPublish("", "notlikelytoexist", true, null, "dummy".getBytes()); |
| 63 | + assertTrue("Should have received returned message", latch.await(1, TimeUnit.SECONDS)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test public void blockedListener() throws Exception { |
| 67 | + final CountDownLatch latch = new CountDownLatch(1); |
| 68 | + try(Connection connection = TestUtils.connectionFactory().newConnection()) { |
| 69 | + connection.addBlockedListener( |
| 70 | + reason -> { |
| 71 | + try { |
| 72 | + unblock(); |
| 73 | + } catch (InterruptedException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + }, |
| 77 | + () -> latch.countDown() |
| 78 | + ); |
| 79 | + block(); |
| 80 | + Channel ch = connection.createChannel(); |
| 81 | + ch.basicPublish("", "", null, "dummy".getBytes()); |
| 82 | + assertTrue("Should have been blocked and unblocked", latch.await(10, TimeUnit.SECONDS)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments