|
16 | 16 |
|
17 | 17 | package org.springframework.kafka.listener;
|
18 | 18 |
|
| 19 | + |
19 | 20 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 21 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
21 | 23 | import static org.mockito.ArgumentMatchers.any;
|
22 | 24 | import static org.mockito.BDDMockito.given;
|
23 | 25 | import static org.mockito.BDDMockito.willAnswer;
|
| 26 | +import static org.mockito.BDDMockito.willThrow; |
24 | 27 | import static org.mockito.Mockito.inOrder;
|
25 | 28 | import static org.mockito.Mockito.mock;
|
26 | 29 | import static org.mockito.Mockito.times;
|
27 | 30 | import static org.mockito.Mockito.verify;
|
28 | 31 | import static org.mockito.Mockito.verifyNoMoreInteractions;
|
29 | 32 |
|
| 33 | +import java.lang.reflect.Field; |
30 | 34 | import java.util.ArrayList;
|
31 | 35 | import java.util.Collections;
|
32 | 36 | import java.util.HashMap;
|
|
42 | 46 | import org.mockito.InOrder;
|
43 | 47 |
|
44 | 48 | import org.springframework.kafka.KafkaException;
|
| 49 | +import org.springframework.util.ReflectionUtils; |
45 | 50 | import org.springframework.util.backoff.FixedBackOff;
|
46 | 51 |
|
47 | 52 | /**
|
@@ -205,4 +210,35 @@ void rePauseOnRebalance() {
|
205 | 210 | verifyNoMoreInteractions(consumer);
|
206 | 211 | }
|
207 | 212 |
|
| 213 | + @Test |
| 214 | + void resetRetryingFlagOnExceptionFromRetryBatch() { |
| 215 | + FallbackBatchErrorHandler eh = new FallbackBatchErrorHandler(new FixedBackOff(0L, 1L), (consumerRecord, e) -> { }); |
| 216 | + |
| 217 | + Consumer<?, ?> consumer = mock(Consumer.class); |
| 218 | + // KafkaException could be thrown from SeekToCurrentBatchErrorHandler, but it is hard to mock |
| 219 | + KafkaException exception = new KafkaException("Failed consumer.resume()"); |
| 220 | + willThrow(exception).given(consumer).resume(any()); |
| 221 | + |
| 222 | + MessageListenerContainer container = mock(MessageListenerContainer.class); |
| 223 | + given(container.isRunning()).willReturn(true); |
| 224 | + |
| 225 | + Map<TopicPartition, List<ConsumerRecord<Object, Object>>> map = new HashMap<>(); |
| 226 | + map.put(new TopicPartition("foo", 0), |
| 227 | + Collections.singletonList(new ConsumerRecord<>("foo", 0, 0L, "foo", "bar"))); |
| 228 | + ConsumerRecords<?, ?> records = new ConsumerRecords<>(map); |
| 229 | + |
| 230 | + assertThatThrownBy(() -> eh.handle(new RuntimeException(), records, consumer, container, () -> { })) |
| 231 | + .isSameAs(exception); |
| 232 | + |
| 233 | + assertThat(getRetryingFieldValue(eh)) |
| 234 | + .withFailMessage("retrying field was not reset to false") |
| 235 | + .isFalse(); |
| 236 | + } |
| 237 | + |
| 238 | + private boolean getRetryingFieldValue(FallbackBatchErrorHandler errorHandler) { |
| 239 | + Field field = ReflectionUtils.findField(FallbackBatchErrorHandler.class, "retrying"); |
| 240 | + ReflectionUtils.makeAccessible(field); |
| 241 | + ThreadLocal<Boolean> value = (ThreadLocal<Boolean>) ReflectionUtils.getField(field, errorHandler); |
| 242 | + return value.get(); |
| 243 | + } |
208 | 244 | }
|
0 commit comments