|
16 | 16 | package com.rabbitmq.client.test.functional;
|
17 | 17 |
|
18 | 18 | import com.rabbitmq.client.*;
|
19 |
| -import com.rabbitmq.client.impl.recovery.AutorecoveringChannel; |
20 |
| -import com.rabbitmq.client.impl.recovery.AutorecoveringConnection; |
21 |
| -import com.rabbitmq.client.impl.recovery.ConsumerRecoveryListener; |
22 |
| -import com.rabbitmq.client.impl.recovery.QueueRecoveryListener; |
| 19 | +import com.rabbitmq.client.impl.recovery.*; |
23 | 20 | import com.rabbitmq.client.test.BrokerTestCase;
|
24 | 21 | import com.rabbitmq.tools.Host;
|
25 | 22 | import org.junit.Test;
|
26 | 23 |
|
27 | 24 | import java.io.IOException;
|
28 |
| -import java.util.ArrayList; |
29 |
| -import java.util.Arrays; |
30 |
| -import java.util.List; |
31 |
| -import java.util.UUID; |
| 25 | +import java.lang.reflect.Field; |
| 26 | +import java.util.*; |
32 | 27 | import java.util.concurrent.CountDownLatch;
|
33 | 28 | import java.util.concurrent.TimeUnit;
|
34 | 29 | import java.util.concurrent.TimeoutException;
|
@@ -637,6 +632,38 @@ public void handleDelivery(String consumerTag,
|
637 | 632 | publishingConnection.abort();
|
638 | 633 | }
|
639 | 634 |
|
| 635 | + @Test public void consumersAreRemovedFromConnectionWhenChannelIsClosed() throws Exception { |
| 636 | + AutorecoveringConnection connection = newRecoveringConnection(true); |
| 637 | + try { |
| 638 | + Field consumersField = AutorecoveringConnection.class.getDeclaredField("consumers"); |
| 639 | + consumersField.setAccessible(true); |
| 640 | + Map<?, ?> connectionConsumers = (Map<?, ?>) consumersField.get(connection); |
| 641 | + |
| 642 | + Channel channel1 = connection.createChannel(); |
| 643 | + Channel channel2 = connection.createChannel(); |
| 644 | + |
| 645 | + assertEquals(0, connectionConsumers.size()); |
| 646 | + |
| 647 | + String queue = channel1.queueDeclare().getQueue(); |
| 648 | + |
| 649 | + channel1.basicConsume(queue, true, new HashMap<String, Object>(), new DefaultConsumer(channel1)); |
| 650 | + assertEquals(1, connectionConsumers.size()); |
| 651 | + channel1.basicConsume(queue, true, new HashMap<String, Object>(), new DefaultConsumer(channel1)); |
| 652 | + assertEquals(2, connectionConsumers.size()); |
| 653 | + |
| 654 | + channel2.basicConsume(queue, true, new HashMap<String, Object>(), new DefaultConsumer(channel2)); |
| 655 | + assertEquals(3, connectionConsumers.size()); |
| 656 | + |
| 657 | + channel1.close(); |
| 658 | + assertEquals(3 - 2, connectionConsumers.size()); |
| 659 | + |
| 660 | + channel2.close(); |
| 661 | + assertEquals(0, connectionConsumers.size()); |
| 662 | + } finally { |
| 663 | + connection.abort(); |
| 664 | + } |
| 665 | + } |
| 666 | + |
640 | 667 | private void assertConsumerCount(int exp, String q) throws IOException {
|
641 | 668 | assertEquals(exp, channel.queueDeclarePassive(q).getConsumerCount());
|
642 | 669 | }
|
|
0 commit comments