lhotari commented on code in PR #25311: URL: https://github.com/apache/pulsar/pull/25311#discussion_r2926937678
########## pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SharedPulsarBaseTest.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.broker.service; + +import java.util.Set; +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.admin.PulsarAdmin; +import org.apache.pulsar.client.api.PulsarClient; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; + +/** + * Base class for tests that share a single {@link SharedPulsarCluster} singleton across all test classes. + * + * <p>Each test method gets its own unique namespace (created in {@link #setupSharedTest()} and + * force-deleted in {@link #cleanupSharedTest()}), providing full isolation without the overhead + * of starting a new broker per test. + * + * <p>Subclasses get access to {@link #admin} and {@link #pulsarClient} (initialized once per class) + * and can use {@link #newTopicName()} to generate unique topic names within the test namespace. + */ +@Slf4j +public abstract class SharedPulsarBaseTest { + + protected PulsarAdmin admin; + protected PulsarClient pulsarClient; + + private String namespace; + + /** + * Returns the unique namespace assigned to the current test method. + */ + protected String getNamespace() { + return namespace; + } + + /** + * Initializes the shared cluster singleton and sets up {@link #admin} and {@link #pulsarClient}. + * Called once per test class. + */ + @BeforeClass(alwaysRun = true) + public void setupSharedCluster() throws Exception { + SharedPulsarCluster cluster = SharedPulsarCluster.get(); + admin = cluster.getAdmin(); + pulsarClient = cluster.getClient(); + } + + /** + * Creates a unique namespace for the current test method. The namespace is automatically + * cleaned up in {@link #cleanupSharedTest()}. + */ + @BeforeMethod(alwaysRun = true) + public void setupSharedTest() throws Exception { + String nsName = "test-" + UUID.randomUUID().toString().substring(0, 8); + String ns = SharedPulsarCluster.TENANT_NAME + "/" + nsName; + namespace = ns; + admin.namespaces().createNamespace(ns, Set.of(SharedPulsarCluster.CLUSTER_NAME)); Review Comment: It would be useful to move this logic as a method directly to SharedPulsarCluster class. The benefit of that would be to reuse SharedPulsarCluster later in Junit Jupiter tests when we start doing that for Pulsar 5.0 implementation. This can also be done later. ########## pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SharedPulsarBaseTest.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.broker.service; + +import java.util.Set; +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.admin.PulsarAdmin; +import org.apache.pulsar.client.api.PulsarClient; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; + +/** + * Base class for tests that share a single {@link SharedPulsarCluster} singleton across all test classes. + * + * <p>Each test method gets its own unique namespace (created in {@link #setupSharedTest()} and + * force-deleted in {@link #cleanupSharedTest()}), providing full isolation without the overhead + * of starting a new broker per test. + * + * <p>Subclasses get access to {@link #admin} and {@link #pulsarClient} (initialized once per class) + * and can use {@link #newTopicName()} to generate unique topic names within the test namespace. + */ +@Slf4j +public abstract class SharedPulsarBaseTest { + + protected PulsarAdmin admin; + protected PulsarClient pulsarClient; + + private String namespace; + + /** + * Returns the unique namespace assigned to the current test method. + */ + protected String getNamespace() { + return namespace; + } + + /** + * Initializes the shared cluster singleton and sets up {@link #admin} and {@link #pulsarClient}. + * Called once per test class. + */ + @BeforeClass(alwaysRun = true) + public void setupSharedCluster() throws Exception { + SharedPulsarCluster cluster = SharedPulsarCluster.get(); + admin = cluster.getAdmin(); + pulsarClient = cluster.getClient(); + } + + /** + * Creates a unique namespace for the current test method. The namespace is automatically + * cleaned up in {@link #cleanupSharedTest()}. + */ + @BeforeMethod(alwaysRun = true) + public void setupSharedTest() throws Exception { + String nsName = "test-" + UUID.randomUUID().toString().substring(0, 8); + String ns = SharedPulsarCluster.TENANT_NAME + "/" + nsName; + namespace = ns; + admin.namespaces().createNamespace(ns, Set.of(SharedPulsarCluster.CLUSTER_NAME)); + log.info("Created test namespace: {}", ns); + } + + /** + * Force-deletes the namespace created by {@link #setupSharedTest()}, including all topics in it. + */ + @AfterMethod(alwaysRun = true) + public void cleanupSharedTest() throws Exception { + String ns = namespace; + if (ns != null) { + namespace = null; + try { + admin.namespaces().deleteNamespace(ns, true); + log.info("Deleted test namespace: {}", ns); + } catch (Exception e) { + log.warn("Failed to delete namespace {}: {}", ns, e.getMessage()); + } Review Comment: Similar comment as above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
