59
59
* Tests for the listener container.
60
60
*
61
61
* @author Gary Russell
62
- *
62
+ * @author Martin Dam
63
63
*/
64
64
public class KafkaMessageListenerContainerTests {
65
65
@@ -73,8 +73,10 @@ public class KafkaMessageListenerContainerTests {
73
73
74
74
private static String topic4 = "testTopic4" ;
75
75
76
+ private static String topic5 = "testTopic5" ;
77
+
76
78
@ ClassRule
77
- public static KafkaEmbedded embeddedKafka = new KafkaEmbedded (1 , true , topic1 , topic2 , topic3 , topic4 );
79
+ public static KafkaEmbedded embeddedKafka = new KafkaEmbedded (1 , true , topic1 , topic2 , topic3 , topic4 , topic5 );
78
80
79
81
@ Rule
80
82
public TestName testName = new TestName ();
@@ -192,6 +194,54 @@ public void onMessage(ConsumerRecord<Integer, String> message, Acknowledgment ac
192
194
logger .info ("Stop " + this .testName .getMethodName () + ackMode );
193
195
}
194
196
197
+ @ Test
198
+ public void testSlowConsumerCommitsAreProcessed () throws Exception {
199
+ Map <String , Object > props = KafkaTestUtils .consumerProps ("slow" , "false" , embeddedKafka );
200
+ DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
201
+ ContainerProperties containerProps = new ContainerProperties (topic5 );
202
+ containerProps .setAckCount (1 );
203
+ containerProps .setPauseAfter (100 );
204
+ containerProps .setAckMode (AckMode .MANUAL );
205
+ KafkaMessageListenerContainer <Integer , String > container =
206
+ new KafkaMessageListenerContainer <>(cf , containerProps );
207
+ final CountDownLatch latch = new CountDownLatch (3 );
208
+ containerProps .setMessageListener ((AcknowledgingMessageListener <Integer , String >) (message , ack ) -> {
209
+ logger .info ("slow: " + message );
210
+ try {
211
+ Thread .sleep (1000 );
212
+ }
213
+ catch (InterruptedException e ) {
214
+ Thread .currentThread ().interrupt ();
215
+ }
216
+ ack .acknowledge ();
217
+ latch .countDown ();
218
+ });
219
+ container .setBeanName ("testSlow" );
220
+ container .start ();
221
+ ContainerTestUtils .waitForAssignment (container , embeddedKafka .getPartitionsPerTopic ());
222
+ Consumer <?, ?> consumer = spyOnConsumer (container );
223
+
224
+
225
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
226
+ ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
227
+ KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
228
+ template .setDefaultTopic (topic5 );
229
+ template .sendDefault (0 , "foo" );
230
+ template .sendDefault (2 , "bar" );
231
+ template .flush ();
232
+ Thread .sleep (300 );
233
+ template .sendDefault (0 , "fiz" );
234
+ template .sendDefault (2 , "buz" );
235
+ template .flush ();
236
+
237
+ // Verify that commitSync is called when paused
238
+ assertThat (latch .await (60 , TimeUnit .SECONDS )).isTrue ();
239
+ verify (consumer , atLeastOnce ()).pause (any (TopicPartition .class ), any (TopicPartition .class ));
240
+ verify (consumer , atLeastOnce ()).commitSync (any ());
241
+ verify (consumer , atLeastOnce ()).resume (any (TopicPartition .class ), any (TopicPartition .class ));
242
+ container .stop ();
243
+ }
244
+
195
245
@ Test
196
246
public void testSlowConsumerWithException () throws Exception {
197
247
logger .info ("Start " + this .testName .getMethodName ());
0 commit comments