|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.broker.transaction.buffer; |
20 | 20 |
|
| 21 | +import org.apache.bookkeeper.mledger.ManagedLedger; |
| 22 | +import org.apache.bookkeeper.mledger.ManagedLedgerException; |
21 | 23 | import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl; |
| 24 | +import org.apache.pulsar.broker.PulsarService; |
| 25 | +import org.apache.pulsar.broker.service.BrokerService; |
| 26 | +import org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic; |
22 | 27 | import org.apache.pulsar.broker.service.persistent.PersistentTopic; |
23 | 28 | import org.apache.pulsar.broker.transaction.TransactionTestBase; |
| 29 | +import org.apache.pulsar.broker.transaction.buffer.impl.TopicTransactionBuffer; |
| 30 | +import org.apache.pulsar.broker.transaction.buffer.impl.TopicTransactionBufferState; |
24 | 31 | import org.apache.pulsar.client.api.Producer; |
25 | 32 | import org.apache.pulsar.client.api.transaction.Transaction; |
26 | 33 | import org.apache.pulsar.common.naming.TopicName; |
|
30 | 37 | import org.apache.pulsar.transaction.coordinator.impl.MLTransactionMetadataStore; |
31 | 38 | import org.awaitility.Awaitility; |
32 | 39 | import org.powermock.reflect.Whitebox; |
| 40 | +import org.mockito.Mockito; |
| 41 | +import org.testng.Assert; |
33 | 42 | import org.testng.annotations.AfterMethod; |
34 | 43 | import org.testng.annotations.BeforeMethod; |
35 | 44 | import org.testng.annotations.Test; |
| 45 | +import java.util.Collections; |
36 | 46 | import java.util.Map; |
| 47 | +import java.util.UUID; |
| 48 | +import java.util.concurrent.CompletableFuture; |
37 | 49 | import java.util.concurrent.TimeUnit; |
| 50 | +import java.util.concurrent.atomic.AtomicReference; |
38 | 51 |
|
39 | 52 | public class TopicTransactionBufferTest extends TransactionTestBase { |
40 | 53 |
|
@@ -86,4 +99,45 @@ public void testTransactionBufferAppendMarkerWriteFailState() throws Exception { |
86 | 99 | Whitebox.setInternalState(persistentTopic.getManagedLedger(), "state", ManagedLedgerImpl.State.WriteFailed); |
87 | 100 | txn.commit().get(); |
88 | 101 | } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testCheckDeduplicationFailedWhenCreatePersistentTopic() throws Exception { |
| 105 | + String topic = "persistent://" + NAMESPACE1 + "/test_" + UUID.randomUUID(); |
| 106 | + PulsarService pulsar = pulsarServiceList.get(0); |
| 107 | + BrokerService brokerService0 = pulsar.getBrokerService(); |
| 108 | + BrokerService brokerService = Mockito.spy(brokerService0); |
| 109 | + AtomicReference<PersistentTopic> reference = new AtomicReference<>(); |
| 110 | + |
| 111 | + Mockito |
| 112 | + .doAnswer(inv -> { |
| 113 | + String topic1 = inv.getArgument(0); |
| 114 | + ManagedLedger ledger = inv.getArgument(1); |
| 115 | + BrokerService service = inv.getArgument(2); |
| 116 | + Class<?> topicKlass = inv.getArgument(3); |
| 117 | + if (topicKlass.equals(PersistentTopic.class)) { |
| 118 | + PersistentTopic pt = Mockito.spy(new PersistentTopic(topic1, ledger, service)); |
| 119 | + CompletableFuture<Void> f =CompletableFuture |
| 120 | + .failedFuture(new ManagedLedgerException("This is an exception")); |
| 121 | + Mockito.doReturn(f).when(pt).checkDeduplicationStatus(); |
| 122 | + reference.set(pt); |
| 123 | + return pt; |
| 124 | + } else { |
| 125 | + return new NonPersistentTopic(topic1, service); |
| 126 | + } |
| 127 | + }) |
| 128 | + .when(brokerService) |
| 129 | + .newTopic(Mockito.eq(topic), Mockito.any(), Mockito.eq(brokerService), |
| 130 | + Mockito.eq(PersistentTopic.class)); |
| 131 | + |
| 132 | + brokerService.createPersistentTopic0(topic, true, new CompletableFuture<>(), Collections.emptyMap()); |
| 133 | + |
| 134 | + Awaitility.waitAtMost(1, TimeUnit.MINUTES).until(() -> reference.get() != null); |
| 135 | + PersistentTopic persistentTopic = reference.get(); |
| 136 | + TransactionBuffer buffer = persistentTopic.getTransactionBuffer(); |
| 137 | + Assert.assertTrue(buffer instanceof TopicTransactionBuffer); |
| 138 | + TopicTransactionBuffer ttb = (TopicTransactionBuffer) buffer; |
| 139 | + TopicTransactionBufferState.State expectState = TopicTransactionBufferState.State.Close; |
| 140 | + Assert.assertEquals(ttb.getState(), expectState); |
| 141 | + } |
| 142 | + |
89 | 143 | } |
0 commit comments