|
72 | 72 | import org.springframework.kafka.transaction.KafkaAwareTransactionManager;
|
73 | 73 | import org.springframework.lang.Nullable;
|
74 | 74 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
75 |
| -import org.springframework.transaction.PlatformTransactionManager; |
76 | 75 | import org.springframework.transaction.support.TransactionSynchronizationManager;
|
77 | 76 |
|
78 | 77 | /**
|
79 | 78 | * @author Gary Russell
|
80 | 79 | * @author Wang Zhiyang
|
| 80 | + * @author Soby Chacko |
81 | 81 | *
|
82 | 82 | * @since 2.2.4
|
83 | 83 | *
|
@@ -662,103 +662,6 @@ public void failure(ConsumerRecords records, Exception exception, Consumer consu
|
662 | 662 | }
|
663 | 663 | }
|
664 | 664 |
|
665 |
| - @Test |
666 |
| - @SuppressWarnings({ "rawtypes", "unchecked" }) |
667 |
| - void testInterceptInTxNonKafkaTM() throws InterruptedException { |
668 |
| - ConsumerFactory consumerFactory = mock(ConsumerFactory.class); |
669 |
| - final Consumer consumer = mock(Consumer.class); |
670 |
| - TopicPartition tp0 = new TopicPartition("foo", 0); |
671 |
| - ConsumerRecord record1 = new ConsumerRecord("foo", 0, 0L, "bar", "baz"); |
672 |
| - ConsumerRecords records = new ConsumerRecords( |
673 |
| - Collections.singletonMap(tp0, Collections.singletonList(record1))); |
674 |
| - ConsumerRecords empty = new ConsumerRecords(Collections.emptyMap()); |
675 |
| - AtomicInteger firstOrSecondPoll = new AtomicInteger(); |
676 |
| - willAnswer(invocation -> { |
677 |
| - Thread.sleep(10); |
678 |
| - return firstOrSecondPoll.incrementAndGet() < 2 ? records : empty; |
679 |
| - }).given(consumer).poll(any()); |
680 |
| - List<TopicPartition> assignments = List.of(tp0); |
681 |
| - willAnswer(invocation -> { |
682 |
| - ((ConsumerRebalanceListener) invocation.getArgument(1)) |
683 |
| - .onPartitionsAssigned(assignments); |
684 |
| - return null; |
685 |
| - }).given(consumer).subscribe(any(Collection.class), any()); |
686 |
| - given(consumer.position(any())).willReturn(0L); |
687 |
| - given(consumerFactory.createConsumer("grp", "", "-0", KafkaTestUtils.defaultPropertyOverrides())) |
688 |
| - .willReturn(consumer); |
689 |
| - ContainerProperties containerProperties = new ContainerProperties("foo"); |
690 |
| - containerProperties.setGroupId("grp"); |
691 |
| - containerProperties.setMessageListener((MessageListener) rec -> { |
692 |
| - }); |
693 |
| - containerProperties.setMissingTopicsFatal(false); |
694 |
| - List<String> order = new ArrayList<>(); |
695 |
| - AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(2)); |
696 |
| - PlatformTransactionManager tm = mock(PlatformTransactionManager.class); |
697 |
| - willAnswer(inv -> { |
698 |
| - order.add("tx"); |
699 |
| - latch.get().countDown(); |
700 |
| - return null; |
701 |
| - }).given(tm).getTransaction(any()); |
702 |
| - containerProperties.setTransactionManager(tm); |
703 |
| - ConcurrentMessageListenerContainer container = new ConcurrentMessageListenerContainer(consumerFactory, |
704 |
| - containerProperties); |
705 |
| - AtomicReference<CountDownLatch> successCalled = new AtomicReference<>(new CountDownLatch(1)); |
706 |
| - container.setRecordInterceptor(new RecordInterceptor() { |
707 |
| - |
708 |
| - @Override |
709 |
| - @Nullable |
710 |
| - public ConsumerRecord intercept(ConsumerRecord rec, Consumer consumer) { |
711 |
| - order.add("interceptor"); |
712 |
| - latch.get().countDown(); |
713 |
| - return rec; |
714 |
| - } |
715 |
| - |
716 |
| - @Override |
717 |
| - public void success(ConsumerRecord record, Consumer consumer) { |
718 |
| - order.add("success"); |
719 |
| - successCalled.get().countDown(); |
720 |
| - } |
721 |
| - |
722 |
| - }); |
723 |
| - container.setBatchInterceptor(new BatchInterceptor() { |
724 |
| - |
725 |
| - @Override |
726 |
| - @Nullable |
727 |
| - public ConsumerRecords intercept(ConsumerRecords recs, Consumer consumer) { |
728 |
| - order.add("b.interceptor"); |
729 |
| - latch.get().countDown(); |
730 |
| - return new ConsumerRecords(Collections.singletonMap(tp0, Collections.singletonList(record1))); |
731 |
| - } |
732 |
| - |
733 |
| - @Override |
734 |
| - public void success(ConsumerRecords records, Consumer consumer) { |
735 |
| - order.add("b.success"); |
736 |
| - successCalled.get().countDown(); |
737 |
| - } |
738 |
| - |
739 |
| - }); |
740 |
| - container.setInterceptBeforeTx(false); |
741 |
| - container.start(); |
742 |
| - try { |
743 |
| - assertThat(latch.get().await(10, TimeUnit.SECONDS)).isTrue(); |
744 |
| - assertThat(successCalled.get().await(10, TimeUnit.SECONDS)).isTrue(); |
745 |
| - assertThat(order).containsExactly("tx", "interceptor", "success"); |
746 |
| - container.stop(); |
747 |
| - latch.set(new CountDownLatch(2)); |
748 |
| - successCalled.set(new CountDownLatch(1)); |
749 |
| - container.getContainerProperties().setMessageListener((BatchMessageListener) recs -> { |
750 |
| - }); |
751 |
| - firstOrSecondPoll.set(0); |
752 |
| - container.start(); |
753 |
| - assertThat(latch.get().await(10, TimeUnit.SECONDS)).isTrue(); |
754 |
| - assertThat(successCalled.get().await(10, TimeUnit.SECONDS)).isTrue(); |
755 |
| - assertThat(order).containsExactly("tx", "interceptor", "success", "tx", "b.interceptor", "b.success"); |
756 |
| - } |
757 |
| - finally { |
758 |
| - container.stop(); |
759 |
| - } |
760 |
| - } |
761 |
| - |
762 | 665 | @SuppressWarnings({ "rawtypes", "unchecked" })
|
763 | 666 | @Test
|
764 | 667 | void testNoCommitOnAssignmentWithEarliest() throws InterruptedException {
|
|
0 commit comments