|
40 | 40 | import com.rabbitmq.client.AMQP.BasicProperties; |
41 | 41 | import com.rabbitmq.client.Channel; |
42 | 42 | import com.rabbitmq.client.Consumer; |
| 43 | +import com.rabbitmq.client.DefaultConsumer; |
43 | 44 | import com.rabbitmq.client.Envelope; |
44 | 45 | import com.rabbitmq.client.PossibleAuthenticationFailureException; |
45 | 46 | import org.aopalliance.intercept.MethodInterceptor; |
@@ -849,6 +850,50 @@ public void testBatchReceiveTimedOut() throws Exception { |
849 | 850 | verifyNoMoreInteractions(listener); |
850 | 851 | } |
851 | 852 |
|
| 853 | + @Test |
| 854 | + @SuppressWarnings("unchecked") |
| 855 | + void listenerIsInterruptedOnUnsuccessfulShutdown() throws Exception { |
| 856 | + ConnectionFactory connectionFactory = mock(); |
| 857 | + Connection connection = mock(); |
| 858 | + Channel channel = mock(); |
| 859 | + given(connectionFactory.createConnection()).willReturn(connection); |
| 860 | + given(connection.createChannel(false)).willReturn(channel); |
| 861 | + willAnswer(invocation -> "1") |
| 862 | + .given(channel) |
| 863 | + .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), |
| 864 | + any(Consumer.class)); |
| 865 | + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); |
| 866 | + container.setQueueNames("some_queue"); |
| 867 | + |
| 868 | + CountDownLatch handlingLatch = new CountDownLatch(1); |
| 869 | + CountDownLatch interruptedLatch = new CountDownLatch(1); |
| 870 | + container.setMessageListener(message -> { |
| 871 | + handlingLatch.countDown(); |
| 872 | + try { |
| 873 | + Thread.sleep(2000L); |
| 874 | + } |
| 875 | + catch (InterruptedException e) { |
| 876 | + interruptedLatch.countDown(); |
| 877 | + } |
| 878 | + }); |
| 879 | + container.setShutdownTimeout(200L); |
| 880 | + container.start(); |
| 881 | + |
| 882 | + Set<BlockingQueueConsumer> consumers = TestUtils.getPropertyValue(container, "consumers", Set.class); |
| 883 | + BlockingQueueConsumer blockingQueueConsumer = consumers.iterator().next(); |
| 884 | + |
| 885 | + Map<String, DefaultConsumer> internalConsumers = |
| 886 | + TestUtils.getPropertyValue(blockingQueueConsumer, "consumers", Map.class); |
| 887 | + internalConsumers.values().iterator().next() |
| 888 | + .handleDelivery("1", new Envelope(1, false, "", ""), new BasicProperties(), new byte[] {1}); |
| 889 | + |
| 890 | + assertThat(handlingLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 891 | + |
| 892 | + container.stop(); |
| 893 | + |
| 894 | + assertThat(interruptedLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 895 | + } |
| 896 | + |
852 | 897 | private Answer<Object> messageToConsumer(final Channel mockChannel, final SimpleMessageListenerContainer container, |
853 | 898 | final boolean cancel, final CountDownLatch latch) { |
854 | 899 | return invocation -> { |
|
0 commit comments