From 870d2ac309de208e973e0d1dd8e0ebbecfb489e0 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 14 Apr 2021 20:17:36 +0300 Subject: [PATCH 1/4] Cleanup PulsarClient instances --- .../ProxySaslAuthenticationTest.java | 3 +- .../pulsar/broker/admin/AdminApiTest.java | 5 + .../pulsar/broker/admin/AdminApiTest2.java | 2 +- .../broker/admin/v1/V1_AdminApiTest.java | 6 +- .../broker/admin/v1/V1_AdminApiTest2.java | 1 + .../AntiAffinityNamespaceGroupTest.java | 3 +- .../LeaderElectionServiceTest.java | 3 +- .../service/BacklogQuotaManagerTest.java | 27 ++--- .../service/BrokerBkEnsemblesTests.java | 7 +- .../service/BrokerBookieIsolationTest.java | 3 + .../broker/service/BrokerServiceTest.java | 1 + .../service/BrokerServiceThrottlingTest.java | 5 +- .../broker/service/MaxMessageSizeTest.java | 3 +- .../broker/service/PeerReplicatorTest.java | 5 +- .../service/PersistentTopicE2ETest.java | 13 ++- .../broker/service/PersistentTopicTest.java | 2 + .../service/ReplicatorGlobalNSTest.java | 8 +- .../pulsar/broker/service/ReplicatorTest.java | 12 ++- .../pulsar/broker/service/TopicOwnerTest.java | 10 +- .../schema/PartitionedTopicsSchemaTest.java | 4 +- .../broker/stats/PrometheusMetricsTest.java | 7 +- .../TopicTransactionBufferRecoverTest.java | 3 + .../transaction/TransactionProduceTest.java | 3 + .../transaction/TransactionTestBase.java | 6 ++ .../buffer/TransactionLowWaterMarkTest.java | 3 + .../buffer/TransactionStablePositionTest.java | 3 + .../TransactionMetaStoreTestBase.java | 3 + .../PendingAckInMemoryDeleteTest.java | 3 + .../api/ClientDeduplicationFailureTest.java | 3 + .../pulsar/client/api/ClientErrorsTest.java | 38 +++---- .../client/api/ConsumerBatchReceiveTest.java | 1 + .../client/api/KeySharedSubscriptionTest.java | 101 +++++++++--------- .../client/api/NonPersistentTopicTest.java | 9 +- .../client/api/ServiceUrlProviderTest.java | 5 +- .../api/SimpleProducerConsumerTest.java | 2 +- .../pulsar/client/api/SimpleSchemaTest.java | 2 + .../impl/BrokerClientIntegrationTest.java | 71 ++++++------ .../client/impl/ConsumerUnsubscribeTest.java | 3 +- .../impl/PulsarMultiHostClientTest.java | 5 +- .../client/impl/SequenceIdWithErrorTest.java | 2 + .../client/impl/TopicDoesNotExistsTest.java | 3 +- .../client/impl/TopicsConsumerImplTest.java | 1 + .../client/impl/TransactionEndToEndTest.java | 3 + .../worker/PulsarFunctionE2ESecurityTest.java | 3 + .../worker/PulsarFunctionLocalRunTest.java | 3 + .../worker/PulsarFunctionPublishTest.java | 3 + .../worker/PulsarWorkerAssignmentTest.java | 3 + .../pulsar/io/AbstractPulsarE2ETest.java | 21 ++-- .../pulsar/io/PulsarFunctionAdminTest.java | 3 + .../client/examples/TransactionExample.java | 2 + .../pulsar/client/impl/BuildersTest.java | 3 +- .../tutorial/ContinuousAsyncProducer.java | 2 + ...roxyAuthenticatedProducerConsumerTest.java | 2 + .../pulsar/proxy/server/ProxyParserTest.java | 8 +- .../pulsar/proxy/server/ProxyStatsTest.java | 5 +- .../pulsar/proxy/server/ProxyTlsTest.java | 7 +- .../server/ProxyWithAuthorizationNegTest.java | 2 + .../server/ProxyWithAuthorizationTest.java | 4 + .../AbstractWebSocketHandlerTest.java | 23 +++- .../pulsar/tests/integration/cli/CLITest.java | 20 ++-- .../cli/ClusterMetadataTearDownTest.java | 10 +- .../tests/integration/schema/SchemaTest.java | 13 +++ .../transaction/TransactionTest.java | 1 + .../SimpleProducerConsumerTest.java | 14 ++- .../SimpleProducerConsumerTest.java | 14 ++- .../SimpleProducerConsumerTest.java | 12 ++- 66 files changed, 365 insertions(+), 216 deletions(-) diff --git a/pulsar-broker-auth-sasl/src/test/java/org/apache/pulsar/broker/authentication/ProxySaslAuthenticationTest.java b/pulsar-broker-auth-sasl/src/test/java/org/apache/pulsar/broker/authentication/ProxySaslAuthenticationTest.java index 98bd6df7ce4c4..aabdcf81df387 100644 --- a/pulsar-broker-auth-sasl/src/test/java/org/apache/pulsar/broker/authentication/ProxySaslAuthenticationTest.java +++ b/pulsar-broker-auth-sasl/src/test/java/org/apache/pulsar/broker/authentication/ProxySaslAuthenticationTest.java @@ -34,6 +34,7 @@ import javax.security.auth.login.Configuration; +import lombok.Cleanup; import org.apache.commons.io.FileUtils; import org.apache.curator.shaded.com.google.common.collect.Maps; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -249,6 +250,7 @@ void testAuthentication() throws Exception { log.info("1 proxy service started {}", proxyService); // Step 3: Pass correct client params + @Cleanup PulsarClient proxyClient = createProxyClient(proxyServiceUrl, 1); log.info("2 create proxy client {}, {}", proxyServiceUrl, proxyClient); @@ -277,7 +279,6 @@ void testAuthentication() throws Exception { consumer.acknowledgeCumulative(msg); consumer.close(); - proxyClient.close(); proxyService.close(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java index 622c4dc9ed338..08dd007e60c0f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest.java @@ -786,6 +786,7 @@ public void persistentTopics(String topicName) throws Exception { Lists.newArrayList("persistent://prop-xyz/ns1/" + topicName)); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -891,6 +892,7 @@ public void partitionedTopics(String topicName) throws Exception { assertEquals(subscriptions.size(), 0); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -1542,6 +1544,7 @@ public void testDeleteSubscription() throws Exception { assertEquals(admin.topics().getSubscriptions(persistentTopicName), Lists.newArrayList(subName)); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -2090,6 +2093,7 @@ public void persistentTopicsInvalidCursorReset() throws Exception { assertEquals(admin.topics().getList("prop-xyz/ns1"), Lists.newArrayList(topicName)); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -2284,6 +2288,7 @@ public void testPersistentTopicExpireMessageOnPartitionTopic() throws Exception // create consumer and subscription URL pulsarUrl = new URL(pulsar.getWebServiceAddress()); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsarUrl.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); Consumer consumer = client.newConsumer().topic("persistent://prop-xyz/ns1/ds1") diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java index cca01e0a7ab21..12e21764e0855 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTest2.java @@ -1737,6 +1737,7 @@ public void testMaxSubPerTopicPriority() throws Exception { admin.namespaces().createNamespace(myNamespace, Sets.newHashSet("test")); final String topic = "persistent://" + myNamespace + "/testMaxSubPerTopic"; //Create a client that can fail quickly + @Cleanup PulsarClient client = PulsarClient.builder().operationTimeout(2,TimeUnit.SECONDS) .serviceUrl(brokerUrl.toString()).build(); //We can only create 2 consumers @@ -1776,7 +1777,6 @@ public void testMaxSubPerTopicPriority() throws Exception { for (Consumer c : consumerList) { c.close(); } - client.close(); } @Test diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest.java index 00e7e46bef355..c3a263909c37f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest.java @@ -710,6 +710,7 @@ public void persistentTopics(String topicName) throws Exception { Lists.newArrayList("persistent://prop-xyz/use/ns1/" + topicName)); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -796,6 +797,7 @@ public void partitionedTopics(String topicName) throws Exception { 0); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -1639,6 +1641,7 @@ public void persistentTopicsInvalidCursorReset() throws Exception { assertEquals(admin.topics().getList("prop-xyz/use/ns1"), Lists.newArrayList(topicName)); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -1776,6 +1779,7 @@ public void testPersistentTopicExpireMessageOnPartitionTopic() throws Exception admin.topics().createPartitionedTopic("persistent://prop-xyz/use/ns1/ds1", 4); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -1816,8 +1820,6 @@ public void testPersistentTopicExpireMessageOnPartitionTopic() throws Exception producer.close(); consumer.close(); - client.close(); - } /** diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest2.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest2.java index 6876b1cc188d1..2410843ba41d6 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest2.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/v1/V1_AdminApiTest2.java @@ -234,6 +234,7 @@ public void nonPersistentTopics() throws Exception { publishMessagesOnTopic("non-persistent://prop-xyz/use/ns1/" + topicName, 0, 0); // create consumer and subscription + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/AntiAffinityNamespaceGroupTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/AntiAffinityNamespaceGroupTest.java index 963ce6fa648a2..cbb8e4053ba66 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/AntiAffinityNamespaceGroupTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/AntiAffinityNamespaceGroupTest.java @@ -40,6 +40,7 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.bookkeeper.util.ZkUtils; import org.apache.pulsar.broker.PulsarService; import org.apache.pulsar.broker.ServiceConfiguration; @@ -518,6 +519,7 @@ public void testLoadSheddingWithAntiAffinityNamespace() throws Exception { admin1.namespaces().setNamespaceAntiAffinityGroup(ns, namespaceAntiAffinityGroup); } + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(pulsar1.getSafeWebServiceAddress()).build(); Producer producer = pulsarClient.newProducer().topic("persistent://" + namespace + "0/my-topic1") .create(); @@ -529,7 +531,6 @@ public void testLoadSheddingWithAntiAffinityNamespace() throws Exception { assertTrue(loadManager.shouldAntiAffinityNamespaceUnload(namespace + "0", bundle, primaryHost)); producer.close(); - pulsarClient.close(); } private boolean isLoadManagerUpdatedDomainCache(ModularLoadManagerImpl loadManager) throws Exception { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/LeaderElectionServiceTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/LeaderElectionServiceTest.java index 08c4703cced70..79db02076ceec 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/LeaderElectionServiceTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/LeaderElectionServiceTest.java @@ -22,6 +22,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import lombok.Cleanup; import lombok.extern.slf4j.Slf4j; import org.apache.pulsar.broker.PulsarServerException; import org.apache.pulsar.broker.PulsarService; @@ -80,6 +81,7 @@ public void anErrorShouldBeThrowBeforeLeaderElected() throws PulsarServerExcepti adminClient.clusters().createCluster(clusterName, new ClusterData("http://localhost:8080")); adminClient.tenants().createTenant(tenant, new TenantInfo(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet(clusterName))); adminClient.namespaces().createNamespace(tenant + "/" + namespace, 16); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl("pulsar://localhost:6650") .startingBackoffInterval(1, TimeUnit.MILLISECONDS) @@ -101,7 +103,6 @@ public void anErrorShouldBeThrowBeforeLeaderElected() throws PulsarServerExcepti .topic("persistent://" + tenant + "/" + namespace + "/1p") .create(); producer.getTopic(); - pulsar.close(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java index f14abdee8c5e0..1be4aea9d5443 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java @@ -25,6 +25,7 @@ import com.beust.jcommander.internal.Maps; import com.google.common.collect.Sets; +import lombok.Cleanup; import org.apache.pulsar.broker.PulsarService; import org.apache.pulsar.broker.ServiceConfiguration; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -274,6 +275,7 @@ public void testConsumerBacklogEviction() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction)); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); @@ -297,7 +299,6 @@ public void testConsumerBacklogEviction() throws Exception { TopicStats stats = admin.topics().getStats(topic1); assertTrue(stats.backlogSize < 10 * 1024, "Storage size is [" + stats.storageSize + "]"); - client.close(); } @Test @@ -306,6 +307,7 @@ public void testConsumerBacklogEvictionWithAck() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction)); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).build(); final String topic1 = "persistent://prop/ns-quota/topic11"; @@ -329,7 +331,6 @@ public void testConsumerBacklogEvictionWithAck() throws Exception { TopicStats stats = admin.topics().getStats(topic1); assertTrue(stats.backlogSize <= 10 * 1024, "Storage size is [" + stats.storageSize + "]"); - client.close(); } @Test @@ -347,8 +348,10 @@ public void testConcurrentAckAndEviction() throws Exception { final CyclicBarrier barrier = new CyclicBarrier(2); final CountDownLatch counter = new CountDownLatch(2); final AtomicBoolean gotException = new AtomicBoolean(false); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); Consumer consumer1 = client2.newConsumer().topic(topic1).subscriptionName(subName1).subscribe(); @@ -401,8 +404,6 @@ public void run() { TopicStats stats = admin.topics().getStats(topic1); assertTrue(stats.backlogSize <= 10 * 1024, "Storage size is [" + stats.storageSize + "]"); - client.close(); - client2.close(); } @Test @@ -420,11 +421,13 @@ public void testNoEviction() throws Exception { final CyclicBarrier barrier = new CyclicBarrier(2); final CountDownLatch counter = new CountDownLatch(2); final AtomicBoolean gotException = new AtomicBoolean(false); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final Consumer consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe(); final Consumer consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe(); + @Cleanup final PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); @@ -467,8 +470,6 @@ public void run() { ConsumerThread.start(); counter.await(); assertFalse(gotException.get()); - client.close(); - client2.close(); } @Test @@ -486,13 +487,16 @@ public void testEvictionMulti() throws Exception { final CyclicBarrier barrier = new CyclicBarrier(4); final CountDownLatch counter = new CountDownLatch(4); final AtomicBoolean gotException = new AtomicBoolean(false); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final Consumer consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe(); final Consumer consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe(); + @Cleanup final PulsarClient client3 = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); + @Cleanup final PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); @@ -575,9 +579,6 @@ public void run() { TopicStats stats = admin.topics().getStats(topic1); assertTrue(stats.backlogSize <= 15 * 1024, "Storage size is [" + stats.storageSize + "]"); - client.close(); - client2.close(); - client3.close(); } @Test @@ -586,6 +587,7 @@ public void testAheadProducerOnHold() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold)); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final String topic1 = "persistent://prop/quotahold/hold"; @@ -616,7 +618,6 @@ public void testAheadProducerOnHold() throws Exception { TopicStats stats = admin.topics().getStats(topic1); assertEquals(stats.publishers.size(), 0, "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]"); - client.close(); } @Test @@ -625,6 +626,7 @@ public void testAheadProducerOnHoldTimeout() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold)); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final String topic1 = "persistent://prop/quotahold/holdtimeout"; @@ -651,7 +653,6 @@ public void testAheadProducerOnHoldTimeout() throws Exception { } assertTrue(gotException, "timeout did not occur"); - client.close(); } @Test @@ -660,6 +661,7 @@ public void testProducerException() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception)); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final String topic1 = "persistent://prop/quotahold/except"; @@ -688,7 +690,6 @@ public void testProducerException() throws Exception { } assertTrue(gotException, "backlog exceeded exception did not occur"); - client.close(); } @Test @@ -697,6 +698,7 @@ public void testProducerExceptionAndThenUnblock() throws Exception { Maps.newHashMap()); admin.namespaces().setBacklogQuota("prop/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception)); + @Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).build(); final String topic1 = "persistent://prop/quotahold/exceptandunblock"; @@ -747,7 +749,6 @@ public void testProducerExceptionAndThenUnblock() throws Exception { sendException = e; } assertFalse(gotException, "unable to publish due to " + sendException); - client.close(); } private static final Logger LOG = LoggerFactory.getLogger(BacklogQuotaManagerTest.class); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBkEnsemblesTests.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBkEnsemblesTests.java index 50880389ddb0e..aa63b224a9d1e 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBkEnsemblesTests.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBkEnsemblesTests.java @@ -80,6 +80,7 @@ public BrokerBkEnsemblesTests(int numberOfBookies) { public void testCrashBrokerWithoutCursorLedgerLeak() throws Exception { ZooKeeper zk = bkEnsemble.getZkClient(); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -153,8 +154,6 @@ public void testCrashBrokerWithoutCursorLedgerLeak() throws Exception { producer.close(); consumer.close(); - client.close(); - } /** @@ -176,6 +175,7 @@ public void testSkipCorruptDataLedger() throws Exception { // Ensure intended state for autoSkipNonRecoverableData admin.brokers().updateDynamicConfiguration("autoSkipNonRecoverableData", "false"); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -274,11 +274,11 @@ public void testSkipCorruptDataLedger() throws Exception { producer.close(); consumer.close(); - client.close(); } @Test(timeOut = 20000) public void testTopicWithWildCardChar() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getWebServiceAddress()) .statsInterval(0, TimeUnit.SECONDS) @@ -303,7 +303,6 @@ public void testTopicWithWildCardChar() throws Exception { Assert.assertEquals(msg.getData(), content); consumer.close(); producer.close(); - client.close(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBookieIsolationTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBookieIsolationTest.java index b8ba03251049e..a984bac5bd406 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBookieIsolationTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerBookieIsolationTest.java @@ -34,6 +34,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.bookkeeper.bookie.Bookie; import org.apache.bookkeeper.client.BookKeeper; import static org.apache.bookkeeper.client.RackawareEnsemblePlacementPolicyImpl.REPP_DNS_RESOLVER_CLASS; @@ -193,6 +194,7 @@ public void testBookieIsolation() throws Exception { // Ok } + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(pulsarService.getBrokerServiceUrl()) .statsInterval(-1, TimeUnit.SECONDS).build(); @@ -334,6 +336,7 @@ public void testBookieIsolationWithSecondaryGroup() throws Exception { // Ok } + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(pulsarService.getBrokerServiceUrl()) .statsInterval(-1, TimeUnit.SECONDS).build(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java index f318b4241ddef..96272348eeb23 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java @@ -759,6 +759,7 @@ public void testTlsAuthUseTrustCert() throws Exception { public void testLookupThrottlingForClientByClient() throws Exception { final String topicName = "persistent://prop/ns-abc/newTopic"; + @Cleanup PulsarClient pulsarClient = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceThrottlingTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceThrottlingTest.java index 8c6ba360f76ca..37436d7853a61 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceThrottlingTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceThrottlingTest.java @@ -88,6 +88,7 @@ public void testLookupThrottlingForClientByBroker0Permit() throws Exception { final String topicName = "persistent://prop/ns-abc/newTopic"; + @Cleanup PulsarClient pulsarClient = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) @@ -130,6 +131,7 @@ public void testLookupThrottlingForClientByBroker0Permit() throws Exception { public void testLookupThrottlingForClientByBroker() throws Exception { final String topicName = "persistent://prop/ns-abc/newTopic"; + @Cleanup PulsarClient pulsarClient = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) @@ -171,7 +173,6 @@ public void testLookupThrottlingForClientByBroker() throws Exception { c.close(); } } - pulsarClient.close(); assertNotEquals(successfulConsumers.size(), totalConsumers); } @@ -193,6 +194,7 @@ public void testLookupThrottlingForClientByBroker() throws Exception { public void testLookupThrottlingForClientByBrokerInternalRetry() throws Exception { final String topicName = "persistent://prop/ns-abc/newTopic-" + UUID.randomUUID().toString(); + @Cleanup PulsarClient pulsarClient = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) @@ -233,7 +235,6 @@ public void testLookupThrottlingForClientByBrokerInternalRetry() throws Exceptio } assertEquals(totalConnectedConsumers, totalConsumers); - pulsarClient.close(); } private boolean areAllConsumersConnected(List> consumers) { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MaxMessageSizeTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MaxMessageSizeTest.java index 1e06ac4969d56..c91f5dafc25de 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MaxMessageSizeTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MaxMessageSizeTest.java @@ -23,6 +23,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.bookkeeper.conf.ServerConfiguration; import org.apache.pulsar.broker.PulsarService; import org.apache.pulsar.broker.ServiceConfiguration; @@ -96,6 +97,7 @@ void shutdown() { @Test public void testMaxMessageSetting() throws PulsarClientException { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).build(); String topicName = "persistent://test/message/topic1"; Producer producer = client.newProducer().topic(topicName).sendTimeout(60, TimeUnit.SECONDS).create(); @@ -148,7 +150,6 @@ public void testMaxMessageSetting() throws PulsarClientException { consumer.unsubscribe(); consumer.close(); producer.close(); - client.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PeerReplicatorTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PeerReplicatorTest.java index 468ea7d6fbf42..d48f0b4cc9a93 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PeerReplicatorTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PeerReplicatorTest.java @@ -28,6 +28,7 @@ import java.util.LinkedHashSet; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.BrokerTestUtil; import org.apache.pulsar.broker.service.persistent.PersistentTopic; import org.apache.pulsar.client.admin.PulsarAdminException; @@ -109,6 +110,7 @@ public void testPeerClusterTopicLookup(String protocol) throws Exception { final String topic1 = "persistent://" + namespace1 + "/topic1"; final String topic2 = "persistent://" + namespace2 + "/topic2"; + @Cleanup PulsarClient client3 = PulsarClient.builder().serviceUrl(serviceUrl).statsInterval(0, TimeUnit.SECONDS) .operationTimeout(1000, TimeUnit.MILLISECONDS).build(); try { @@ -157,7 +159,6 @@ public void testPeerClusterTopicLookup(String protocol) throws Exception { assertEquals(stats.publishers.size(), 1); producer.close(); - client3.close(); } @@ -210,6 +211,7 @@ public void testPeerClusterInReplicationClusterListChange() throws Exception { final String topic1 = "persistent://" + namespace1 + "/topic1"; + @Cleanup PulsarClient client3 = PulsarClient.builder().serviceUrl(serviceUrl).statsInterval(0, TimeUnit.SECONDS).build(); // set peer-clusters : r3->r1 admin1.clusters().updatePeerClusterNames("r3", Sets.newLinkedHashSet(Lists.newArrayList("r1"))); @@ -244,7 +246,6 @@ public void testPeerClusterInReplicationClusterListChange() throws Exception { // topic should be unloaded from broker1 assertFalse(pulsar1.getBrokerService().getTopics().containsKey(topic1)); - client3.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java index 302b78d59a32b..7f684adb7a325 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java @@ -501,7 +501,9 @@ public void testMultipleClientsMultipleSubscriptions() throws Exception { final String topicName = "persistent://prop/ns-abc/topic7"; final String subName = "sub7"; + @Cleanup("shutdown") PulsarClient client1 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); + @Cleanup("shutdown") PulsarClient client2 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); try { @@ -514,9 +516,6 @@ public void testMultipleClientsMultipleSubscriptions() throws Exception { fail("Should have thrown an exception since one consumer is already connected"); } catch (PulsarClientException cce) { Assert.assertTrue(cce.getMessage().contains("Exclusive consumer is already connected")); - } finally { - client2.shutdown(); - client1.shutdown(); } } @@ -689,6 +688,7 @@ public void testGCWillDeleteSchema() throws Exception { @Test public void testDeleteSchema() throws Exception { + @Cleanup PulsarClientImpl httpProtocolClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); PulsarClientImpl binaryProtocolClient = (PulsarClientImpl) pulsarClient; LookupService binaryLookupService = binaryProtocolClient.getLookup(); @@ -1213,6 +1213,7 @@ public void testProducerQueueFullBlocking() throws Exception { final String topicName = "persistent://prop/ns-abc/topic-xyzx"; final int messages = 10; + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); // 1. Producer connect @@ -1250,7 +1251,6 @@ public void testProducerQueueFullBlocking() throws Exception { // 4. producer disconnect producer.close(); - client.close(); // 5. Restart broker setup(); @@ -1262,6 +1262,7 @@ public void testProducerQueueFullNonBlocking() throws Exception { final int messages = 10; // 1. Producer connect + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); ProducerImpl producer = (ProducerImpl) client.newProducer() .topic(topicName) @@ -1301,7 +1302,6 @@ public void testProducerQueueFullNonBlocking() throws Exception { // 4. producer disconnect producer.close(); - client.close(); // 5. Restart broker setup(); @@ -1480,6 +1480,9 @@ public void testBrokerConnectionStats() throws Exception { }); pulsar.getConfiguration().setAuthenticationEnabled(true); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()) .operationTimeout(1, TimeUnit.MILLISECONDS).build(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java index 0a98b94674bef..4865613fdddb8 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java @@ -1525,6 +1525,7 @@ public void testAtomicReplicationRemoval() throws Exception { final URL brokerUrl = new URL( "http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort().get()); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); ManagedCursor cursor = mock(ManagedCursorImpl.class); doReturn(remoteCluster).when(cursor).getName(); @@ -1569,6 +1570,7 @@ public void testClosingReplicationProducerTwice() throws Exception { final URL brokerUrl = new URL( "http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort().get()); + @Cleanup PulsarClient client = spy(PulsarClient.builder().serviceUrl(brokerUrl.toString()).build()); PulsarClientImpl clientImpl = (PulsarClientImpl) client; doReturn(new CompletableFuture()).when(clientImpl) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorGlobalNSTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorGlobalNSTest.java index cb8be77208a69..402b34a00206d 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorGlobalNSTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorGlobalNSTest.java @@ -19,6 +19,7 @@ package org.apache.pulsar.broker.service; import com.google.common.collect.Sets; +import lombok.Cleanup; import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; import org.apache.pulsar.client.api.MessageRoutingMode; import org.apache.pulsar.client.api.PulsarClient; @@ -73,8 +74,10 @@ public void testRemoveLocalClusterOnGlobalNamespace() throws Exception { final String topicName = "persistent://" + namespace + "/topic"; + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); @@ -95,8 +98,6 @@ public void testRemoveLocalClusterOnGlobalNamespace() throws Exception { Assert.assertFalse(consumer1.isConnected()); Assert.assertTrue(consumer2.isConnected()); - client1.close(); - client2.close(); } @Test @@ -109,6 +110,7 @@ public void testForcefullyTopicDeletion() throws Exception { final String topicName = "persistent://" + namespace + "/topic"; + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); @@ -122,8 +124,6 @@ public void testForcefullyTopicDeletion() throws Exception { .retryStrategically((test) -> !pulsar1.getBrokerService().getTopics().containsKey(topicName), 50, 150); Assert.assertFalse(pulsar1.getBrokerService().getTopics().containsKey(topicName)); - - client1.close(); } private static final Logger log = LoggerFactory.getLogger(ReplicatorGlobalNSTest.class); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorTest.java index 7f05524e4e01b..e1ed94669d913 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorTest.java @@ -769,12 +769,14 @@ public void testCloseReplicatorStartProducer() throws Exception { public void verifyChecksumAfterReplication() throws Exception { final String topicName = BrokerTestUtil.newUniqueName("persistent://pulsar/ns/checksumAfterReplication"); + @Cleanup PulsarClient c1 = PulsarClient.builder().serviceUrl(url1.toString()).build(); Producer p1 = c1.newProducer().topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); + @Cleanup PulsarClient c2 = PulsarClient.builder().serviceUrl(url2.toString()).build(); RawReader reader2 = RawReader.create(c2, topicName, "sub").get(); @@ -819,6 +821,7 @@ public void testReplicatorOnPartitionedTopic(boolean isPartitionedTopic) throws } // load namespace with dummy topic on ns + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(url1.toString()).build(); client.newProducer().topic("persistent://" + namespace + "/dummyTopic") .enableBatching(false) @@ -864,8 +867,10 @@ public void testReplicatedCluster() throws Exception { admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2", "r3")); admin1.topics().createPartitionedTopic(topicName, 4); + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); @@ -914,8 +919,10 @@ public void testUpdateGlobalTopicPartition() throws Exception { admin1.namespaces().createNamespace(namespace, Sets.newHashSet(cluster1, cluster2)); admin1.topics().createPartitionedTopic(topicName, startPartitions); + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); @@ -942,9 +949,6 @@ public void testUpdateGlobalTopicPartition() throws Exception { producer2.close(); consumer1.close(); consumer2.close(); - - client1.close(); - client2.close(); } @DataProvider(name = "topicPrefix") @@ -967,8 +971,10 @@ public void testTopicReplicatedAndProducerCreate(String topicPrefix, String topi admin1.topics().createPartitionedTopic(partitionedTopicName, startPartitions); admin1.topics().createNonPartitionedTopic(nonPartitionedTopicName); + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS) .build(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/TopicOwnerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/TopicOwnerTest.java index 24587c7c5b621..7e47559d18948 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/TopicOwnerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/TopicOwnerTest.java @@ -22,15 +22,14 @@ import static org.mockito.ArgumentMatchers.nullable; import static org.powermock.api.mockito.PowerMockito.doAnswer; import static org.powermock.api.mockito.PowerMockito.spy; - import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.google.common.collect.Sets; - import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.CompletableFuture; - +import lombok.Cleanup; import org.apache.commons.lang3.mutable.MutableBoolean; import org.apache.commons.lang3.mutable.MutableObject; import org.apache.jute.Record; @@ -70,8 +69,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Optional; - @Test(groups = "broker") public class TopicOwnerTest { @@ -518,6 +515,7 @@ public void testConnectToInvalidateBundleCacheBroker() throws Exception { pulsarServices[0].getNamespaceService().getBundle(TopicName.get(topic1)).getBundleRange(), true, null); + @Cleanup PulsarClient client = PulsarClient.builder(). serviceUrl(serviceUrlForTopic1) .build(); @@ -556,6 +554,7 @@ public void testListNonPersistentTopic() throws Exception { final String topicName = "non-persistent://my-tenant/my-ns/my-topic"; pulsarAdmins[0].topics().createPartitionedTopic(topicName, 16); + @Cleanup PulsarClient client = PulsarClient.builder(). serviceUrl(pulsarServices[0].getBrokerServiceUrl()) .build(); @@ -573,6 +572,5 @@ public void testListNonPersistentTopic() throws Exception { } consumer.close(); - client.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/PartitionedTopicsSchemaTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/PartitionedTopicsSchemaTest.java index 8bf16a3f8ef2f..11be95c783c44 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/PartitionedTopicsSchemaTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/PartitionedTopicsSchemaTest.java @@ -26,6 +26,7 @@ import java.util.TreeSet; import java.util.concurrent.CompletableFuture; +import lombok.Cleanup; import org.apache.pulsar.broker.service.BkEnsemblesTestBase; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.Message; @@ -51,6 +52,7 @@ public void partitionedTopicWithSchema() throws Exception { int N = 10; + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).build(); CompletableFuture> producerFuture = client.newProducer(Schema.STRING) @@ -103,8 +105,6 @@ public void partitionedTopicWithSchema() throws Exception { for (int i = 0; i < N; i++) { assertTrue(messages.contains("Hello-" + i)); } - - client.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/PrometheusMetricsTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/PrometheusMetricsTest.java index 48e6d0e995418..2e768b1565629 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/PrometheusMetricsTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/PrometheusMetricsTest.java @@ -33,10 +33,10 @@ import java.lang.reflect.Field; import java.math.RoundingMode; import java.nio.charset.StandardCharsets; -import java.util.Date; import java.text.NumberFormat; import java.util.Arrays; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -1058,8 +1058,9 @@ public void testBrokerConnection() throws Exception { compareBrokerConnectionStateCount(cm, 1.0); pulsar.getConfiguration().setAuthenticationEnabled(true); - pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()) - .operationTimeout(1, TimeUnit.MILLISECONDS).build(); + + replacePulsarClient(PulsarClient.builder().serviceUrl(lookupUrl.toString()) + .operationTimeout(1, TimeUnit.MILLISECONDS)); try { pulsarClient.newProducer() diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java index efcacb9fb8bc8..fabe1a814a365 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java @@ -98,6 +98,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionProduceTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionProduceTest.java index 1216b30d36aa8..e2836c6cb72ae 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionProduceTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionProduceTest.java @@ -100,6 +100,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionTestBase.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionTestBase.java index 8868c807ff931..8fc1fd127b78f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionTestBase.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TransactionTestBase.java @@ -83,8 +83,14 @@ public void internalSetup() throws Exception { incrementSetupNumber(); init(); + if (admin != null) { + admin.close(); + } admin = spy(PulsarAdmin.builder().serviceHttpUrl(pulsarServiceList.get(0).getWebServiceAddress()).build()); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder().serviceUrl(pulsarServiceList.get(0).getBrokerServiceUrl()).build(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionLowWaterMarkTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionLowWaterMarkTest.java index d5e68e0b6c9e3..7c3c6a6852e66 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionLowWaterMarkTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionLowWaterMarkTest.java @@ -96,6 +96,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionStablePositionTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionStablePositionTest.java index 0b145f7e53431..e7236fe308c00 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionStablePositionTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TransactionStablePositionTest.java @@ -73,6 +73,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/coordinator/TransactionMetaStoreTestBase.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/coordinator/TransactionMetaStoreTestBase.java index c4532a6e9d779..16cc4751d3542 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/coordinator/TransactionMetaStoreTestBase.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/coordinator/TransactionMetaStoreTestBase.java @@ -87,6 +87,9 @@ protected final void setup() throws Exception { afterPulsarStart(); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder(). serviceUrl(pulsarServices[0].getBrokerServiceUrl()) .build(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckInMemoryDeleteTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckInMemoryDeleteTest.java index 1df9e1a8de3bd..48f669e3a3f8a 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckInMemoryDeleteTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckInMemoryDeleteTest.java @@ -89,6 +89,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientDeduplicationFailureTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientDeduplicationFailureTest.java index b65f206b710a6..a3ec437f77127 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientDeduplicationFailureTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientDeduplicationFailureTest.java @@ -108,6 +108,9 @@ void setup(Method method) throws Exception { ClusterData clusterData = new ClusterData(url.toString()); admin.clusters().createCluster(config.getClusterName(), clusterData); + if (pulsarClient != null) { + pulsarClient.shutdown(); + } ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).maxBackoffInterval(1, TimeUnit.SECONDS); pulsarClient = clientBuilder.build(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientErrorsTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientErrorsTest.java index 07d9a1bb8821f..a38b71138f38d 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientErrorsTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ClientErrorsTest.java @@ -31,6 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import lombok.Cleanup; import org.apache.pulsar.client.impl.ConsumerBase; import org.apache.pulsar.client.impl.ProducerBase; import org.apache.pulsar.common.api.proto.CommandLookupTopicResponse.LookupType; @@ -63,11 +64,11 @@ public void teardown() { } @Test - public void testMockBrokerService() { + public void testMockBrokerService() throws PulsarClientException { // test default actions of mock broker service + @Cleanup + PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); try { - PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); - Consumer consumer = client.newConsumer().topic("persistent://prop/use/ns/t1") .subscriptionName("sub1").subscribe(); @@ -80,7 +81,6 @@ public void testMockBrokerService() { producer.close(); consumer.close(); - client.close(); } catch (Exception e) { fail("None of the mocked operations should throw a client side exception"); } @@ -97,6 +97,7 @@ public void testPartitionedProducerCreateFailWithoutRetry() throws Exception { } private void producerCreateFailWithoutRetry(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -119,7 +120,6 @@ private void producerCreateFailWithoutRetry(String topic) throws Exception { assertTrue(e instanceof PulsarClientException.AuthorizationException); } mockBrokerService.resetHandleProducer(); - client.close(); } @Test @@ -133,6 +133,7 @@ public void testPartitionedProducerCreateSuccessAfterRetry() throws Exception { } private void producerCreateSuccessAfterRetry(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -151,7 +152,6 @@ private void producerCreateSuccessAfterRetry(String topic) throws Exception { } mockBrokerService.resetHandleProducer(); - client.close(); } @Test @@ -165,6 +165,7 @@ public void testPartitionedProducerCreateFailAfterRetryTimeout() throws Exceptio } private void producerCreateFailAfterRetryTimeout(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()) .operationTimeout(1, TimeUnit.SECONDS).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -189,7 +190,6 @@ private void producerCreateFailAfterRetryTimeout(String topic) throws Exception } mockBrokerService.resetHandleProducer(); - client.close(); } @Test @@ -203,6 +203,7 @@ public void testPartitionedProducerFailDoesNotFailOtherProducer() throws Excepti } private void producerFailDoesNotFailOtherProducer(String topic1, String topic2) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -230,7 +231,6 @@ private void producerFailDoesNotFailOtherProducer(String topic1, String topic2) assertFalse(producer2 != null && producer2.isConnected()); mockBrokerService.resetHandleProducer(); - client.close(); } @Test @@ -244,6 +244,7 @@ public void testPartitionedProducerContinuousRetryAfterSendFail() throws Excepti } private void producerContinuousRetryAfterSendFail(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -276,7 +277,6 @@ private void producerContinuousRetryAfterSendFail(String topic) throws Exception mockBrokerService.resetHandleProducer(); mockBrokerService.resetHandleSend(); - client.close(); } @Test @@ -291,6 +291,7 @@ public void testPartitionedSubscribeFailWithoutRetry() throws Exception { @Test public void testLookupWithDisconnection() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); String topic = "persistent://prop/use/ns/t1"; @@ -320,11 +321,11 @@ public void testLookupWithDisconnection() throws Exception { } mockBrokerService.resetHandlePartitionLookup(); mockBrokerService.resetHandleLookup(); - client.close(); } private void subscribeFailWithoutRetry(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -347,7 +348,6 @@ private void subscribeFailWithoutRetry(String topic) throws Exception { assertTrue(e instanceof PulsarClientException.BrokerPersistenceException); } mockBrokerService.resetHandleSubscribe(); - client.close(); } @Test @@ -361,6 +361,7 @@ public void testPartitionedSubscribeSuccessAfterRetry() throws Exception { } private void subscribeSuccessAfterRetry(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -379,7 +380,6 @@ private void subscribeSuccessAfterRetry(String topic) throws Exception { } mockBrokerService.resetHandleSubscribe(); - client.close(); } @Test @@ -393,6 +393,7 @@ public void testPartitionedSubscribeFailAfterRetryTimeout() throws Exception { } private void subscribeFailAfterRetryTimeout(String topic) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()) .operationTimeout(200, TimeUnit.MILLISECONDS).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -417,7 +418,6 @@ private void subscribeFailAfterRetryTimeout(String topic) throws Exception { } mockBrokerService.resetHandleSubscribe(); - client.close(); } @Test @@ -431,6 +431,7 @@ public void testPartitionedSubscribeFailDoesNotFailOtherConsumer() throws Except } private void subscribeFailDoesNotFailOtherConsumer(String topic1, String topic2) throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); final AtomicInteger counter = new AtomicInteger(0); @@ -459,13 +460,13 @@ private void subscribeFailDoesNotFailOtherConsumer(String topic1, String topic2) assertFalse(consumer2 != null && consumer2.isConnected()); mockBrokerService.resetHandleSubscribe(); - client.close(); } // if a producer fails to connect while creating partitioned producer, it should close all successful connections of // other producers and fail @Test public void testOneProducerFailShouldCloseAllProducersInPartitionedProducer() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getHttpAddress()).build(); final AtomicInteger producerCounter = new AtomicInteger(0); final AtomicInteger closeCounter = new AtomicInteger(0); @@ -494,13 +495,13 @@ public void testOneProducerFailShouldCloseAllProducersInPartitionedProducer() th mockBrokerService.resetHandleProducer(); mockBrokerService.resetHandleCloseProducer(); - client.close(); } // if a consumer fails to subscribe while creating partitioned consumer, it should close all successful connections // of other consumers and fail @Test public void testOneConsumerFailShouldCloseAllConsumersInPartitionedConsumer() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getHttpAddress()).build(); final AtomicInteger subscribeCounter = new AtomicInteger(0); final AtomicInteger closeCounter = new AtomicInteger(0); @@ -530,11 +531,11 @@ public void testOneConsumerFailShouldCloseAllConsumersInPartitionedConsumer() th mockBrokerService.resetHandleSubscribe(); mockBrokerService.resetHandleCloseConsumer(); - client.close(); } @Test public void testFlowSendWhenPartitionedSubscribeCompletes() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getHttpAddress()).build(); AtomicInteger subscribed = new AtomicInteger(); @@ -560,7 +561,6 @@ public void testFlowSendWhenPartitionedSubscribeCompletes() throws Exception { mockBrokerService.resetHandleSubscribe(); mockBrokerService.resetHandleFlow(); - client.close(); } // Run this test multiple times to reproduce race conditions on reconnection logic @@ -587,6 +587,7 @@ public void testProducerReconnect() throws Exception { ctx.writeAndFlush(Commands.newSendReceipt(0, 0, 0, 1, 1)); }); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); Producer producer = client.newProducer().topic("persistent://prop/use/ns/t1").create(); @@ -601,7 +602,6 @@ public void testProducerReconnect() throws Exception { mockBrokerService.resetHandleConnect(); mockBrokerService.resetHandleProducer(); mockBrokerService.resetHandleSend(); - client.close(); } @Test @@ -625,6 +625,7 @@ public void testConsumerReconnect() throws Exception { ctx.writeAndFlush(Commands.newSuccess(subscribe.getRequestId())); }); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(mockBrokerService.getBrokerAddress()).build(); client.newConsumer().topic("persistent://prop/use/ns/t1").subscriptionName("sub1").subscribe(); @@ -636,6 +637,5 @@ public void testConsumerReconnect() throws Exception { mockBrokerService.resetHandleConnect(); mockBrokerService.resetHandleSubscribe(); - client.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerBatchReceiveTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerBatchReceiveTest.java index fcc208441de41..5522dd7e495a2 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerBatchReceiveTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerBatchReceiveTest.java @@ -445,6 +445,7 @@ private void testBatchReceiveAsync(String topic, return; } + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().ioThreads(10).serviceUrl(lookupUrl.toString()).build(); ProducerBuilder producerBuilder = pulsarClient.newProducer(Schema.STRING).topic(topic); if (!batchProduce) { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/KeySharedSubscriptionTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/KeySharedSubscriptionTest.java index 99e54954b24fe..d8e3c63bff8b6 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/KeySharedSubscriptionTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/KeySharedSubscriptionTest.java @@ -474,63 +474,64 @@ public void testMakingProgressWithSlowerConsumer(boolean enableBatch) throws Exc String slowKey = "slowKey"; List clients = new ArrayList<>(); - - AtomicInteger receivedMessages = new AtomicInteger(); - - for (int i = 0; i < 10; i++) { - PulsarClient client = PulsarClient.builder() - .serviceUrl(brokerUrl.toString()) - .build(); - clients.add(client); - - client.newConsumer(Schema.INT32) - .topic(topic) - .subscriptionName("key_shared") - .subscriptionType(SubscriptionType.Key_Shared) - .receiverQueueSize(1) - .messageListener((consumer, msg) -> { - try { - if (slowKey.equals(msg.getKey())) { - // Block the thread to simulate a slow consumer - Thread.sleep(10000); + try { + AtomicInteger receivedMessages = new AtomicInteger(); + + for (int i = 0; i < 10; i++) { + PulsarClient client = PulsarClient.builder() + .serviceUrl(brokerUrl.toString()) + .build(); + clients.add(client); + + client.newConsumer(Schema.INT32) + .topic(topic) + .subscriptionName("key_shared") + .subscriptionType(SubscriptionType.Key_Shared) + .receiverQueueSize(1) + .messageListener((consumer, msg) -> { + try { + if (slowKey.equals(msg.getKey())) { + // Block the thread to simulate a slow consumer + Thread.sleep(10000); + } + + receivedMessages.incrementAndGet(); + consumer.acknowledge(msg); + } catch (Exception e) { + e.printStackTrace(); } + }) + .subscribe(); + } - receivedMessages.incrementAndGet(); - consumer.acknowledge(msg); - } catch (Exception e) { - e.printStackTrace(); - } - }) - .subscribe(); - } - - @Cleanup - Producer producer = createProducer(topic, enableBatch); - - // First send the "slow key" so that 1 consumer will get stuck - producer.newMessage() - .key(slowKey) - .value(-1) - .send(); - - int N = 1000; + @Cleanup + Producer producer = createProducer(topic, enableBatch); - // Then send all the other keys - for (int i = 0; i < N; i++) { + // First send the "slow key" so that 1 consumer will get stuck producer.newMessage() - .key(String.valueOf(random.nextInt(NUMBER_OF_KEYS))) - .value(i) + .key(slowKey) + .value(-1) .send(); - } - // Since only 1 out of 10 consumers is stuck, we should be able to receive ~90% messages, - // plus or minus for some skew in the key distribution. - Thread.sleep(5000); + int N = 1000; + + // Then send all the other keys + for (int i = 0; i < N; i++) { + producer.newMessage() + .key(String.valueOf(random.nextInt(NUMBER_OF_KEYS))) + .value(i) + .send(); + } - assertEquals((double) receivedMessages.get(), N * 0.9, N * 0.3); + // Since only 1 out of 10 consumers is stuck, we should be able to receive ~90% messages, + // plus or minus for some skew in the key distribution. + Thread.sleep(5000); - for (PulsarClient c : clients) { - c.close(); + assertEquals((double) receivedMessages.get(), N * 0.9, N * 0.3); + } finally { + for (PulsarClient c : clients) { + c.close(); + } } } @@ -895,6 +896,7 @@ public void testOrderingWithConsumerListener(boolean partitioned) throws Excepti final int messages = 1000; List> received = Collections.synchronizedList(new ArrayList<>(1000)); Random random = new Random(); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(lookupUrl.toString()) .listenerThreads(8) @@ -942,7 +944,6 @@ public void received(Consumer consumer, Message msg) { producer.close(); consumer.close(); - client.close(); } @Test diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/NonPersistentTopicTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/NonPersistentTopicTest.java index 1a90f223bb7e5..74ca55664f477 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/NonPersistentTopicTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/NonPersistentTopicTest.java @@ -245,6 +245,7 @@ public void testPartitionedNonPersistentTopicWithTcpLookup(SubscriptionType type final String topic = "non-persistent://my-property/my-ns/partitioned-topic"; admin.topics().createPartitionedTopic(topic, numPartitions); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) @@ -289,7 +290,6 @@ public void testPartitionedNonPersistentTopicWithTcpLookup(SubscriptionType type producer.close(); consumer.close(); log.info("-- Exiting {} test --", methodName); - client.close(); } /** @@ -543,8 +543,11 @@ public void testReplicator() throws Exception { NonPersistentTopicStats stats; SubscriptionStats subStats; + @Cleanup PulsarClient client1 = PulsarClient.builder().serviceUrl(replication.url1.toString()).build(); + @Cleanup PulsarClient client2 = PulsarClient.builder().serviceUrl(replication.url2.toString()).build(); + @Cleanup PulsarClient client3 = PulsarClient.builder().serviceUrl(replication.url3.toString()).build(); ConsumerImpl consumer1 = (ConsumerImpl) client1.newConsumer().topic(globalTopicName) @@ -663,10 +666,6 @@ public void testReplicator() throws Exception { consumer1.close(); repl2Consumer.close(); repl3Consumer.close(); - client1.close(); - client2.close(); - client3.close(); - } finally { replication.shutdownReplicationCluster(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java index 76b38b5bc1b26..4493698abe1f0 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java @@ -21,6 +21,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import lombok.extern.slf4j.Slf4j; import org.apache.pulsar.broker.PulsarService; @@ -53,6 +54,7 @@ protected void cleanup() throws Exception { @Test public void testCreateClientWithServiceUrlProvider() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrlProvider(new TestServiceUrlProvider(pulsar.getSafeBrokerServiceUrl())) .statsInterval(1, TimeUnit.SECONDS) @@ -81,7 +83,6 @@ public void testCreateClientWithServiceUrlProvider() throws Exception { Assert.assertEquals(200, received); producer.close(); consumer.close(); - client.close(); } @Test @@ -89,6 +90,7 @@ public void testCreateClientWithAutoChangedServiceUrlProvider() throws Exception AutoChangedServiceUrlProvider serviceUrlProvider = new AutoChangedServiceUrlProvider(pulsar.getSafeBrokerServiceUrl()); + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrlProvider(serviceUrlProvider) .statsInterval(1, TimeUnit.SECONDS) @@ -131,7 +133,6 @@ public void testCreateClientWithAutoChangedServiceUrlProvider() throws Exception Assert.assertEquals(consumer.getClient().getLookup().getServiceUrl(), pulsarService2.getSafeBrokerServiceUrl()); producer.close(); consumer.close(); - client.close(); } static class TestServiceUrlProvider implements ServiceUrlProvider { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java index 061c4d4ba6db0..94362d4378957 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java @@ -3980,6 +3980,7 @@ public void testPartitionTopicsOnSeparateListner() throws Exception { final String topicName = "persistent://my-property/my-ns/one-partitioned-topic"; final String subscriptionName = "my-sub-"; + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().listenerThreads(10).serviceUrl(lookupUrl.toString()).build(); // create partitioned topic @@ -4024,7 +4025,6 @@ public void testPartitionTopicsOnSeparateListner() throws Exception { assertEquals(listenerThreads.size(), partitions - 1); // unblock the listener thread blockedMessageLatch.countDown(); - pulsarClient.close(); log.info("-- Exiting {} test --", methodName); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleSchemaTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleSchemaTest.java index 4d5c156994805..9690fe66d20f6 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleSchemaTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleSchemaTest.java @@ -20,6 +20,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; +import lombok.Cleanup; import lombok.Data; import lombok.NoArgsConstructor; @@ -629,6 +630,7 @@ public void testAutoKeyValueConsume(boolean batching) throws Exception { public void testGetSchemaByVersion() throws PulsarClientException, PulsarAdminException, ExecutionException, InterruptedException { final String topic = "persistent://my-property/my-ns/testGetSchemaByVersion"; + @Cleanup PulsarClientImpl httpProtocolClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(brokerUrl.toString()).build(); PulsarClientImpl binaryProtocolClient = (PulsarClientImpl) pulsarClient; diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java index 9b1fc4b693af6..e9384022a9bff 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java @@ -600,10 +600,6 @@ public void testCloseConnectionOnBrokerRejectedRequest() throws Exception { */ @Test public void testMaxConcurrentTopicLoading() throws Exception { - - final PulsarClientImpl pulsarClient; - final PulsarClientImpl pulsarClient2; - final String topicName = "persistent://prop/usw/my-ns/cocurrentLoadingTopic"; int concurrentTopic = pulsar.getConfiguration().getMaxConcurrentTopicLoadRequest(); final int concurrentLookupRequests = 20; @@ -617,39 +613,38 @@ public void testMaxConcurrentTopicLoading() throws Exception { pulsar.getConfiguration().setMaxConcurrentTopicLoadRequest(1); String lookupUrl = pulsar.getBrokerServiceUrl(); - pulsarClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(lookupUrl) + try (PulsarClientImpl pulsarClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(lookupUrl) .statsInterval(0, TimeUnit.SECONDS).maxNumberOfRejectedRequestPerConnection(0).build(); + PulsarClientImpl pulsarClient2 = (PulsarClientImpl) PulsarClient.builder().serviceUrl(lookupUrl) + .statsInterval(0, TimeUnit.SECONDS).ioThreads(concurrentLookupRequests) + .connectionsPerBroker(20).build()) { + + ProducerImpl producer = + (ProducerImpl) pulsarClient.newProducer().topic(topicName).create(); + ClientCnx cnx = producer.cnx(); + assertTrue(cnx.channel().isActive()); + + final List>> futures = Lists.newArrayList(); + final int totalProducers = 10; + CountDownLatch latch = new CountDownLatch(totalProducers); + for (int i = 0; i < totalProducers; i++) { + executor.submit(() -> { + final String randomTopicName1 = topicName + randomUUID().toString(); + final String randomTopicName2 = topicName + randomUUID().toString(); + // pass producer-name to avoid exception: producer is already connected to topic + synchronized (futures) { + futures.add(pulsarClient2.newProducer().topic(randomTopicName1).createAsync()); + futures.add(pulsarClient.newProducer().topic(randomTopicName2).createAsync()); + } + latch.countDown(); + }); + } - pulsarClient2 = (PulsarClientImpl) PulsarClient.builder().serviceUrl(lookupUrl) - .statsInterval(0, TimeUnit.SECONDS).ioThreads(concurrentLookupRequests).connectionsPerBroker(20) - .build(); - - ProducerImpl producer = (ProducerImpl) pulsarClient.newProducer().topic(topicName).create(); - ClientCnx cnx = producer.cnx(); - assertTrue(cnx.channel().isActive()); - - final List>> futures = Lists.newArrayList(); - final int totalProducers = 10; - CountDownLatch latch = new CountDownLatch(totalProducers); - for (int i = 0; i < totalProducers; i++) { - executor.submit(() -> { - final String randomTopicName1 = topicName + randomUUID().toString(); - final String randomTopicName2 = topicName + randomUUID().toString(); - // pass producer-name to avoid exception: producer is already connected to topic - synchronized (futures) { - futures.add(pulsarClient2.newProducer().topic(randomTopicName1).createAsync()); - futures.add(pulsarClient.newProducer().topic(randomTopicName2).createAsync()); - } - latch.countDown(); - }); - } - - latch.await(); - synchronized (futures) { - FutureUtil.waitForAll(futures).get(); + latch.await(); + synchronized (futures) { + FutureUtil.waitForAll(futures).get(); + } } - pulsarClient.close(); - pulsarClient2.close(); } finally { // revert back to original value pulsar.getConfiguration().setMaxConcurrentTopicLoadRequest(concurrentTopic); @@ -664,11 +659,10 @@ public void testMaxConcurrentTopicLoading() throws Exception { @Test public void testCloseConnectionOnInternalServerError() throws Exception { - final PulsarClient pulsarClient; - final String topicName = "persistent://prop/usw/my-ns/newTopic"; - pulsarClient = PulsarClient.builder() + @Cleanup + final PulsarClient pulsarClient = PulsarClient.builder() .serviceUrl(pulsar.getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) .operationTimeout(1000, TimeUnit.MILLISECONDS) @@ -690,7 +684,6 @@ public void testCloseConnectionOnInternalServerError() throws Exception { } // connection must be closed assertFalse(cnx.channel().isActive()); - pulsarClient.close(); } @Test @@ -749,6 +742,7 @@ public void testCleanProducer() throws Exception { admin.namespaces().createNamespace("my-property/global/lookup"); final int operationTimeOut = 500; + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()) .statsInterval(0, TimeUnit.SECONDS).operationTimeout(operationTimeOut, TimeUnit.MILLISECONDS).build(); CountDownLatch latch = new CountDownLatch(1); @@ -765,7 +759,6 @@ public void testCleanProducer() throws Exception { Set> producers = (Set>) prodField .get(pulsarClient); assertTrue(producers.isEmpty()); - pulsarClient.close(); log.info("-- Exiting {} test --", methodName); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConsumerUnsubscribeTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConsumerUnsubscribeTest.java index 75cd1df20fcb9..87d1546e0819f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConsumerUnsubscribeTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConsumerUnsubscribeTest.java @@ -20,6 +20,7 @@ import static org.testng.Assert.assertEquals; +import lombok.Cleanup; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.MockBrokerService; import org.apache.pulsar.client.api.PulsarClient; @@ -48,6 +49,7 @@ public void teardown() { @Test public void testConsumerUnsubscribeReference() throws Exception { + @Cleanup PulsarClientImpl client = (PulsarClientImpl) PulsarClient.builder() .serviceUrl(mockBrokerService.getBrokerAddress()) .build(); @@ -56,6 +58,5 @@ public void testConsumerUnsubscribeReference() throws Exception { consumer.unsubscribe(); assertEquals(client.consumersCount(), 0); - client.close(); } } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java index 04c73f48e949b..a1344871ee726 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java @@ -18,6 +18,7 @@ */ package org.apache.pulsar.client.impl; +import lombok.Cleanup; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.Message; import org.apache.pulsar.client.api.Producer; @@ -95,9 +96,9 @@ public void testGetPartitionedTopicDataTimeout() { url = "pulsar://localhost:51000,localhost:51001"; } - PulsarClient client; try { - client = PulsarClient.builder() + @Cleanup + PulsarClient client = PulsarClient.builder() .serviceUrl(url) .statsInterval(0, TimeUnit.SECONDS) .operationTimeout(3, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/SequenceIdWithErrorTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/SequenceIdWithErrorTest.java index a6c44b3c059a4..b41e1ff3755df 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/SequenceIdWithErrorTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/SequenceIdWithErrorTest.java @@ -20,6 +20,7 @@ import static org.testng.Assert.assertEquals; import java.util.Collections; +import lombok.Cleanup; import org.apache.bookkeeper.mledger.ManagedLedger; import org.apache.bookkeeper.mledger.ManagedLedgerFactory; import org.apache.pulsar.broker.ManagedLedgerClientFactory; @@ -45,6 +46,7 @@ public void testCheckSequenceId() throws Exception { String topicName = "prop/my-test/my-topic"; int N = 10; + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).build(); // Create consumer diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java index 6fe28124ec1b5..bca4a9fc5cdd2 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java @@ -19,6 +19,7 @@ package org.apache.pulsar.client.impl; import io.netty.util.HashedWheelTimer; +import lombok.Cleanup; import org.apache.pulsar.client.api.ProducerConsumerBase; import org.apache.pulsar.client.api.PulsarClient; import org.apache.pulsar.client.api.PulsarClientException; @@ -52,6 +53,7 @@ public void cleanup() throws Exception { @Test public void testCreateProducerOnNotExistsTopic() throws PulsarClientException, InterruptedException { + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl.toString()).build(); try { pulsarClient.newProducer() @@ -65,7 +67,6 @@ public void testCreateProducerOnNotExistsTopic() throws PulsarClientException, I HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer(); Assert.assertEquals(timer.pendingTimeouts(), 0); Assert.assertEquals(((PulsarClientImpl) pulsarClient).producersCount(), 0); - pulsarClient.close(); } @Test diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicsConsumerImplTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicsConsumerImplTest.java index 0c038d973a2ee..777ecc14f5773 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicsConsumerImplTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicsConsumerImplTest.java @@ -1141,6 +1141,7 @@ public void multiTopicsInDifferentNameSpace() throws PulsarAdminException, Pulsa @Test(timeOut = testTimeout) public void testSubscriptionMustCompleteWhenOperationTimeoutOnMultipleTopics() throws PulsarClientException { + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(lookupUrl.toString()) .ioThreads(2) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TransactionEndToEndTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TransactionEndToEndTest.java index a18869fa77053..7d0530b42bf66 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TransactionEndToEndTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TransactionEndToEndTest.java @@ -104,6 +104,9 @@ protected void setup() throws Exception { admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString()); admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(), 16); + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = PulsarClient.builder() .serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl()) .statsInterval(0, TimeUnit.SECONDS) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionE2ESecurityTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionE2ESecurityTest.java index fb3d1ca42fd39..7085832277db9 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionE2ESecurityTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionE2ESecurityTest.java @@ -180,6 +180,9 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) { clientBuilder.authentication(workerConfig.getBrokerClientAuthenticationPlugin(), workerConfig.getBrokerClientAuthenticationParameters()); } + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); TenantInfo propAdmin = new TenantInfo(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionLocalRunTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionLocalRunTest.java index 47f0504520f59..365b81f0c8b7f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionLocalRunTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionLocalRunTest.java @@ -260,6 +260,9 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) { workerConfig.getBrokerClientAuthenticationParameters()); clientBuilder.serviceUrl(pulsar.getBrokerServiceUrlTls()); } + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); TenantInfo propAdmin = new TenantInfo(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionPublishTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionPublishTest.java index d7597d8da4284..b6474b613b5e6 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionPublishTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarFunctionPublishTest.java @@ -186,6 +186,9 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) { clientBuilder.authentication(workerConfig.getBrokerClientAuthenticationPlugin(), workerConfig.getBrokerClientAuthenticationParameters()); } + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); TenantInfo propAdmin = new TenantInfo(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarWorkerAssignmentTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarWorkerAssignmentTest.java index 7076b3820383c..2c2e333657adb 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarWorkerAssignmentTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/functions/worker/PulsarWorkerAssignmentTest.java @@ -111,6 +111,9 @@ void setup(Method method) throws Exception { admin.clusters().updateCluster(config.getClusterName(), clusterData); final ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(this.workerConfig.getPulsarServiceUrl()); + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); final TenantInfo propAdmin = new TenantInfo(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/io/AbstractPulsarE2ETest.java b/pulsar-broker/src/test/java/org/apache/pulsar/io/AbstractPulsarE2ETest.java index 53ca40a6bbee6..2bee28bf06dfc 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/io/AbstractPulsarE2ETest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/io/AbstractPulsarE2ETest.java @@ -85,14 +85,14 @@ public abstract class AbstractPulsarE2ETest { public static final Logger log = LoggerFactory.getLogger(AbstractPulsarE2ETest.class); - + protected final String TLS_SERVER_CERT_FILE_PATH = "./src/test/resources/authentication/tls/broker-cert.pem"; protected final String TLS_SERVER_KEY_FILE_PATH = "./src/test/resources/authentication/tls/broker-key.pem"; protected final String TLS_CLIENT_CERT_FILE_PATH = "./src/test/resources/authentication/tls/client-cert.pem"; protected final String TLS_CLIENT_KEY_FILE_PATH = "./src/test/resources/authentication/tls/client-key.pem"; protected final String TLS_TRUST_CERT_FILE_PATH = "./src/test/resources/authentication/tls/cacert.pem"; protected final String tenant = "external-repl-prop"; - + protected LocalBookkeeperEnsemble bkEnsemble; protected ServiceConfiguration config; protected WorkerConfig workerConfig; @@ -106,12 +106,12 @@ public abstract class AbstractPulsarE2ETest { protected String workerId; protected PulsarFunctionTestTemporaryDirectory tempDirectory; protected FileServer fileServer; - + @DataProvider(name = "validRoleName") public Object[][] validRoleName() { return new Object[][] { { Boolean.TRUE }, { Boolean.FALSE } }; } - + @BeforeMethod(alwaysRun = true) public void setup(Method method) throws Exception { log.info("--- Setting up method {} ---", method.getName()); @@ -198,6 +198,9 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) { clientBuilder.authentication(workerConfig.getBrokerClientAuthenticationPlugin(), workerConfig.getBrokerClientAuthenticationParameters()); } + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); TenantInfo propAdmin = new TenantInfo(); @@ -229,23 +232,23 @@ void shutdown() throws Exception { if (fileServer != null) { fileServer.stop(); } - + if (pulsarClient != null) { pulsarClient.close(); } - + if (admin != null) { admin.close(); } - + if (functionsWorkerService != null) { functionsWorkerService.stop(); } - + if (pulsar != null) { pulsar.close(); } - + if (bkEnsemble != null) { bkEnsemble.stop(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/io/PulsarFunctionAdminTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/io/PulsarFunctionAdminTest.java index 74c31d85e5849..6d41c33f247e9 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/io/PulsarFunctionAdminTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/io/PulsarFunctionAdminTest.java @@ -149,6 +149,9 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) { clientBuilder.authentication(workerConfig.getBrokerClientAuthenticationPlugin(), workerConfig.getBrokerClientAuthenticationParameters()); } + if (pulsarClient != null) { + pulsarClient.close(); + } pulsarClient = clientBuilder.build(); TenantInfo propAdmin = new TenantInfo(); diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/examples/TransactionExample.java b/pulsar-client/src/test/java/org/apache/pulsar/client/examples/TransactionExample.java index db627de385f9e..faf8986910115 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/examples/TransactionExample.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/examples/TransactionExample.java @@ -21,6 +21,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.Message; import org.apache.pulsar.client.api.MessageId; @@ -40,6 +41,7 @@ public class TransactionExample { public static void main(String[] args) throws Exception { String serviceUrl = "pulsar://localhost:6650"; + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(serviceUrl) .build(); diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BuildersTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BuildersTest.java index 39c85440f5ad3..2ba6cba83db4e 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BuildersTest.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/BuildersTest.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import com.google.common.collect.ImmutableSet; +import lombok.Cleanup; import org.apache.pulsar.client.api.MessageId; import org.apache.pulsar.client.api.PulsarClient; import org.apache.pulsar.client.api.PulsarClientException; @@ -82,6 +83,7 @@ public void enableTlsTest() { @Test public void readerBuilderLoadConfTest() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build(); String topicName = "test_src"; MessageId messageId = new MessageIdImpl(1, 2, 3); @@ -99,7 +101,6 @@ public void readerBuilderLoadConfTest() throws Exception { assertTrue(obj instanceof ReaderConfigurationData); assertEquals(((ReaderConfigurationData) obj).getTopicName(), topicName); assertEquals(((ReaderConfigurationData) obj).getStartMessageId(), messageId); - client.close(); } @Test(expectedExceptions = {PulsarClientException.class}, expectedExceptionsMessageRegExp = ".* must be specified but they cannot be specified at the same time.*") diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/tutorial/ContinuousAsyncProducer.java b/pulsar-client/src/test/java/org/apache/pulsar/client/tutorial/ContinuousAsyncProducer.java index 7e4d272efbc41..a56474be975d2 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/tutorial/ContinuousAsyncProducer.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/tutorial/ContinuousAsyncProducer.java @@ -20,12 +20,14 @@ import java.io.IOException; +import lombok.Cleanup; import org.apache.pulsar.client.api.Producer; import org.apache.pulsar.client.api.PulsarClient; import org.apache.pulsar.client.api.PulsarClientException; public class ContinuousAsyncProducer { public static void main(String[] args) throws InterruptedException, IOException { + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl("http://127.0.0.1:8080").build(); Producer producer = pulsarClient.newProducer().topic("persistent://my-tenant/my-ns/my-topic") diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticatedProducerConsumerTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticatedProducerConsumerTest.java index ba4b1107e878a..447ab62840a92 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticatedProducerConsumerTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticatedProducerConsumerTest.java @@ -29,6 +29,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.authentication.AuthenticationProviderTls; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -163,6 +164,7 @@ public void testTlsSyncProducerAndConsumer() throws Exception { Authentication authTls = new AuthenticationTls(); authTls.configure(authParams); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient(authTls, proxyServiceUrl); admin.clusters().createCluster(configClusterName, new ClusterData(brokerUrl.toString(), brokerUrlTls.toString(), diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java index 33c1ed63475ef..a2e84fb77f7db 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java @@ -96,6 +96,7 @@ protected void cleanup() throws Exception { @Test public void testProducer() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()) .build(); Producer producer = client.newProducer(Schema.BYTES).topic("persistent://sample/test/local/producer-topic") @@ -104,12 +105,11 @@ public void testProducer() throws Exception { for (int i = 0; i < 10; i++) { producer.send("test".getBytes()); } - - client.close(); } @Test public void testProducerConsumer() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()) .build(); Producer producer = client.newProducer(Schema.BYTES) @@ -136,13 +136,13 @@ public void testProducerConsumer() throws Exception { checkArgument(msg == null); consumer.close(); - client.close(); } @Test public void testPartitions() throws Exception { TenantInfo tenantInfo = createDefaultTenantInfo(); admin.tenants().createTenant("sample", tenantInfo); + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()) .build(); admin.topics().createPartitionedTopic("persistent://sample/test/local/partitioned-topic", 2); @@ -164,8 +164,6 @@ public void testPartitions() throws Exception { Message msg = consumer.receive(1, TimeUnit.SECONDS); checkNotNull(msg); } - - client.close(); } @Test diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyStatsTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyStatsTest.java index 936f17055d779..2b1c22c22d012 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyStatsTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyStatsTest.java @@ -34,6 +34,7 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import lombok.Cleanup; import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.api.Consumer; @@ -104,6 +105,7 @@ protected void cleanup() throws Exception { @Test public void testConnectionsStats() throws Exception { final String topicName1 = "persistent://sample/test/local/connections-stats"; + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()).build(); Producer producer = client.newProducer(Schema.BYTES).topic(topicName1).enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition).create(); @@ -133,7 +135,6 @@ public void testConnectionsStats() throws Exception { assertNotNull(connectionStats); consumer.close(); - client.close(); } /** @@ -147,6 +148,7 @@ public void testTopicStats() throws Exception { final String topicName = "persistent://sample/test/local/topic-stats"; final String topicName2 = "persistent://sample/test/local/topic-stats-2"; + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()).build(); Producer producer1 = client.newProducer(Schema.BYTES).topic(topicName).enableBatching(false) .producerName("producer1").messageRoutingMode(MessageRoutingMode.SinglePartition).create(); @@ -184,7 +186,6 @@ public void testTopicStats() throws Exception { consumer.close(); consumer2.close(); - client.close(); } /** diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTest.java index e844661fb3111..686bb95c9e1f3 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyTlsTest.java @@ -24,6 +24,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.api.Consumer; @@ -82,6 +83,7 @@ protected void cleanup() throws Exception { @Test public void testProducer() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(proxyService.getServiceUrlTls()) .allowTlsInsecureConnection(false).tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).build(); @@ -90,12 +92,11 @@ public void testProducer() throws Exception { for (int i = 0; i < 10; i++) { producer.send("test".getBytes()); } - - client.close(); } @Test public void testPartitions() throws Exception { + @Cleanup PulsarClient client = PulsarClient.builder() .serviceUrl(proxyService.getServiceUrlTls()) .allowTlsInsecureConnection(false).tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).build(); @@ -118,8 +119,6 @@ public void testPartitions() throws Exception { Message msg = consumer.receive(1, TimeUnit.SECONDS); checkNotNull(msg); } - - client.close(); } } diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationNegTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationNegTest.java index e9e33aea1a965..2b7a1de9aeaa1 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationNegTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationNegTest.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.authentication.AuthenticationProviderTls; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -162,6 +163,7 @@ public void testProxyAuthorization() throws Exception { createAdminClient(); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient("pulsar+ssl://localhost:" + proxyService.getListenPortTls().get()); String namespaceName = "my-property/proxy-authorization-neg/my-ns"; diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java index 9e771bebb8c94..ef6f2e9f80de8 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.authentication.AuthenticationProviderTls; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -235,6 +236,7 @@ public void testProxyAuthorization() throws Exception { startProxy(); createAdminClient(); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient(proxyService.getServiceUrlTls(), PulsarClient.builder()); String namespaceName = "my-property/proxy-authorization/my-ns"; @@ -287,6 +289,7 @@ public void testTlsHostVerificationProxyToClient(boolean hostnameVerificationEna startProxy(); createAdminClient(); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient(proxyService.getServiceUrlTls(), PulsarClient.builder().enableTlsHostnameVerification(hostnameVerificationEnabled)); @@ -338,6 +341,7 @@ public void testTlsHostVerificationProxyToBroker(boolean hostnameVerificationEna startProxy(); createAdminClient(); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient(proxyService.getServiceUrlTls(), PulsarClient.builder().operationTimeout(1, TimeUnit.SECONDS)); diff --git a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/AbstractWebSocketHandlerTest.java b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/AbstractWebSocketHandlerTest.java index da5d3a6e8d0de..9bd9907677996 100644 --- a/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/AbstractWebSocketHandlerTest.java +++ b/pulsar-websocket/src/test/java/org/apache/pulsar/websocket/AbstractWebSocketHandlerTest.java @@ -26,7 +26,10 @@ import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -48,12 +51,28 @@ import org.apache.pulsar.common.naming.TopicName; import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; import org.mockito.Mock; +import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; public class AbstractWebSocketHandlerTest { + List createdClients = Collections.synchronizedList(new ArrayList<>()); + @Mock private HttpServletRequest httpServletRequest; + @AfterClass(alwaysRun = true) + final void cleanupCreatedClients() { + for (PulsarClient createdClient : createdClients) { + try { + createdClient.shutdown(); + } catch (PulsarClientException e) { + // ignore + } + } + createdClients.clear(); + } + @Test public void topicNameUrlEncodingTest() throws Exception { String producerV1 = "/ws/producer/persistent/my-property/my-cluster/my-ns/"; @@ -205,10 +224,12 @@ public void sendError(int statusCode, String message) { } PulsarClient newPulsarClient() throws PulsarClientException { - return PulsarClient.builder() + PulsarClient client = PulsarClient.builder() .serviceUrl("pulsar://localhost:6650") .operationTimeout(1, TimeUnit.SECONDS) .build(); + createdClients.add(client); + return client; } class MockedProducerHandler extends ProducerHandler { diff --git a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/CLITest.java b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/CLITest.java index 666e20353a522..c57b205290003 100644 --- a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/CLITest.java +++ b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/CLITest.java @@ -18,6 +18,12 @@ */ package org.apache.pulsar.tests.integration.cli; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; +import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -38,13 +44,6 @@ import org.testng.Assert; import org.testng.annotations.Test; -import java.util.concurrent.TimeUnit; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - /** * Test Pulsar CLI. */ @@ -271,12 +270,15 @@ public void testJarPojoSchemaUploadJson() throws Exception { private void testPublishAndConsume(String topic, String sub, Schema type) throws PulsarClientException { + @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(pulsarCluster.getPlainTextServiceUrl()).build(); + @Cleanup Producer producer = client.newProducer(type) .topic(topic + "-message") .create(); + @Cleanup Consumer consumer = client.newConsumer(type) .topic(topic + "-message") .subscriptionName(sub) @@ -293,10 +295,6 @@ private void testPublishAndConsume(String topic, String sub, Schema type) throws Message receive = consumer.receive(5, TimeUnit.SECONDS); Assert.assertEquals(receive.getValue(), expected); } - - producer.close(); - consumer.close(); - client.close(); } @Test diff --git a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/ClusterMetadataTearDownTest.java b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/ClusterMetadataTearDownTest.java index badc4482484dd..bb9f7b227f7ac 100644 --- a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/ClusterMetadataTearDownTest.java +++ b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/cli/ClusterMetadataTearDownTest.java @@ -46,6 +46,7 @@ import org.apache.pulsar.client.api.Message; import org.apache.pulsar.client.api.Producer; import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.client.api.PulsarClientException; import org.apache.pulsar.client.api.Schema; import org.apache.pulsar.client.api.SubscriptionInitialPosition; import org.apache.pulsar.common.policies.data.TenantInfo; @@ -102,8 +103,15 @@ public final void setup() throws Exception { @Override @AfterClass(alwaysRun = true) - public final void cleanup() { + public final void cleanup() throws PulsarClientException { markCurrentSetupNumberCleaned(); + if (client != null) { + client.close(); + } + if (admin != null) { + admin.close(); + } + try { ledgerManager.close(); } catch (IOException e) { diff --git a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/schema/SchemaTest.java b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/schema/SchemaTest.java index df8dbc460153f..a2f13cdbbc92c 100644 --- a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/schema/SchemaTest.java +++ b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/schema/SchemaTest.java @@ -64,6 +64,19 @@ public void setupCluster() throws Exception { .build(); } + @Override + public void tearDownCluster() throws Exception { + if (client != null) { + client.close(); + client = null; + } + if (admin != null) { + admin.close(); + admin = null; + } + super.tearDownCluster(); + } + @Test public void testCreateSchemaAfterDeletion() throws Exception { final String tenant = PUBLIC_TENANT; diff --git a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/transaction/TransactionTest.java b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/transaction/TransactionTest.java index 558d4580d3563..3cc05be8d99a6 100644 --- a/tests/integration/src/test/java/org/apache/pulsar/tests/integration/transaction/TransactionTest.java +++ b/tests/integration/src/test/java/org/apache/pulsar/tests/integration/transaction/TransactionTest.java @@ -75,6 +75,7 @@ public class TransactionTest extends TransactionTestBase { @Test(dataProvider = "ServiceUrls") public void transferNormalTest(Supplier serviceUrl) throws Exception { log.info("transfer normal test start."); + @Cleanup PulsarClient pulsarClient = PulsarClient.builder().enableTransaction(true).serviceUrl(serviceUrl.get()).build(); final int transferCount = 20; diff --git a/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index 2cd355e7c4cdc..dd1b7955a0592 100644 --- a/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -78,20 +78,26 @@ public void setup() throws Exception { .build(); lookupUrl = new URI(pulsarContainer.getPlainTextPulsarBrokerUrl()); + @Cleanup PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(pulsarContainer.getPulsarAdminUrl()).build(); admin.tenants().createTenant("my-property", new TenantInfo(new HashSet<>(Arrays.asList("appid1", "appid2")), Collections.singleton("standalone"))); admin.namespaces().createNamespace("my-property/my-ns"); admin.namespaces().setNamespaceReplicationClusters("my-property/my-ns", Collections.singleton("standalone")); - admin.close(); } @Override @AfterClass(alwaysRun = true) public void cleanup() throws Exception { - pulsarClient.close(); - pulsarContainer.stop(); - pulsarContainer.close(); + if (pulsarClient != null) { + pulsarClient.close(); + pulsarClient = null; + } + if (pulsarContainer != null) { + pulsarContainer.stop(); + pulsarContainer.close(); + pulsarContainer = null; + } } private PulsarClient newPulsarClient(String url, int intervalInSecs) throws PulsarClientException { diff --git a/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index 543e4750864fe..c2317df00c854 100644 --- a/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -73,20 +73,26 @@ public void setup() throws Exception { .build(); lookupUrl = new URI(pulsarContainer.getPlainTextPulsarBrokerUrl()); + @Cleanup PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(pulsarContainer.getPulsarAdminUrl()).build(); admin.tenants().createTenant("my-property", new TenantInfo(new HashSet<>(Arrays.asList("appid1", "appid2")), Collections.singleton("standalone"))); admin.namespaces().createNamespace("my-property/my-ns"); admin.namespaces().setNamespaceReplicationClusters("my-property/my-ns", Collections.singleton("standalone")); - admin.close(); } @Override @AfterClass(alwaysRun = true) public void cleanup() throws Exception { - pulsarClient.close(); - pulsarContainer.stop(); - pulsarContainer.close(); + if (pulsarClient != null) { + pulsarClient.close(); + pulsarClient = null; + } + if (pulsarContainer != null) { + pulsarContainer.stop(); + pulsarContainer.close(); + pulsarContainer = null; + } } private PulsarClient newPulsarClient(String url, int intervalInSecs) throws PulsarClientException { diff --git a/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index a96db7adfc9ac..d998aac0dbc8e 100644 --- a/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -88,9 +88,15 @@ public void setup() throws Exception { @Override @AfterClass(alwaysRun = true) public void cleanup() throws Exception { - pulsarClient.close(); - pulsarContainer.stop(); - pulsarContainer.close(); + if (pulsarClient != null) { + pulsarClient.close(); + pulsarClient = null; + } + if (pulsarContainer != null) { + pulsarContainer.stop(); + pulsarContainer.close(); + pulsarContainer = null; + } } private PulsarClient newPulsarClient(String url, int intervalInSecs) throws PulsarClientException { From 421d87a09fb4306b588768b1fd9bab992d313e13 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 14 Apr 2021 08:06:06 +0300 Subject: [PATCH 2/4] Fix flaky PulsarMultiHostClientTest --- .../impl/PulsarMultiHostClientTest.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java index a1344871ee726..656b8acbe2c0b 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarMultiHostClientTest.java @@ -18,6 +18,14 @@ */ package org.apache.pulsar.client.impl; +import static org.testng.Assert.fail; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.ServerSocket; +import java.net.URI; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.TimeUnit; import lombok.Cleanup; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.Message; @@ -31,13 +39,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.net.URI; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import static org.testng.Assert.fail; - @Test(groups = "broker-impl") public class PulsarMultiHostClientTest extends ProducerConsumerBase { @@ -68,6 +69,7 @@ public void testGetPartitionedTopicMetaData() { if (isTcpLookup) { url = pulsar.getBrokerServiceUrl(); } + @Cleanup PulsarClient client = newPulsarClient(url, 0); Consumer consumer = client.newConsumer().topic(topicName).subscriptionName(subscriptionName) @@ -76,7 +78,6 @@ public void testGetPartitionedTopicMetaData() { consumer.close(); producer.close(); - client.close(); } catch (PulsarClientException pce) { log.error("create producer or consumer error: ", pce); fail(); @@ -85,16 +86,13 @@ public void testGetPartitionedTopicMetaData() { log.info("-- Exiting {} test --", methodName); } - @Test (timeOut = 4000) + @Test (timeOut = 15000) public void testGetPartitionedTopicDataTimeout() { log.info("-- Starting {} test --", methodName); final String topicName = "persistent://my-property/my-ns/my-topic1"; - String url = "http://localhost:51000,localhost:51001"; - if (isTcpLookup) { - url = "pulsar://localhost:51000,localhost:51001"; - } + String url = "http://localhost:" + getFreePort() + ",localhost:" + getFreePort(); try { @Cleanup @@ -114,6 +112,14 @@ public void testGetPartitionedTopicDataTimeout() { log.info("-- Exiting {} test --", methodName); } + private static int getFreePort() { + try (ServerSocket serverSocket = new ServerSocket(0)) { + return serverSocket.getLocalPort(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + @Test public void testMultiHostUrlRetrySuccess() throws Exception { log.info("-- Starting {} test --", methodName); @@ -126,6 +132,7 @@ public void testMultiHostUrlRetrySuccess() throws Exception { if (isTcpLookup) { urlsWithUnreached = "pulsar://localhost:51000,localhost" + new URI(pulsar.getBrokerServiceUrl()).getPort(); } + @Cleanup PulsarClient client = newPulsarClient(urlsWithUnreached, 0); Consumer consumer = client.newConsumer().topic(topicName).subscriptionName(subscriptionName) @@ -153,7 +160,6 @@ public void testMultiHostUrlRetrySuccess() throws Exception { consumer.close(); producer.close(); - client.close(); log.info("-- Exiting {} test --", methodName); } From 822774797c71f2ca908c9d0049d8ca033e72fad8 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Wed, 14 Apr 2021 08:57:48 +0300 Subject: [PATCH 3/4] Add more PulsarClient cleanups --- .../service/PersistentTopicE2ETest.java | 6 +++++ .../TopicTransactionBufferRecoverTest.java | 4 +++ .../client/api/ConsumerCleanupTest.java | 2 ++ .../client/api/DeadLetterTopicTest.java | 8 +++--- .../api/DispatcherBlockConsumerTest.java | 4 +-- .../pulsar/client/api/RetryTopicTest.java | 7 ++--- .../api/SimpleProducerConsumerTest.java | 26 ++++++++++--------- .../api/v1/V1_ProducerConsumerTest.java | 18 ++++++------- .../impl/BrokerClientIntegrationTest.java | 2 +- .../client/impl/TopicDoesNotExistsTest.java | 1 + .../proxy/server/ProxyAuthenticationTest.java | 5 ++-- .../pulsar/proxy/server/ProxyParserTest.java | 3 +++ .../server/ProxyWithAuthorizationTest.java | 3 +-- .../ProxyWithoutServiceDiscoveryTest.java | 2 ++ .../SimpleProducerConsumerTest.java | 3 +++ .../SimpleProducerConsumerTest.java | 6 ++--- .../SimpleProducerConsumerTest.java | 6 ++--- 17 files changed, 65 insertions(+), 41 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java index 7f684adb7a325..a697f26c9f7e3 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java @@ -1061,6 +1061,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 1. shared consumer on an exclusive sub fails try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Shared).subscribe(); @@ -1071,6 +1072,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 2. failover consumer on an exclusive sub fails try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Failover).subscribe(); @@ -1082,6 +1084,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 3. disconnected sub can be converted in shared consumer1.close(); try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Shared).subscribe(); @@ -1092,6 +1095,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 4. exclusive fails on shared sub try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Exclusive).subscribe(); @@ -1103,6 +1107,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 5. disconnected sub can be converted in failover consumer2.close(); try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Failover).subscribe(); @@ -1114,6 +1119,7 @@ public void testSubscriptionTypeTransitions() throws Exception { // 5. exclusive consumer can connect after failover disconnects consumer3.close(); try { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Exclusive).subscribe(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java index fabe1a814a365..101fa2bc013c9 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/TopicTransactionBufferRecoverTest.java @@ -112,6 +112,10 @@ protected void setup() throws Exception { @AfterMethod(alwaysRun = true) protected void cleanup() throws Exception { + if (pulsarClient != null) { + pulsarClient.shutdown(); + pulsarClient = null; + } super.internalCleanup(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerCleanupTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerCleanupTest.java index 2c4c07b3b8d04..230c2ae8cd0e3 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerCleanupTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ConsumerCleanupTest.java @@ -20,6 +20,7 @@ package org.apache.pulsar.client.api; import io.netty.util.HashedWheelTimer; +import lombok.Cleanup; import org.apache.pulsar.client.impl.PulsarClientImpl; import org.testng.Assert; import org.testng.annotations.AfterClass; @@ -53,6 +54,7 @@ public Object[][] ackReceiptEnabled() { @Test(dataProvider = "ackReceiptEnabled") public void testAllTimerTaskShouldCanceledAfterConsumerClosed(boolean ackReceiptEnabled) throws PulsarClientException, InterruptedException { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 1); Consumer consumer = pulsarClient.newConsumer() .topic("persistent://public/default/" + UUID.randomUUID().toString()) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DeadLetterTopicTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DeadLetterTopicTest.java index c26fd4fce846b..faf9cf6421307 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DeadLetterTopicTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DeadLetterTopicTest.java @@ -77,6 +77,7 @@ public void testDeadLetterTopic() throws Exception { .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES) .topic("persistent://my-property/my-ns/dead-letter-topic-my-subscription-DLQ") @@ -126,7 +127,6 @@ public void testDeadLetterTopic() throws Exception { assertNull(checkMessage); checkConsumer.close(); - newPulsarClient.close(); } @Test(timeOut = 20000) @@ -145,6 +145,7 @@ public void testDeadLetterTopicHasOriginalInfo() throws Exception { .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES) .topic("persistent://my-property/my-ns/dead-letter-topic-my-subscription-DLQ") @@ -180,7 +181,6 @@ public void testDeadLetterTopicHasOriginalInfo() throws Exception { assertEquals(totalInDeadLetter, sendMessages); deadLetterConsumer.close(); consumer.close(); - newPulsarClient.close(); } @Test(timeOut = 30000) @@ -356,6 +356,7 @@ public void testDeadLetterTopicByCustomTopicName() throws Exception { .build()) .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES) .topic("persistent://my-property/my-ns/dead-letter-custom-topic-my-subscription-custom-DLQ") @@ -385,6 +386,7 @@ public void testDeadLetterTopicByCustomTopicName() throws Exception { } while (totalInDeadLetter < sendMessages); deadLetterConsumer.close(); consumer.close(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer checkConsumer = newPulsarClient1.newConsumer(Schema.BYTES) .topic(topic) @@ -397,8 +399,6 @@ public void testDeadLetterTopicByCustomTopicName() throws Exception { log.info("check consumer received message : {} {}", checkMessage.getMessageId(), new String(checkMessage.getData())); } assertNull(checkMessage); - newPulsarClient.close(); - newPulsarClient1.close(); checkConsumer.close(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DispatcherBlockConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DispatcherBlockConsumerTest.java index ed56901add0b7..9900c32227cd3 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DispatcherBlockConsumerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/DispatcherBlockConsumerTest.java @@ -759,6 +759,7 @@ public void testBlockBrokerDispatching() { } // client must receive number of messages = maxUnAckPerbroker rather all produced messages assertNotEquals(messages1.size(), totalProducedMsgs); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection // (1.b) consumer2 with same sub should not receive any more messages as subscription is blocked ConsumerImpl consumer2Sub1 = (ConsumerImpl) newPulsarClient.newConsumer().topic(topicName) @@ -857,7 +858,6 @@ public void testBlockBrokerDispatching() { consumer1Sub1.close(); consumerSub2.close(); consumer1Sub3.close(); - newPulsarClient.close(); log.info("-- Exiting {} test --", methodName); } catch (Exception e) { @@ -960,6 +960,7 @@ public void testBrokerDispatchBlockAndSubAckBackRequiredMsgs() { // client must receive number of messages = maxUnAckPerbroker rather all produced messages assertNotEquals(messages1.size(), totalProducedMsgs); // (1.b) consumer2 with same sub should not receive any more messages as subscription is blocked + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection ConsumerImpl consumer2Sub1 = (ConsumerImpl) newPulsarClient.newConsumer().topic(topicName) .subscriptionName(subscriberName1).receiverQueueSize(receiverQueueSize) @@ -1026,7 +1027,6 @@ public void testBrokerDispatchBlockAndSubAckBackRequiredMsgs() { consumer1Sub1.close(); consumer1Sub2.close(); - newPulsarClient.close(); log.info("-- Exiting {} test --", methodName); } catch (Exception e) { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/RetryTopicTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/RetryTopicTest.java index e9ba50967ccd1..3840efa2917a5 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/RetryTopicTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/RetryTopicTest.java @@ -18,6 +18,7 @@ */ package org.apache.pulsar.client.api; +import lombok.Cleanup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.annotations.AfterMethod; @@ -64,6 +65,7 @@ public void testRetryTopic() throws Exception { .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES) .topic("persistent://my-property/my-ns/my-subscription-DLQ") @@ -114,7 +116,6 @@ public void testRetryTopic() throws Exception { assertNull(checkMessage); checkConsumer.close(); - newPulsarClient.close(); } /** @@ -220,6 +221,7 @@ public void testRetryTopicByCustomTopicName() throws Exception { .build()) .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer deadLetterConsumer = newPulsarClient.newConsumer(Schema.BYTES) .topic("persistent://my-property/my-ns/my-subscription-DLQ") @@ -250,6 +252,7 @@ public void testRetryTopicByCustomTopicName() throws Exception { } while (totalInDeadLetter < sendMessages); deadLetterConsumer.close(); consumer.close(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer checkConsumer = newPulsarClient1.newConsumer(Schema.BYTES) .topic(topic) @@ -262,8 +265,6 @@ public void testRetryTopicByCustomTopicName() throws Exception { log.info("check consumer received message : {} {}", checkMessage.getMessageId(), new String(checkMessage.getData())); } assertNull(checkMessage); - newPulsarClient.close(); - newPulsarClient1.close(); checkConsumer.close(); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java index 94362d4378957..abcb7ede90756 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/SimpleProducerConsumerTest.java @@ -1534,6 +1534,7 @@ public void testMultipleSharedConsumerBlockingWithUnActedMessages(boolean ackRec .isAckReceiptEnabled(ackReceiptEnabled) .receiverQueueSize(receiverQueueSize).subscriptionType(SubscriptionType.Shared).subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient.newConsumer() .topic("persistent://my-property/my-ns/unacked-topic").subscriptionName("subscriber-1") @@ -1607,7 +1608,6 @@ public void testMultipleSharedConsumerBlockingWithUnActedMessages(boolean ackRec producer.close(); consumer1.close(); consumer2.close(); - newPulsarClient.close(); log.info("-- Exiting {} test --", methodName); } catch (Exception e) { fail(); @@ -2131,16 +2131,19 @@ public void testPriorityConsumer() throws Exception { .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(5).priorityLevel(1).subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(5).priorityLevel(1).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer3 = newPulsarClient1.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(5).priorityLevel(1).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer4 = newPulsarClient2.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") @@ -2186,9 +2189,6 @@ public void testPriorityConsumer() throws Exception { consumer2.close(); consumer3.close(); consumer4.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); log.info("-- Exiting {} test --", methodName); } @@ -2216,12 +2216,14 @@ public void testSharedSamePriorityConsumer() throws Exception { .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c1 = newPulsarClient.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(queueSize) .acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c2 = newPulsarClient1.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") @@ -2265,18 +2267,21 @@ public void testSharedSamePriorityConsumer() throws Exception { Assert.assertEquals(queueSize * 2, messages.size()); // create new consumers with the same priority + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c3 = newPulsarClient2.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(queueSize) .acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient3 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c4 = newPulsarClient3.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") .subscriptionType(SubscriptionType.Shared).receiverQueueSize(queueSize) .acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient4 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c5 = newPulsarClient4.newConsumer() .topic("persistent://my-property/my-ns/my-topic2").subscriptionName("my-subscriber-name") @@ -2323,11 +2328,6 @@ public void testSharedSamePriorityConsumer() throws Exception { c3.close(); c4.close(); c5.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); - newPulsarClient3.close(); - newPulsarClient4.close(); pulsar.getConfiguration().setMaxUnackedMessagesPerConsumer(maxUnAckMsgs); log.info("-- Exiting {} test --", methodName); } @@ -2797,18 +2797,21 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad .cryptoKeyReader(new EncKeyReader()).create(); // Creates new client connection + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0); Consumer consumer1 = newPulsarClient.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new EncKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); // Creates new client connection + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0); Consumer consumer2 = newPulsarClient1.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new InvalidKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); // Creates new client connection + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0); Consumer consumer3 = newPulsarClient2.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name") @@ -2854,9 +2857,6 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad consumer1.close(); consumer2.close(); consumer3.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); log.info("-- Exiting {} test --", methodName); } @@ -3763,6 +3763,7 @@ public void testGetStats() throws Exception { final String topicName = "persistent://my-property/my-ns/testGetStats" + UUID.randomUUID(); final String subName = "my-sub"; final int receiveQueueSize = 100; + @Cleanup PulsarClient client = newPulsarClient(lookupUrl.toString(), 100); Producer producer = pulsarClient.newProducer(Schema.STRING) .enableBatching(false).topic(topicName).create(); @@ -3788,6 +3789,7 @@ public void testGetStatsForPartitionedTopic() throws Exception { final int receiveQueueSize = 100; admin.topics().createPartitionedTopic(topicName, 3); + @Cleanup PulsarClient client = newPulsarClient(lookupUrl.toString(), 100); Producer producer = pulsarClient.newProducer(Schema.STRING) .enableBatching(false).topic(topicName).create(); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/v1/V1_ProducerConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/v1/V1_ProducerConsumerTest.java index 744ed8782555e..601827799dc53 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/v1/V1_ProducerConsumerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/v1/V1_ProducerConsumerTest.java @@ -1131,6 +1131,7 @@ public void testMutlipleSharedConsumerBlockingWithUnAckedMessages() throws Excep .subscriptionType(SubscriptionType.Shared) .subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient.newConsumer() .topic("persistent://my-property/use/my-ns/unacked-topic") @@ -1207,7 +1208,6 @@ public void testMutlipleSharedConsumerBlockingWithUnAckedMessages() throws Excep producer.close(); consumer1.close(); consumer2.close(); - newPulsarClient.close(); log.info("-- Exiting {} test --", methodName); } catch (Exception e) { fail(); @@ -1753,21 +1753,25 @@ public void testBlockUnackedConsumerRedeliverySpecificMessagesCloseConsumerWhile public void testPriorityConsumer() throws Exception { log.info("-- Starting {} test --", methodName); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer1 = newPulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .priorityLevel(1).receiverQueueSize(5).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .priorityLevel(1).receiverQueueSize(5).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer3 = newPulsarClient2.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .priorityLevel(1).receiverQueueSize(5).subscribe(); + @Cleanup PulsarClient newPulsarClient3 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer4 = newPulsarClient3.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) @@ -1814,10 +1818,6 @@ public void testPriorityConsumer() throws Exception { consumer2.close(); consumer3.close(); consumer4.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); - newPulsarClient3.close(); log.info("-- Exiting {} test --", methodName); } @@ -1845,6 +1845,7 @@ public void testSharedSamePriorityConsumer() throws Exception { .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .receiverQueueSize(queueSize).subscribe(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c2 = newPulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) @@ -1892,16 +1893,19 @@ public void testSharedSamePriorityConsumer() throws Exception { Assert.assertEquals(queueSize * 2, messages.size()); // create new consumers with the same priority + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c3 = newPulsarClient1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .receiverQueueSize(queueSize).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c4 = newPulsarClient2.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) .receiverQueueSize(queueSize).subscribe(); + @Cleanup PulsarClient newPulsarClient3 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer c5 = newPulsarClient3.newConsumer().topic("persistent://my-property/use/my-ns/my-topic2") .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared) @@ -1947,10 +1951,6 @@ public void testSharedSamePriorityConsumer() throws Exception { c3.close(); c4.close(); c5.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); - newPulsarClient3.close(); pulsar.getConfiguration().setMaxUnackedMessagesPerConsumer(maxUnAckMsgs); log.info("-- Exiting {} test --", methodName); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java index e9384022a9bff..c4d03adcd1cb1 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/BrokerClientIntegrationTest.java @@ -385,6 +385,7 @@ public void testUnsupportedBatchMessageConsumer(SubscriptionType subType) throws assertNull(msg); // subscribe consumer2 with supporting batch version + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0); // Creates new client connection Consumer consumer2 = newPulsarClient.newConsumer() .topic(topicName) @@ -406,7 +407,6 @@ public void testUnsupportedBatchMessageConsumer(SubscriptionType subType) throws consumer2.close(); producer.close(); batchProducer.close(); - newPulsarClient.close(); log.info("-- Exiting {} test --", methodName); } diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java index bca4a9fc5cdd2..b5dbc2238a641 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/impl/TopicDoesNotExistsTest.java @@ -71,6 +71,7 @@ public void testCreateProducerOnNotExistsTopic() throws PulsarClientException, I @Test public void testCreateConsumerOnNotExistsTopic() throws PulsarClientException, InterruptedException { + @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 1); try { pulsarClient.newConsumer() diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticationTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticationTest.java index 83efb842933f0..550aad43c0224 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticationTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyAuthenticationTest.java @@ -34,6 +34,7 @@ import javax.naming.AuthenticationException; +import lombok.Cleanup; import org.apache.pulsar.broker.ServiceConfiguration; import org.apache.pulsar.broker.authentication.AuthenticationDataSource; import org.apache.pulsar.broker.authentication.AuthenticationProvider; @@ -224,12 +225,14 @@ void testAuthentication() throws Exception { proxyConfig.setForwardAuthorizationCredentials(true); AuthenticationService authenticationService = new AuthenticationService( PulsarConfigurationLoader.convertFrom(proxyConfig)); + @Cleanup ProxyService proxyService = new ProxyService(proxyConfig, authenticationService); proxyService.start(); final String proxyServiceUrl = proxyService.getServiceUrl(); // Step 3: Pass correct client params + @Cleanup PulsarClient proxyClient = createPulsarClient(proxyServiceUrl, clientAuthParams, 1); proxyClient.newProducer(Schema.BYTES).topic(topicName).create(); // Sleep for 4 seconds - wait for proxy auth params to expire @@ -238,8 +241,6 @@ void testAuthentication() throws Exception { // Sleep for 3 seconds - wait for client auth parans to expire Thread.sleep(3 * 1000); proxyClient.newProducer(Schema.BYTES).topic(topicName).create(); - proxyClient.close(); - proxyService.close(); } private void updateAdminClient() throws PulsarClientException { diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java index a2e84fb77f7db..10889031a2780 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyParserTest.java @@ -218,6 +218,7 @@ public void testProtocolVersionAdvertisement() throws Exception { ClientConfigurationData conf = new ClientConfigurationData(); conf.setServiceUrl(proxyService.getServiceUrl()); + @Cleanup PulsarClient client = getClientActiveConsumerChangeNotSupported(conf); Producer producer = client.newProducer().topic(topic).create(); @@ -237,6 +238,8 @@ public void testProtocolVersionAdvertisement() throws Exception { producer.close(); consumer.close(); client.close(); + // shutdown EventLoopGroup created in getClientActiveConsumerChangeNotSupported method + ((PulsarClientImpl) client).getCnxPool().close(); } private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf) diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java index ef6f2e9f80de8..d3c1fe55f323c 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithAuthorizationTest.java @@ -442,7 +442,7 @@ public void tlsCiphersAndProtocols(Set tlsCiphers, Set tlsProtoc } }, 3, 1000); try { - + @Cleanup PulsarClient proxyClient = createPulsarClient("pulsar://localhost:" + proxyService.getListenPortTls().get(), PulsarClient.builder()); Consumer consumer = proxyClient.newConsumer() .topic("persistent://my-property/proxy-authorization/my-ns/my-topic1") @@ -452,7 +452,6 @@ public void tlsCiphersAndProtocols(Set tlsCiphers, Set tlsProtoc Assert.fail("Failure expected for this test case"); } consumer.close(); - proxyClient.close(); } catch (Exception ex) { if (!expectFailure) { Assert.fail("This test case should not fail"); diff --git a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithoutServiceDiscoveryTest.java b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithoutServiceDiscoveryTest.java index 1fec47534f1b1..3f2ab2c1a1017 100644 --- a/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithoutServiceDiscoveryTest.java +++ b/pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithoutServiceDiscoveryTest.java @@ -28,6 +28,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; +import lombok.Cleanup; import org.apache.pulsar.broker.authentication.AuthenticationProviderTls; import org.apache.pulsar.broker.authentication.AuthenticationService; import org.apache.pulsar.client.admin.PulsarAdmin; @@ -157,6 +158,7 @@ public void testDiscoveryService() throws Exception { Authentication authTls = new AuthenticationTls(); authTls.configure(authParams); // create a client which connects to proxy over tls and pass authData + @Cleanup PulsarClient proxyClient = createPulsarClient(authTls, proxyService.getServiceUrlTls()); admin.clusters().createCluster("without-service-discovery", new ClusterData(brokerUrl.toString())); diff --git a/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index dd1b7955a0592..d00463e810e3e 100644 --- a/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-admin-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -285,16 +285,19 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad .addEncryptionKey(encryptionKeyName).compressionType(CompressionType.LZ4) .cryptoKeyReader(new EncKeyReader()).create(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer1 = newPulsarClient.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new EncKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient1.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new InvalidKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer3 = newPulsarClient2.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); diff --git a/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index c2317df00c854..b4b04cd8abec0 100644 --- a/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-all-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -280,16 +280,19 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad .addEncryptionKey(encryptionKeyName).compressionType(CompressionType.LZ4) .cryptoKeyReader(new EncKeyReader()).create(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer1 = newPulsarClient.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new EncKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient1.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new InvalidKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer3 = newPulsarClient2.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); @@ -330,9 +333,6 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad consumer1.close(); consumer2.close(); consumer3.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); } @Test diff --git a/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java b/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java index d998aac0dbc8e..035453e1db877 100644 --- a/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java +++ b/tests/pulsar-client-shade-test/src/test/java/org/apache/pulsar/tests/integration/SimpleProducerConsumerTest.java @@ -284,16 +284,19 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad .addEncryptionKey(encryptionKeyName).compressionType(CompressionType.LZ4) .cryptoKeyReader(new EncKeyReader()).create(); + @Cleanup PulsarClient newPulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer1 = newPulsarClient.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new EncKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient1 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer2 = newPulsarClient1.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").cryptoKeyReader(new InvalidKeyReader()) .subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); + @Cleanup PulsarClient newPulsarClient2 = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection Consumer consumer3 = newPulsarClient2.newConsumer().topicsPattern(topicName) .subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Shared).ackTimeout(1, TimeUnit.SECONDS).subscribe(); @@ -334,9 +337,6 @@ public EncryptionKeyInfo getPrivateKey(String keyName, Map metad consumer1.close(); consumer2.close(); consumer3.close(); - newPulsarClient.close(); - newPulsarClient1.close(); - newPulsarClient2.close(); } @Test From fbccc7b2fcd85ee1ffb03b2d556cc981d520baf1 Mon Sep 17 00:00:00 2001 From: Lari Hotari Date: Thu, 15 Apr 2021 16:56:31 +0300 Subject: [PATCH 4/4] Fix cleanup for PersistentTopicE2ETest --- .../service/PersistentTopicE2ETest.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java index a697f26c9f7e3..62ae6b4cc104b 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicE2ETest.java @@ -39,7 +39,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; - import lombok.Cleanup; import lombok.Data; import lombok.EqualsAndHashCode; @@ -73,7 +72,6 @@ import org.apache.pulsar.client.impl.ProducerImpl; import org.apache.pulsar.client.impl.PulsarClientImpl; import org.apache.pulsar.client.impl.TypedMessageBuilderImpl; -import org.apache.pulsar.client.impl.auth.AuthenticationTls; import org.apache.pulsar.client.impl.schema.JSONSchema; import org.apache.pulsar.common.api.proto.CommandSubscribe.SubType; import org.apache.pulsar.common.naming.TopicName; @@ -92,6 +90,7 @@ @Test(groups = "flaky") public class PersistentTopicE2ETest extends BrokerTestBase { + private final List closeables = new ArrayList<>(); @BeforeMethod(alwaysRun = true) @Override @@ -102,6 +101,14 @@ protected void setup() throws Exception { @AfterMethod(alwaysRun = true) @Override protected void cleanup() throws Exception { + for (AutoCloseable closeable : closeables) { + try { + closeable.close(); + } catch (Exception e) { + // ignore exception + } + } + closeables.clear(); super.internalCleanup(); } @@ -1061,8 +1068,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 1. shared consumer on an exclusive sub fails try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Shared).subscribe(); fail("should have failed"); @@ -1072,8 +1079,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 2. failover consumer on an exclusive sub fails try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Failover).subscribe(); fail("should have failed"); @@ -1084,8 +1091,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 3. disconnected sub can be converted in shared consumer1.close(); try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Shared).subscribe(); assertEquals(subRef.getDispatcher().getType(), SubType.Shared); @@ -1095,8 +1102,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 4. exclusive fails on shared sub try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Exclusive).subscribe(); fail("should have failed"); @@ -1107,8 +1114,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 5. disconnected sub can be converted in failover consumer2.close(); try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Failover).subscribe(); assertEquals(subRef.getDispatcher().getType(), SubType.Failover); @@ -1119,8 +1126,8 @@ public void testSubscriptionTypeTransitions() throws Exception { // 5. exclusive consumer can connect after failover disconnects consumer3.close(); try { - @Cleanup PulsarClient pulsarClient = newPulsarClient(lookupUrl.toString(), 0);// Creates new client connection + closeables.add(pulsarClient); consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) .subscriptionType(SubscriptionType.Exclusive).subscribe(); assertEquals(subRef.getDispatcher().getType(), SubType.Exclusive);