|
80 | 80 | import org.apache.kafka.common.errors.TopicAuthorizationException;
|
81 | 81 | import org.apache.kafka.common.errors.WakeupException;
|
82 | 82 | import org.apache.kafka.common.serialization.IntegerDeserializer;
|
| 83 | +import org.assertj.core.api.InstanceOfAssertFactories; |
83 | 84 | import org.junit.jupiter.api.BeforeAll;
|
84 | 85 | import org.junit.jupiter.api.Test;
|
85 | 86 | import org.mockito.ArgumentCaptor;
|
@@ -2761,6 +2762,70 @@ public void dontResumePausedPartition() throws Exception {
|
2761 | 2762 | container.stop();
|
2762 | 2763 | }
|
2763 | 2764 |
|
| 2765 | + @SuppressWarnings({ "unchecked" }) |
| 2766 | + @Test |
| 2767 | + public void rePausePartitionAfterRebalance() throws Exception { |
| 2768 | + ConsumerFactory<Integer, String> cf = mock(ConsumerFactory.class); |
| 2769 | + Consumer<Integer, String> consumer = mock(Consumer.class); |
| 2770 | + given(cf.createConsumer(eq("grp"), eq("clientId"), isNull(), any())).willReturn(consumer); |
| 2771 | + AtomicBoolean first = new AtomicBoolean(true); |
| 2772 | + TopicPartition tp0 = new TopicPartition("foo", 0); |
| 2773 | + TopicPartition tp1 = new TopicPartition("foo", 1); |
| 2774 | + given(consumer.assignment()).willReturn(Set.of(tp0, tp1)); |
| 2775 | + final CountDownLatch pauseLatch1 = new CountDownLatch(1); |
| 2776 | + final CountDownLatch pauseLatch2 = new CountDownLatch(2); |
| 2777 | + Set<TopicPartition> pausedParts = ConcurrentHashMap.newKeySet(); |
| 2778 | + willAnswer(i -> { |
| 2779 | + pausedParts.clear(); |
| 2780 | + pausedParts.addAll(i.getArgument(0)); |
| 2781 | + pauseLatch1.countDown(); |
| 2782 | + pauseLatch2.countDown(); |
| 2783 | + return null; |
| 2784 | + }).given(consumer).pause(any()); |
| 2785 | + given(consumer.paused()).willReturn(pausedParts); |
| 2786 | + given(consumer.poll(any(Duration.class))).willAnswer(i -> { |
| 2787 | + Thread.sleep(50); |
| 2788 | + return ConsumerRecords.empty(); |
| 2789 | + }); |
| 2790 | + AtomicReference<ConsumerRebalanceListener> rebal = new AtomicReference<>(); |
| 2791 | + Collection<String> foos = new ArrayList<>(); |
| 2792 | + foos.add("foo"); |
| 2793 | + willAnswer(inv -> { |
| 2794 | + rebal.set(inv.getArgument(1)); |
| 2795 | + rebal.get().onPartitionsAssigned(Set.of(tp0, tp1)); |
| 2796 | + return null; |
| 2797 | + }).given(consumer).subscribe(eq(foos), any(ConsumerRebalanceListener.class)); |
| 2798 | + final CountDownLatch resumeLatch = new CountDownLatch(1); |
| 2799 | + ContainerProperties containerProps = new ContainerProperties("foo"); |
| 2800 | + containerProps.setGroupId("grp"); |
| 2801 | + containerProps.setAckMode(AckMode.RECORD); |
| 2802 | + containerProps.setClientId("clientId"); |
| 2803 | + containerProps.setIdleEventInterval(100L); |
| 2804 | + containerProps.setMessageListener((MessageListener) rec -> { }); |
| 2805 | + containerProps.setMissingTopicsFatal(false); |
| 2806 | + KafkaMessageListenerContainer<Integer, String> container = |
| 2807 | + new KafkaMessageListenerContainer<>(cf, containerProps); |
| 2808 | + container.start(); |
| 2809 | + InOrder inOrder = inOrder(consumer); |
| 2810 | + container.pausePartition(tp0); |
| 2811 | + container.pausePartition(tp1); |
| 2812 | + assertThat(pauseLatch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 2813 | + assertThat(pausedParts).hasSize(2) |
| 2814 | + .contains(tp0, tp1); |
| 2815 | + rebal.get().onPartitionsRevoked(Set.of(tp0, tp1)); |
| 2816 | + rebal.get().onPartitionsAssigned(Collections.singleton(tp0)); |
| 2817 | + assertThat(pauseLatch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 2818 | + assertThat(pausedParts).hasSize(1) |
| 2819 | + .contains(tp0); |
| 2820 | + assertThat(container).extracting("listenerConsumer") |
| 2821 | + .extracting("pausedPartitions") |
| 2822 | + .asInstanceOf(InstanceOfAssertFactories.collection(TopicPartition.class)) |
| 2823 | + .hasSize(1) |
| 2824 | + .containsExactlyInAnyOrder(tp0); |
| 2825 | + |
| 2826 | + container.stop(); |
| 2827 | + } |
| 2828 | + |
2764 | 2829 | @SuppressWarnings({ "unchecked", "rawtypes" })
|
2765 | 2830 | @Test
|
2766 | 2831 | public void testInitialSeek() throws Exception {
|
|
0 commit comments