|
| 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.client.impl; |
| 20 | + |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import lombok.extern.slf4j.Slf4j; |
| 24 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 25 | +import org.apache.pulsar.broker.service.BrokerService; |
| 26 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 27 | +import org.apache.pulsar.client.api.PulsarClientException; |
| 28 | +import org.apache.pulsar.common.naming.TopicName; |
| 29 | +import org.awaitility.Awaitility; |
| 30 | +import org.testng.Assert; |
| 31 | +import org.testng.annotations.AfterClass; |
| 32 | +import org.testng.annotations.BeforeClass; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
| 35 | +@Slf4j |
| 36 | +@Test(groups = "broker-api") |
| 37 | +public class ConsumerCloseTest extends ProducerConsumerBase { |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + @Override |
| 41 | + protected void setup() throws Exception { |
| 42 | + super.internalSetup(); |
| 43 | + super.producerBaseSetup(); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterClass(alwaysRun = true) |
| 47 | + @Override |
| 48 | + protected void cleanup() throws Exception { |
| 49 | + super.internalCleanup(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testInterruptedWhenCreateConsumer() throws InterruptedException { |
| 54 | + |
| 55 | + String tpName = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 56 | + String subName = "test-sub"; |
| 57 | + String mlCursorPath = BrokerService.MANAGED_LEDGER_PATH_ZNODE + "/" + TopicName.get(tpName).getPersistenceNamingEncoding() + "/" + subName; |
| 58 | + |
| 59 | + // Make create cursor delay 1s |
| 60 | + CountDownLatch topicLoadLatch = new CountDownLatch(1); |
| 61 | + for (int i = 0; i < 5; i++) { |
| 62 | + mockZooKeeper.delay(1000, (op, path) -> { |
| 63 | + if (mlCursorPath.equals(path)) { |
| 64 | + topicLoadLatch.countDown(); |
| 65 | + return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + Thread startConsumer = new Thread(() -> { |
| 72 | + try { |
| 73 | + pulsarClient.newConsumer() |
| 74 | + .topic(tpName) |
| 75 | + .subscriptionName(subName) |
| 76 | + .subscribe(); |
| 77 | + Assert.fail("Should have thrown an exception"); |
| 78 | + } catch (PulsarClientException e) { |
| 79 | + Assert.assertTrue(e.getCause() instanceof InterruptedException); |
| 80 | + } |
| 81 | + }); |
| 82 | + startConsumer.start(); |
| 83 | + topicLoadLatch.await(); |
| 84 | + startConsumer.interrupt(); |
| 85 | + |
| 86 | + PulsarClientImpl clientImpl = (PulsarClientImpl) pulsarClient; |
| 87 | + Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { |
| 88 | + Assert.assertEquals(clientImpl.consumersCount(), 0); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments