|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.broker.service.persistent; |
20 | 20 |
|
| 21 | +import static org.mockito.Mockito.doAnswer; |
21 | 22 | import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.spy; |
| 24 | +import static org.testng.Assert.assertEquals; |
| 25 | +import static org.testng.Assert.assertTrue; |
22 | 26 | import java.util.ArrayList; |
23 | 27 | import java.util.Arrays; |
24 | 28 | import java.util.Collections; |
25 | 29 | import java.util.LinkedList; |
26 | 30 | import java.util.List; |
27 | | - |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
28 | 32 | import lombok.extern.slf4j.Slf4j; |
29 | 33 | import org.apache.bookkeeper.mledger.Entry; |
30 | 34 | import org.apache.bookkeeper.mledger.impl.PositionImpl; |
|
34 | 38 | import org.apache.pulsar.broker.service.OneWayReplicatorTestBase; |
35 | 39 | import org.apache.pulsar.broker.service.persistent.PersistentReplicator.InFlightTask; |
36 | 40 | import org.apache.pulsar.client.api.MessageId; |
| 41 | +import org.mockito.invocation.InvocationOnMock; |
| 42 | +import org.mockito.stubbing.Answer; |
37 | 43 | import org.testng.Assert; |
38 | 44 | import org.testng.annotations.AfterClass; |
39 | 45 | import org.testng.annotations.BeforeClass; |
@@ -66,6 +72,40 @@ private void createTopics() throws Exception { |
66 | 72 | admin1.topics().createSubscription(topicName, subscriptionName, MessageId.earliest); |
67 | 73 | } |
68 | 74 |
|
| 75 | + @Test |
| 76 | + public void testReplicationTaskStoppedAfterTopicClosed() throws Exception { |
| 77 | + // Close a topic, which has enabled replication. |
| 78 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://" + replicatedNamespace + "/tp_"); |
| 79 | + admin1.topics().createNonPartitionedTopic(topicName); |
| 80 | + waitReplicatorStarted(topicName); |
| 81 | + PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topicName, false) |
| 82 | + .join().get(); |
| 83 | + PersistentReplicator replicator = (PersistentReplicator) topic.getReplicators().get(cluster2); |
| 84 | + admin1.topics().unload(topicName); |
| 85 | + |
| 86 | + // Inject a task into the "inFlightTasks" to calculate how many times the method "replicator.readMoreEntries" |
| 87 | + // has been called. |
| 88 | + AtomicInteger counter = new AtomicInteger(); |
| 89 | + InFlightTask injectedTask = new InFlightTask(PositionImpl.get(1, 1), 1, replicator.getReplicatorId()); |
| 90 | + injectedTask.setEntries(Collections.emptyList()); |
| 91 | + InFlightTask spyTask = spy(injectedTask); |
| 92 | + replicator.inFlightTasks.add(spyTask); |
| 93 | + doAnswer(new Answer() { |
| 94 | + @Override |
| 95 | + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { |
| 96 | + counter.incrementAndGet(); |
| 97 | + return invocationOnMock.callRealMethod(); |
| 98 | + } |
| 99 | + }).when(spyTask).getReadPos(); |
| 100 | + |
| 101 | + // Verify: there is no scheduled task to retry to read entries to replicate. |
| 102 | + // Call "readMoreEntries" to make the issue happen. |
| 103 | + replicator.readMoreEntries(); |
| 104 | + Thread.sleep(PersistentTopic.MESSAGE_RATE_BACKOFF_MS * 10); |
| 105 | + assertEquals(replicator.getState(), AbstractReplicator.State.Terminated); |
| 106 | + assertTrue(counter.get() <= 1); |
| 107 | + } |
| 108 | + |
69 | 109 | @Test |
70 | 110 | public void testCreateOrRecycleInFlightTaskIntoQueue() throws Exception { |
71 | 111 | log.info("Starting testCreateOrRecycleInFlightTaskIntoQueue"); |
|
0 commit comments