|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.service.persistent; |
| 20 | + |
| 21 | +import com.carrotsearch.hppc.ObjectSet; |
| 22 | +import java.util.List; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 25 | +import org.apache.pulsar.broker.service.Dispatcher; |
| 26 | +import org.apache.pulsar.client.api.Consumer; |
| 27 | +import org.apache.pulsar.client.api.MessageId; |
| 28 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 29 | +import org.apache.pulsar.client.api.Schema; |
| 30 | +import org.apache.pulsar.client.api.SubscriptionType; |
| 31 | +import org.awaitility.reflect.WhiteboxImpl; |
| 32 | +import org.testng.annotations.AfterClass; |
| 33 | +import org.testng.annotations.BeforeClass; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +@Slf4j |
| 37 | +@Test(groups = "broker-api") |
| 38 | +public class PersistentDispatcherMultipleConsumersTest extends ProducerConsumerBase { |
| 39 | + |
| 40 | + @BeforeClass(alwaysRun = true) |
| 41 | + @Override |
| 42 | + protected void setup() throws Exception { |
| 43 | + super.internalSetup(); |
| 44 | + super.producerBaseSetup(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterClass(alwaysRun = true) |
| 48 | + @Override |
| 49 | + protected void cleanup() throws Exception { |
| 50 | + super.internalCleanup(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test(timeOut = 30 * 1000) |
| 54 | + public void testTopicDeleteIfConsumerSetMismatchConsumerList() throws Exception { |
| 55 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 56 | + final String subscription = "s1"; |
| 57 | + admin.topics().createNonPartitionedTopic(topicName); |
| 58 | + admin.topics().createSubscription(topicName, subscription, MessageId.earliest); |
| 59 | + |
| 60 | + Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName).subscriptionName(subscription) |
| 61 | + .subscriptionType(SubscriptionType.Shared).subscribe(); |
| 62 | + // Make an error that "consumerSet" is mismatch with "consumerList". |
| 63 | + Dispatcher dispatcher = pulsar.getBrokerService() |
| 64 | + .getTopic(topicName, false).join().get() |
| 65 | + .getSubscription(subscription).getDispatcher(); |
| 66 | + ObjectSet<org.apache.pulsar.broker.service.Consumer> consumerSet = |
| 67 | + WhiteboxImpl.getInternalState(dispatcher, "consumerSet"); |
| 68 | + List<org.apache.pulsar.broker.service.Consumer> consumerList = |
| 69 | + WhiteboxImpl.getInternalState(dispatcher, "consumerList"); |
| 70 | + |
| 71 | + org.apache.pulsar.broker.service.Consumer serviceConsumer = consumerList.get(0); |
| 72 | + consumerSet.add(serviceConsumer); |
| 73 | + consumerList.add(serviceConsumer); |
| 74 | + |
| 75 | + // Verify: the topic can be deleted successfully. |
| 76 | + consumer.close(); |
| 77 | + admin.topics().delete(topicName, false); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(timeOut = 30 * 1000) |
| 81 | + public void testTopicDeleteIfConsumerSetMismatchConsumerList2() throws Exception { |
| 82 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 83 | + final String subscription = "s1"; |
| 84 | + admin.topics().createNonPartitionedTopic(topicName); |
| 85 | + admin.topics().createSubscription(topicName, subscription, MessageId.earliest); |
| 86 | + |
| 87 | + Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName).subscriptionName(subscription) |
| 88 | + .subscriptionType(SubscriptionType.Shared).subscribe(); |
| 89 | + // Make an error that "consumerSet" is mismatch with "consumerList". |
| 90 | + Dispatcher dispatcher = pulsar.getBrokerService() |
| 91 | + .getTopic(topicName, false).join().get() |
| 92 | + .getSubscription(subscription).getDispatcher(); |
| 93 | + ObjectSet<org.apache.pulsar.broker.service.Consumer> consumerSet = |
| 94 | + WhiteboxImpl.getInternalState(dispatcher, "consumerSet"); |
| 95 | + consumerSet.clear(); |
| 96 | + |
| 97 | + // Verify: the topic can be deleted successfully. |
| 98 | + consumer.close(); |
| 99 | + admin.topics().delete(topicName, false); |
| 100 | + } |
| 101 | +} |
0 commit comments