|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.kafka.annotation.EnableKafka; |
| 30 | +import org.springframework.kafka.annotation.KafkaListener; |
| 31 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 32 | +import org.springframework.kafka.core.ConsumerFactory; |
| 33 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 34 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 35 | +import org.springframework.kafka.core.KafkaTemplate; |
| 36 | +import org.springframework.kafka.core.ProducerFactory; |
| 37 | +import org.springframework.kafka.listener.AbstractConsumerSeekAwareTests.Config.MultiGroupListener; |
| 38 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 39 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 40 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 41 | +import org.springframework.stereotype.Component; |
| 42 | +import org.springframework.test.annotation.DirtiesContext; |
| 43 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Borahm Lee |
| 47 | + * @since 3.3 |
| 48 | + */ |
| 49 | +@DirtiesContext |
| 50 | +@SpringJUnitConfig |
| 51 | +@EmbeddedKafka(topics = {AbstractConsumerSeekAwareTests.TOPIC}, partitions = 3) |
| 52 | +class AbstractConsumerSeekAwareTests { |
| 53 | + |
| 54 | + static final String TOPIC = "Seek"; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + Config config; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + KafkaTemplate<String, String> template; |
| 61 | + |
| 62 | + @Autowired |
| 63 | + MultiGroupListener multiGroupListener; |
| 64 | + |
| 65 | + @Test |
| 66 | + void seekForAllGroups() throws Exception { |
| 67 | + template.send(TOPIC, "test-data"); |
| 68 | + template.send(TOPIC, "test-data"); |
| 69 | + assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 70 | + assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 71 | + |
| 72 | + MultiGroupListener.latch1 = new CountDownLatch(2); |
| 73 | + MultiGroupListener.latch2 = new CountDownLatch(2); |
| 74 | + |
| 75 | + multiGroupListener.seekToBeginning(); |
| 76 | + assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 77 | + assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void seekForSpecificGroup() throws Exception { |
| 82 | + template.send(TOPIC, "test-data"); |
| 83 | + template.send(TOPIC, "test-data"); |
| 84 | + assertThat(MultiGroupListener.latch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 85 | + assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 86 | + |
| 87 | + MultiGroupListener.latch1 = new CountDownLatch(2); |
| 88 | + MultiGroupListener.latch2 = new CountDownLatch(2); |
| 89 | + |
| 90 | + multiGroupListener.seekToBeginningForGroup("group2"); |
| 91 | + assertThat(MultiGroupListener.latch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 92 | + assertThat(MultiGroupListener.latch1.await(100, TimeUnit.MICROSECONDS)).isFalse(); |
| 93 | + assertThat(MultiGroupListener.latch1.getCount()).isEqualTo(2); |
| 94 | + } |
| 95 | + |
| 96 | + @EnableKafka |
| 97 | + @Configuration |
| 98 | + static class Config { |
| 99 | + |
| 100 | + @Autowired |
| 101 | + EmbeddedKafkaBroker broker; |
| 102 | + |
| 103 | + @Bean |
| 104 | + ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory( |
| 105 | + ConsumerFactory<String, String> consumerFactory) { |
| 106 | + ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); |
| 107 | + factory.setConsumerFactory(consumerFactory); |
| 108 | + return factory; |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + ConsumerFactory<String, String> consumerFactory() { |
| 113 | + return new DefaultKafkaConsumerFactory<>(KafkaTestUtils.consumerProps("test-group", "false", this.broker)); |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + ProducerFactory<String, String> producerFactory() { |
| 118 | + return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(this.broker)); |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + KafkaTemplate<String, String> template(ProducerFactory<String, String> pf) { |
| 123 | + return new KafkaTemplate<>(pf); |
| 124 | + } |
| 125 | + |
| 126 | + @Component |
| 127 | + static class MultiGroupListener extends AbstractConsumerSeekAware { |
| 128 | + |
| 129 | + static CountDownLatch latch1 = new CountDownLatch(2); |
| 130 | + |
| 131 | + static CountDownLatch latch2 = new CountDownLatch(2); |
| 132 | + |
| 133 | + @KafkaListener(groupId = "group1", topics = TOPIC) |
| 134 | + void listenForGroup1(String in) { |
| 135 | + latch1.countDown(); |
| 136 | + } |
| 137 | + |
| 138 | + @KafkaListener(groupId = "group2", topics = TOPIC) |
| 139 | + void listenForGroup2(String in) { |
| 140 | + latch2.countDown(); |
| 141 | + } |
| 142 | + |
| 143 | + void seekToBeginningForGroup(String groupIdForSeek) { |
| 144 | + getCallbacksAndTopics().forEach((cb, topics) -> { |
| 145 | + if (groupIdForSeek.equals(cb.getGroupId())) { |
| 146 | + topics.forEach(tp -> cb.seekToBeginning(tp.topic(), tp.partition())); |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments