@@ -1967,6 +1967,54 @@ public void testAckModeCount() throws Exception {
1967
1967
container .stop ();
1968
1968
}
1969
1969
1970
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
1971
+ @ Test
1972
+ public void testCommitErrorHandlerCalled () throws Exception {
1973
+ ConsumerFactory <Integer , String > cf = mock (ConsumerFactory .class );
1974
+ Consumer <Integer , String > consumer = mock (Consumer .class );
1975
+ given (cf .createConsumer (eq ("grp" ), eq ("clientId" ), isNull ())).willReturn (consumer );
1976
+ final Map <TopicPartition , List <ConsumerRecord <Integer , String >>> records = new HashMap <>();
1977
+ records .put (new TopicPartition ("foo" , 0 ), Arrays .asList (
1978
+ new ConsumerRecord <>("foo" , 0 , 0L , 1 , "foo" ),
1979
+ new ConsumerRecord <>("foo" , 0 , 1L , 1 , "bar" )));
1980
+ ConsumerRecords <Integer , String > consumerRecords = new ConsumerRecords <>(records );
1981
+ ConsumerRecords <Integer , String > emptyRecords = new ConsumerRecords <>(Collections .emptyMap ());
1982
+ AtomicBoolean first = new AtomicBoolean (true );
1983
+ given (consumer .poll (anyLong ())).willAnswer (i -> {
1984
+ Thread .sleep (50 );
1985
+ return first .getAndSet (false ) ? consumerRecords : emptyRecords ;
1986
+ });
1987
+ willAnswer (i -> {
1988
+ throw new RuntimeException ("Commit failed" );
1989
+ }).given (consumer ).commitSync (any (Map .class ));
1990
+ TopicPartitionInitialOffset [] topicPartition = new TopicPartitionInitialOffset [] {
1991
+ new TopicPartitionInitialOffset ("foo" , 0 ) };
1992
+ ContainerProperties containerProps = new ContainerProperties (topicPartition );
1993
+ containerProps .setGroupId ("grp" );
1994
+ containerProps .setClientId ("clientId" );
1995
+ containerProps .setIdleEventInterval (100L );
1996
+ containerProps .setMessageListener ((MessageListener ) r -> { });
1997
+ KafkaMessageListenerContainer <Integer , String > container =
1998
+ new KafkaMessageListenerContainer <>(cf , containerProps );
1999
+ final CountDownLatch ehl = new CountDownLatch (1 );
2000
+ container .setErrorHandler ((r , t ) -> {
2001
+ ehl .countDown ();
2002
+ });
2003
+ container .start ();
2004
+ assertThat (ehl .await (10 , TimeUnit .SECONDS )).isTrue ();
2005
+ container .stop ();
2006
+ containerProps .setMessageListener ((BatchMessageListener ) r -> { });
2007
+ container = new KafkaMessageListenerContainer <>(cf , containerProps );
2008
+ final CountDownLatch behl = new CountDownLatch (1 );
2009
+ container .setBatchErrorHandler ((r , t ) -> {
2010
+ behl .countDown ();
2011
+ });
2012
+ first .set (true );
2013
+ container .start ();
2014
+ assertThat (behl .await (10 , TimeUnit .SECONDS )).isTrue ();
2015
+ container .stop ();
2016
+ }
2017
+
1970
2018
private Consumer <?, ?> spyOnConsumer (KafkaMessageListenerContainer <Integer , String > container ) {
1971
2019
Consumer <?, ?> consumer = spy (
1972
2020
KafkaTestUtils .getPropertyValue (container , "listenerConsumer.consumer" , Consumer .class ));
0 commit comments