|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.core; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.BDDMockito.willAnswer; |
| 22 | +import static org.mockito.Mockito.inOrder; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.concurrent.BlockingQueue; |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
| 28 | + |
| 29 | +import org.apache.kafka.clients.producer.Producer; |
| 30 | +import org.apache.kafka.common.KafkaException; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.mockito.InOrder; |
| 33 | + |
| 34 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 35 | +import org.springframework.kafka.transaction.KafkaTransactionManager; |
| 36 | +import org.springframework.transaction.CannotCreateTransactionException; |
| 37 | +import org.springframework.transaction.support.TransactionTemplate; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Gary Russell |
| 41 | + * @since 1.3.5 |
| 42 | + * |
| 43 | + */ |
| 44 | +public class DefaultKafkaProducerFactoryTests { |
| 45 | + |
| 46 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 47 | + @Test |
| 48 | + public void testProducerClosedAfterBadTransition() throws Exception { |
| 49 | + final Producer producer = mock(Producer.class); |
| 50 | + DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory(new HashMap<>()) { |
| 51 | + |
| 52 | + @Override |
| 53 | + protected Producer createTransactionalProducer() { |
| 54 | + producer.initTransactions(); |
| 55 | + BlockingQueue<Producer> cache = getCache(); |
| 56 | + Producer cached = cache.poll(); |
| 57 | + return cached == null ? new CloseSafeProducer(producer, cache) : cached; |
| 58 | + } |
| 59 | + |
| 60 | + }; |
| 61 | + pf.setTransactionIdPrefix("foo"); |
| 62 | + |
| 63 | + final AtomicInteger flag = new AtomicInteger(); |
| 64 | + willAnswer(i -> { |
| 65 | + if (flag.incrementAndGet() == 2) { |
| 66 | + throw new KafkaException("Invalid transition ..."); |
| 67 | + } |
| 68 | + return null; |
| 69 | + }).given(producer).beginTransaction(); |
| 70 | + |
| 71 | + final KafkaTemplate kafkaTemplate = new KafkaTemplate(pf); |
| 72 | + KafkaTransactionManager tm = new KafkaTransactionManager(pf); |
| 73 | + TransactionTemplate transactionTemplate = new TransactionTemplate(tm); |
| 74 | + transactionTemplate.execute(s -> { |
| 75 | + kafkaTemplate.send("foo", "bar"); |
| 76 | + return null; |
| 77 | + }); |
| 78 | + BlockingQueue cache = KafkaTestUtils.getPropertyValue(pf, "cache", BlockingQueue.class); |
| 79 | + assertThat(cache).hasSize(1); |
| 80 | + try { |
| 81 | + transactionTemplate.execute(s -> { |
| 82 | + return null; |
| 83 | + }); |
| 84 | + } |
| 85 | + catch (CannotCreateTransactionException e) { |
| 86 | + assertThat(e.getCause().getMessage()).contains("Invalid transition"); |
| 87 | + } |
| 88 | + assertThat(cache).hasSize(0); |
| 89 | + |
| 90 | + InOrder inOrder = inOrder(producer); |
| 91 | + inOrder.verify(producer).initTransactions(); |
| 92 | + inOrder.verify(producer).beginTransaction(); |
| 93 | + inOrder.verify(producer).send(any(), any()); |
| 94 | + inOrder.verify(producer).commitTransaction(); |
| 95 | + inOrder.verify(producer).beginTransaction(); |
| 96 | + inOrder.verify(producer).close(); |
| 97 | + inOrder.verifyNoMoreInteractions(); |
| 98 | + pf.destroy(); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments