Skip to content

Commit 9bf65e1

Browse files
committed
catch exceptions thrown from container error handler
1 parent f665e20 commit 9bf65e1

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

spring-kafka/src/test/java/org/springframework/kafka/listener/KafkaMessageListenerContainerTests.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collection;
3636
import java.util.Collections;
3737
import java.util.HashMap;
38+
import java.util.HashSet;
3839
import java.util.List;
3940
import java.util.Map;
4041
import java.util.Map.Entry;
@@ -78,6 +79,7 @@
7879
import org.springframework.kafka.listener.config.ContainerProperties;
7980
import org.springframework.kafka.support.Acknowledgment;
8081
import org.springframework.kafka.support.TopicPartitionInitialOffset;
82+
import org.springframework.kafka.support.TopicPartitionInitialOffset.SeekPosition;
8183
import org.springframework.kafka.support.serializer.JsonDeserializer;
8284
import org.springframework.kafka.support.serializer.JsonSerializer;
8385
import org.springframework.kafka.test.rule.KafkaEmbedded;
@@ -1677,6 +1679,47 @@ public void testPauseResume() throws Exception {
16771679
container.stop();
16781680
}
16791681

1682+
@SuppressWarnings({ "unchecked", "rawtypes" })
1683+
@Test
1684+
public void testInitialSeek() throws Exception {
1685+
ConsumerFactory<Integer, String> cf = mock(ConsumerFactory.class);
1686+
Consumer<Integer, String> consumer = mock(Consumer.class);
1687+
given(cf.createConsumer(isNull(), eq("clientId"), isNull())).willReturn(consumer);
1688+
ConsumerRecords<Integer, String> emptyRecords = new ConsumerRecords<>(Collections.emptyMap());
1689+
final CountDownLatch latch = new CountDownLatch(1);
1690+
given(consumer.poll(anyLong())).willAnswer(i -> {
1691+
latch.countDown();
1692+
Thread.sleep(50);
1693+
return emptyRecords;
1694+
});
1695+
TopicPartitionInitialOffset[] topicPartition = new TopicPartitionInitialOffset[] {
1696+
new TopicPartitionInitialOffset("foo", 0, SeekPosition.BEGINNING),
1697+
new TopicPartitionInitialOffset("foo", 1, SeekPosition.END),
1698+
new TopicPartitionInitialOffset("foo", 2, 0L),
1699+
new TopicPartitionInitialOffset("foo", 3, Long.MAX_VALUE),
1700+
new TopicPartitionInitialOffset("foo", 4, SeekPosition.BEGINNING),
1701+
new TopicPartitionInitialOffset("foo", 5, SeekPosition.END),
1702+
};
1703+
ContainerProperties containerProps = new ContainerProperties(topicPartition);
1704+
containerProps.setAckMode(AckMode.RECORD);
1705+
containerProps.setClientId("clientId");
1706+
containerProps.setMessageListener((MessageListener) r -> { });
1707+
KafkaMessageListenerContainer<Integer, String> container =
1708+
new KafkaMessageListenerContainer<>(cf, containerProps);
1709+
container.start();
1710+
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
1711+
ArgumentCaptor<Collection<TopicPartition>> captor = ArgumentCaptor.forClass(List.class);
1712+
verify(consumer).seekToBeginning(captor.capture());
1713+
assertThat(captor.getValue()
1714+
.equals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 0), new TopicPartition("foo", 4)))));
1715+
verify(consumer).seekToEnd(captor.capture());
1716+
assertThat(captor.getValue()
1717+
.equals(new HashSet<>(Arrays.asList(new TopicPartition("foo", 1), new TopicPartition("foo", 5)))));
1718+
verify(consumer).seek(new TopicPartition("foo", 2), 0L);
1719+
verify(consumer).seek(new TopicPartition("foo", 3), Long.MAX_VALUE);
1720+
container.stop();
1721+
}
1722+
16801723
@Test
16811724
public void testExceptionWhenCommitAfterRebalance() throws Exception {
16821725
final CountDownLatch rebalanceLatch = new CountDownLatch(2);
@@ -1692,7 +1735,8 @@ public void testExceptionWhenCommitAfterRebalance() throws Exception {
16921735
consumeLatch.countDown();
16931736
try {
16941737
Thread.sleep(3000);
1695-
} catch (InterruptedException e) {
1738+
}
1739+
catch (InterruptedException e) {
16961740
e.printStackTrace();
16971741
}
16981742
});

0 commit comments

Comments
 (0)