|
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
20 | 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
21 | 22 | import static org.mockito.BDDMockito.given; |
22 | 23 | import static org.mockito.BDDMockito.willAnswer; |
23 | 24 | import static org.mockito.Mockito.inOrder; |
|
34 | 35 | import java.util.concurrent.TimeUnit; |
35 | 36 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 37 | import java.util.concurrent.atomic.AtomicInteger; |
| 38 | +import java.util.concurrent.atomic.AtomicReference; |
37 | 39 |
|
38 | 40 | import org.apache.kafka.clients.consumer.Consumer; |
39 | 41 | import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; |
@@ -116,6 +118,45 @@ public void discardRemainingRecordsFromPollAndSeek() throws Exception { |
116 | 118 | assertThat(((ListenerExecutionFailedException) this.config.ehException).getGroupId()).isEqualTo(CONTAINER_ID); |
117 | 119 | } |
118 | 120 |
|
| 121 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 122 | + @Test |
| 123 | + public void verifyCorrectContainer() throws InterruptedException { |
| 124 | + ConsumerFactory consumerFactory = mock(ConsumerFactory.class); |
| 125 | + final Consumer consumer = mock(Consumer.class); |
| 126 | + AtomicBoolean first = new AtomicBoolean(true); |
| 127 | + willAnswer(invocation -> { |
| 128 | + if (first.getAndSet(false)) { |
| 129 | + throw new IllegalStateException("intentional"); |
| 130 | + } |
| 131 | + Thread.sleep(50); |
| 132 | + return new ConsumerRecords(Collections.emptyMap()); |
| 133 | + }).given(consumer).poll(any()); |
| 134 | + given(consumerFactory.createConsumer(anyString(), anyString(), anyString(), any())) |
| 135 | + .willReturn(consumer); |
| 136 | + ContainerProperties containerProperties = new ContainerProperties("foo"); |
| 137 | + containerProperties.setGroupId("grp"); |
| 138 | + containerProperties.setMessageListener((BatchMessageListener) record -> { }); |
| 139 | + containerProperties.setMissingTopicsFatal(false); |
| 140 | + ConcurrentMessageListenerContainer container = new ConcurrentMessageListenerContainer<>(consumerFactory, |
| 141 | + containerProperties); |
| 142 | + AtomicReference<MessageListenerContainer> parent = new AtomicReference<>(); |
| 143 | + CountDownLatch latch = new CountDownLatch(1); |
| 144 | + container.setBatchErrorHandler(new ContainerAwareBatchErrorHandler() { |
| 145 | + |
| 146 | + @Override |
| 147 | + public void handle(Exception thrownException, ConsumerRecords<?, ?> data, Consumer<?, ?> consumer, |
| 148 | + MessageListenerContainer container) { |
| 149 | + |
| 150 | + parent.set(container); |
| 151 | + latch.countDown(); |
| 152 | + } |
| 153 | + }); |
| 154 | + container.start(); |
| 155 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 156 | + container.stop(); |
| 157 | + assertThat(parent.get()).isSameAs(container); |
| 158 | + } |
| 159 | + |
119 | 160 | @Configuration |
120 | 161 | @EnableKafka |
121 | 162 | public static class Config { |
|
0 commit comments