|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 21 | +import static org.mockito.BDDMockito.willReturn; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.reset; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import java.util.function.BiConsumer; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import org.apache.commons.logging.LogFactory; |
| 35 | +import org.apache.kafka.clients.consumer.Consumer; |
| 36 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 37 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 38 | +import org.apache.kafka.common.TopicPartition; |
| 39 | +import org.junit.jupiter.api.BeforeEach; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | + |
| 42 | +import org.springframework.classify.BinaryExceptionClassifier; |
| 43 | +import org.springframework.core.log.LogAccessor; |
| 44 | +import org.springframework.kafka.KafkaException; |
| 45 | +import org.springframework.util.backoff.BackOff; |
| 46 | +import org.springframework.util.backoff.FixedBackOff; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Antonio Tomac |
| 50 | + * @since 3.0.9 |
| 51 | + * |
| 52 | + */ |
| 53 | +class ErrorHandlingUtilsTest { |
| 54 | + |
| 55 | + private final Exception thrownException = new RuntimeException("initial cause"); |
| 56 | + private final Consumer<?, ?> consumer = mock(Consumer.class); |
| 57 | + private final MessageListenerContainer container = mock(MessageListenerContainer.class); |
| 58 | + private final Runnable listener = mock(Runnable.class); |
| 59 | + private final BackOff backOff = new FixedBackOff(1000, 3); |
| 60 | + private final CommonErrorHandler seeker = mock(CommonErrorHandler.class); |
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + private final BiConsumer<ConsumerRecords<?, ?>, Exception> recoverer = mock(BiConsumer.class); |
| 63 | + private final LogAccessor logger = new LogAccessor(LogFactory.getLog(ErrorHandlingUtilsTest.class)); |
| 64 | + private final List<RetryListener> retryListeners = new ArrayList<>(); |
| 65 | + private final BinaryExceptionClassifier classifier = BinaryExceptionClassifier.defaultClassifier(); |
| 66 | + |
| 67 | + private final ConsumerRecords<?, ?> consumerRecords = recordsOf( |
| 68 | + new ConsumerRecord<>("foo", 0, 0L, "a", "a"), |
| 69 | + new ConsumerRecord<>("foo", 1, 0L, "b", "b") |
| 70 | + ); |
| 71 | + |
| 72 | + @SafeVarargs |
| 73 | + private <K, V> ConsumerRecords<K, V> recordsOf(ConsumerRecord<K, V>... records) { |
| 74 | + return new ConsumerRecords<>( |
| 75 | + Arrays.stream(records).collect(Collectors.groupingBy( |
| 76 | + (cr) -> new TopicPartition(cr.topic(), cr.partition()) |
| 77 | + )) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @BeforeEach |
| 82 | + public void resetMocks() { |
| 83 | + reset(consumer, container, listener, seeker, recoverer); |
| 84 | + willReturn(true).given(container).isRunning(); |
| 85 | + willReturn(false).given(container).isPauseRequested(); |
| 86 | + } |
| 87 | + |
| 88 | + private void doRetries() { |
| 89 | + ErrorHandlingUtils.retryBatch( |
| 90 | + thrownException, consumerRecords, consumer, container, listener, backOff, |
| 91 | + seeker, recoverer, logger, KafkaException.Level.INFO, retryListeners, |
| 92 | + classifier, true |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private long execDurationOf(Runnable runnable) { |
| 97 | + long start = System.currentTimeMillis(); |
| 98 | + runnable.run(); |
| 99 | + long end = System.currentTimeMillis(); |
| 100 | + return end - start; |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testStopRetriesWhenNotRunning() { |
| 105 | + willReturn(false).given(container).isRunning(); |
| 106 | + assertThatThrownBy(this::doRetries) |
| 107 | + .isInstanceOf(KafkaException.class) |
| 108 | + .message().isEqualTo("Container stopped during retries"); |
| 109 | + verifyNoInteractions(seeker, listener, recoverer); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testOneSuccessfulRetry() { |
| 114 | + long duration = execDurationOf(this::doRetries); |
| 115 | + assertThat(duration).as("duration of one round of sleep").isGreaterThanOrEqualTo(1000L); |
| 116 | + verifyNoInteractions(seeker, recoverer); |
| 117 | + verify(listener, times(1)).run(); |
| 118 | + verifyNoInteractions(seeker, recoverer); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void stopRetriesWhenContainerIsPaused() { |
| 123 | + willReturn(true).given(container).isPauseRequested(); |
| 124 | + long duration = execDurationOf(() -> |
| 125 | + assertThatThrownBy(this::doRetries) |
| 126 | + .isInstanceOf(KafkaException.class) |
| 127 | + .message().isEqualTo("Container paused requested during retries") |
| 128 | + ); |
| 129 | + assertThat(duration) |
| 130 | + .as("duration should not be full retry interval") |
| 131 | + .isLessThan(1000L); |
| 132 | + verify(seeker).handleBatch(thrownException, consumerRecords, consumer, container, ErrorHandlingUtils.NO_OP); |
| 133 | + verifyNoInteractions(listener, recoverer); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void stopRetriesWhenPartitionIsPaused() { |
| 138 | + willReturn(true).given(container).isPartitionPauseRequested(new TopicPartition("foo", 1)); |
| 139 | + long duration = execDurationOf(() -> |
| 140 | + assertThatThrownBy(this::doRetries) |
| 141 | + .isInstanceOf(KafkaException.class) |
| 142 | + .message().isEqualTo("Container paused requested during retries") |
| 143 | + ); |
| 144 | + assertThat(duration) |
| 145 | + .as("duration should not be full retry interval") |
| 146 | + .isLessThan(1000L); |
| 147 | + verify(seeker).handleBatch(thrownException, consumerRecords, consumer, container, ErrorHandlingUtils.NO_OP); |
| 148 | + verifyNoInteractions(listener, recoverer); |
| 149 | + } |
| 150 | +} |
0 commit comments