|
110 | 110 | * @author Mohammad Hewedy
|
111 | 111 | * @author Yansong Ren
|
112 | 112 | * @author Tim Bourquin
|
| 113 | + * @author Jeonggi Kim |
113 | 114 | */
|
114 | 115 | public class SimpleMessageListenerContainerTests {
|
115 | 116 |
|
@@ -784,6 +785,59 @@ void testWithConsumerStartWhenNotActive() {
|
784 | 785 | assertThat(start.getCount()).isEqualTo(0L);
|
785 | 786 | }
|
786 | 787 |
|
| 788 | + @Test |
| 789 | + public void testBatchReceiveTimedOut() throws Exception { |
| 790 | + ConnectionFactory connectionFactory = mock(ConnectionFactory.class); |
| 791 | + Connection connection = mock(Connection.class); |
| 792 | + Channel channel = mock(Channel.class); |
| 793 | + given(connectionFactory.createConnection()).willReturn(connection); |
| 794 | + given(connection.createChannel(false)).willReturn(channel); |
| 795 | + final AtomicReference<Consumer> consumer = new AtomicReference<>(); |
| 796 | + willAnswer(invocation -> { |
| 797 | + consumer.set(invocation.getArgument(6)); |
| 798 | + consumer.get().handleConsumeOk("1"); |
| 799 | + return "1"; |
| 800 | + }).given(channel) |
| 801 | + .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), |
| 802 | + any(Consumer.class)); |
| 803 | + final CountDownLatch latch = new CountDownLatch(2); |
| 804 | + willAnswer(invocation -> { |
| 805 | + latch.countDown(); |
| 806 | + return null; |
| 807 | + }).given(channel).basicAck(anyLong(), anyBoolean()); |
| 808 | + |
| 809 | + final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); |
| 810 | + container.setAfterReceivePostProcessors(msg -> null); |
| 811 | + container.setQueueNames("foo"); |
| 812 | + MessageListener listener = mock(BatchMessageListener.class); |
| 813 | + container.setMessageListener(listener); |
| 814 | + container.setBatchSize(3); |
| 815 | + container.setConsumerBatchEnabled(true); |
| 816 | + container.setReceiveTimeout(10); |
| 817 | + container.setBatchReceiveTimeout(20); |
| 818 | + container.start(); |
| 819 | + |
| 820 | + BasicProperties props = new BasicProperties(); |
| 821 | + byte[] payload = "baz".getBytes(); |
| 822 | + Envelope envelope = new Envelope(1L, false, "foo", "bar"); |
| 823 | + consumer.get().handleDelivery("1", envelope, props, payload); |
| 824 | + envelope = new Envelope(2L, false, "foo", "bar"); |
| 825 | + consumer.get().handleDelivery("1", envelope, props, payload); |
| 826 | + // waiting for batch receive timed out |
| 827 | + Thread.sleep(20); |
| 828 | + envelope = new Envelope(3L, false, "foo", "bar"); |
| 829 | + consumer.get().handleDelivery("1", envelope, props, payload); |
| 830 | + assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); |
| 831 | + verify(channel, never()).basicAck(eq(1), anyBoolean()); |
| 832 | + verify(channel).basicAck(2, true); |
| 833 | + verify(channel, never()).basicAck(eq(2), anyBoolean()); |
| 834 | + verify(channel).basicAck(3, true); |
| 835 | + container.stop(); |
| 836 | + verify(listener).containerAckMode(AcknowledgeMode.AUTO); |
| 837 | + verify(listener).isAsyncReplies(); |
| 838 | + verifyNoMoreInteractions(listener); |
| 839 | + } |
| 840 | + |
787 | 841 | private Answer<Object> messageToConsumer(final Channel mockChannel, final SimpleMessageListenerContainer container,
|
788 | 842 | final boolean cancel, final CountDownLatch latch) {
|
789 | 843 | return invocation -> {
|
|
0 commit comments