|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2017 the original author or authors. |
| 2 | + * Copyright 2016-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -430,6 +430,50 @@ public void publishEvent(ApplicationEvent event) {
|
430 | 430 | container.stop();
|
431 | 431 | }
|
432 | 432 |
|
| 433 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 434 | + @Test |
| 435 | + public void testNonResponsiveConsumerEventNotIssuedWithActiveConsumer() throws Exception { |
| 436 | + ConsumerFactory<Integer, String> cf = mock(ConsumerFactory.class); |
| 437 | + Consumer<Integer, String> consumer = mock(Consumer.class); |
| 438 | + given(cf.createConsumer(anyString(), eq(""))).willReturn(consumer); |
| 439 | + ConsumerRecords records = new ConsumerRecords(Collections.emptyMap()); |
| 440 | + CountDownLatch latch = new CountDownLatch(20); |
| 441 | + given(consumer.poll(anyLong())).willAnswer(i -> { |
| 442 | + Thread.sleep(100); |
| 443 | + latch.countDown(); |
| 444 | + return records; |
| 445 | + }); |
| 446 | + TopicPartitionInitialOffset[] topicPartition = new TopicPartitionInitialOffset[] { |
| 447 | + new TopicPartitionInitialOffset("foo", 0) }; |
| 448 | + ContainerProperties containerProps = new ContainerProperties(topicPartition); |
| 449 | + containerProps.setNoPollThreshold(2.0f); |
| 450 | + containerProps.setPollTimeout(100); |
| 451 | + containerProps.setMonitorInterval(1); |
| 452 | + containerProps.setMessageListener(mock(MessageListener.class)); |
| 453 | + KafkaMessageListenerContainer<Integer, String> container = |
| 454 | + new KafkaMessageListenerContainer<>(cf, containerProps); |
| 455 | + final AtomicInteger eventCounter = new AtomicInteger(); |
| 456 | + container.setApplicationEventPublisher(new ApplicationEventPublisher() { |
| 457 | + |
| 458 | + @Override |
| 459 | + public void publishEvent(Object e) { |
| 460 | + if (e instanceof NonResponsiveConsumerEvent) { |
| 461 | + eventCounter.incrementAndGet(); |
| 462 | + } |
| 463 | + } |
| 464 | + |
| 465 | + @Override |
| 466 | + public void publishEvent(ApplicationEvent event) { |
| 467 | + publishEvent((Object) event); |
| 468 | + } |
| 469 | + |
| 470 | + }); |
| 471 | + container.start(); |
| 472 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 473 | + container.stop(); |
| 474 | + assertThat(eventCounter.get()).isEqualTo(0); |
| 475 | + } |
| 476 | + |
433 | 477 | @Test
|
434 | 478 | public void testBatchAck() throws Exception {
|
435 | 479 | logger.info("Start batch ack");
|
|
0 commit comments