|
| 1 | +/* |
| 2 | + * Copyright 2022 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.retrytopic; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.BDDMockito.willAnswer; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | + |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.function.Consumer; |
| 30 | + |
| 31 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.mockito.ArgumentCaptor; |
| 34 | + |
| 35 | +import org.springframework.beans.factory.annotation.Autowired; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.annotation.Configuration; |
| 38 | +import org.springframework.kafka.annotation.EnableKafka; |
| 39 | +import org.springframework.kafka.annotation.KafkaListener; |
| 40 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 41 | +import org.springframework.kafka.core.ConsumerFactory; |
| 42 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 43 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 44 | +import org.springframework.kafka.core.KafkaOperations; |
| 45 | +import org.springframework.kafka.core.KafkaTemplate; |
| 46 | +import org.springframework.kafka.support.SendResult; |
| 47 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 48 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 49 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 50 | +import org.springframework.scheduling.TaskScheduler; |
| 51 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
| 52 | +import org.springframework.test.annotation.DirtiesContext; |
| 53 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 54 | + |
| 55 | +/** |
| 56 | + * @author Gary Russell |
| 57 | + * @since 2.9.2 |
| 58 | + * |
| 59 | + */ |
| 60 | +@DirtiesContext |
| 61 | +@SpringJUnitConfig |
| 62 | +@EmbeddedKafka(topics = "partition.resolver.tests") |
| 63 | +public class PartitionResolverTests extends AbstractRetryTopicIntegrationTests { |
| 64 | + |
| 65 | + @Test |
| 66 | + void testNullPartition(@Autowired KafkaOperations<Integer, String> template, |
| 67 | + @Autowired EmbeddedKafkaBroker broker, @Autowired Config config) throws InterruptedException { |
| 68 | + |
| 69 | + Map<String, Object> producerProps = KafkaTestUtils.producerProps(broker); |
| 70 | + DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(producerProps); |
| 71 | + KafkaTemplate<Integer, String> kt = new KafkaTemplate<>(pf); |
| 72 | + kt.send("partition.resolver.tests", 1, null, "test"); |
| 73 | + assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 74 | + @SuppressWarnings("unchecked") |
| 75 | + ArgumentCaptor<ProducerRecord<Integer, String>> captor = ArgumentCaptor.forClass(ProducerRecord.class); |
| 76 | + verify(template).send(captor.capture()); |
| 77 | + assertThat(captor.getValue().partition()).isNull(); |
| 78 | + } |
| 79 | + |
| 80 | + @EnableKafka |
| 81 | + @Configuration |
| 82 | + public static class Config extends RetryTopicConfigurationSupport { |
| 83 | + |
| 84 | + final CountDownLatch latch = new CountDownLatch(1); |
| 85 | + |
| 86 | + @Override |
| 87 | + protected Consumer<DeadLetterPublishingRecovererFactory> configureDeadLetterPublishingContainerFactory() { |
| 88 | + return dlprf -> dlprf.setPartitionResolver((cr, nextTopic) -> null); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + RetryTopicConfiguration myRetryTopic(KafkaOperations<Integer, String> template) { |
| 93 | + return RetryTopicConfigurationBuilder |
| 94 | + .newInstance() |
| 95 | + .create(template); |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + @Bean |
| 100 | + KafkaOperations<Integer, String> template() { |
| 101 | + KafkaOperations<Integer, String> mock = mock(KafkaOperations.class); |
| 102 | + CompletableFuture<SendResult<Integer, String>> future = new CompletableFuture<>(); |
| 103 | + future.complete(mock(SendResult.class)); |
| 104 | + willAnswer(inv -> { |
| 105 | + latch.countDown(); |
| 106 | + return future; |
| 107 | + }).given(mock).send(any(ProducerRecord.class)); |
| 108 | + return mock; |
| 109 | + } |
| 110 | + |
| 111 | + @KafkaListener(topics = "partition.resolver.tests") |
| 112 | + void listen(String in) { |
| 113 | + throw new RuntimeException("test"); |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + ConsumerFactory<Integer, String> cf(EmbeddedKafkaBroker broker) { |
| 118 | + Map<String, Object> props = KafkaTestUtils.consumerProps("prt", "false", broker); |
| 119 | + return new DefaultKafkaConsumerFactory<>(props); |
| 120 | + } |
| 121 | + |
| 122 | + @Bean |
| 123 | + ConcurrentKafkaListenerContainerFactory<Integer, String> kafkaListenerContainerFactory( |
| 124 | + ConsumerFactory<Integer, String> cf) { |
| 125 | + ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); |
| 126 | + factory.setConsumerFactory(cf); |
| 127 | + return factory; |
| 128 | + } |
| 129 | + |
| 130 | + @Bean |
| 131 | + TaskScheduler taskScheduler() { |
| 132 | + return new ThreadPoolTaskScheduler(); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments