|
35 | 35 | import java.util.Collection;
|
36 | 36 | import java.util.Collections;
|
37 | 37 | import java.util.HashMap;
|
| 38 | +import java.util.HashSet; |
38 | 39 | import java.util.List;
|
39 | 40 | import java.util.Map;
|
40 | 41 | import java.util.Map.Entry;
|
|
78 | 79 | import org.springframework.kafka.listener.config.ContainerProperties;
|
79 | 80 | import org.springframework.kafka.support.Acknowledgment;
|
80 | 81 | import org.springframework.kafka.support.TopicPartitionInitialOffset;
|
| 82 | +import org.springframework.kafka.support.TopicPartitionInitialOffset.SeekPosition; |
81 | 83 | import org.springframework.kafka.support.serializer.JsonDeserializer;
|
82 | 84 | import org.springframework.kafka.support.serializer.JsonSerializer;
|
83 | 85 | import org.springframework.kafka.test.rule.KafkaEmbedded;
|
@@ -1675,6 +1677,47 @@ public void testPauseResume() throws Exception {
|
1675 | 1677 | container.stop();
|
1676 | 1678 | }
|
1677 | 1679 |
|
| 1680 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1681 | + @Test |
| 1682 | + public void testInitialSeek() throws Exception { |
| 1683 | + ConsumerFactory<Integer, String> cf = mock(ConsumerFactory.class); |
| 1684 | + Consumer<Integer, String> consumer = mock(Consumer.class); |
| 1685 | + given(cf.createConsumer(isNull(), eq("clientId"), isNull())).willReturn(consumer); |
| 1686 | + ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap()); |
| 1687 | + final CountDownLatch latch = new CountDownLatch(1); |
| 1688 | + given(consumer.poll(anyLong())).willAnswer(i -> { |
| 1689 | + latch.countDown(); |
| 1690 | + Thread.sleep(50); |
| 1691 | + return emptyRecords; |
| 1692 | + }); |
| 1693 | + TopicPartitionInitialOffset[] topicPartition = new TopicPartitionInitialOffset[] { |
| 1694 | + new TopicPartitionInitialOffset("foo", 0, SeekPosition.BEGINNING), |
| 1695 | + new TopicPartitionInitialOffset("foo", 1, SeekPosition.END), |
| 1696 | + new TopicPartitionInitialOffset("foo", 2, 0L), |
| 1697 | + new TopicPartitionInitialOffset("foo", 3, Long.MAX_VALUE), |
| 1698 | + new TopicPartitionInitialOffset("foo", 4, SeekPosition.BEGINNING), |
| 1699 | + new TopicPartitionInitialOffset("foo", 5, SeekPosition.END), |
| 1700 | + }; |
| 1701 | + ContainerProperties containerProps = new ContainerProperties(topicPartition); |
| 1702 | + containerProps.setAckMode(AckMode.RECORD); |
| 1703 | + containerProps.setClientId("clientId"); |
| 1704 | + containerProps.setMessageListener((MessageListener) r -> { }); |
| 1705 | + KafkaMessageListenerContainer<Integer, String> container = |
| 1706 | + new KafkaMessageListenerContainer<>(cf, containerProps); |
| 1707 | + container.start(); |
| 1708 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 1709 | + ArgumentCaptor<Collection<TopicPartition>> captor = ArgumentCaptor.forClass(List.class); |
| 1710 | + verify(consumer).seekToBeginning(captor.capture()); |
| 1711 | + assertThat(captor.getValue() |
| 1712 | + .equals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 0), new TopicPartition("foo", 4))))); |
| 1713 | + verify(consumer).seekToEnd(captor.capture()); |
| 1714 | + assertThat(captor.getValue() |
| 1715 | + .equals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 1), new TopicPartition("foo", 5))))); |
| 1716 | + verify(consumer).seek(new TopicPartition("foo", 2), 0L); |
| 1717 | + verify(consumer).seek(new TopicPartition("foo", 3), Long.MAX_VALUE); |
| 1718 | + container.stop(); |
| 1719 | + } |
| 1720 | + |
1678 | 1721 | private Consumer<?, ?> spyOnConsumer(KafkaMessageListenerContainer<Integer, String> container) {
|
1679 | 1722 | Consumer<?, ?> consumer = spy(
|
1680 | 1723 | KafkaTestUtils.getPropertyValue(container, "listenerConsumer.consumer", Consumer.class));
|
|
0 commit comments