eolivelli commented on code in PR #19153:
URL: https://github.com/apache/pulsar/pull/19153#discussion_r1063965653


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/TopicEventsListener.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+/**
+ * Listener for the Topic events.
+ */
+public interface TopicEventsListener {
+
+    /**
+     * Types of events currently supported.
+     *  create/load/unload/delete
+     */
+    enum TopicEvent {
+        // create events included into load events
+        CREATE,
+        LOAD,
+        UNLOAD,
+        DELETE,
+    }
+
+    /**
+     * Stages of events currently supported.
+     *  before starting the event/successful completion/failed completion
+     */
+    enum EventStage {
+        BEFORE,
+        SUCCESS,
+        FAILURE
+    }
+
+    /**
+     * Handle topic event.
+     * Choice of the thread / maintenance of the thread pool is up to the 
event handlers.
+     * @param topicName - name of the topic
+     * @param event - TopicEvent
+     * @param stage - EventStage
+     * @param t - exception in case of FAILURE, if present/known
+     */
+    void handleEvent(String topicName, TopicEvent event, EventStage stage, 
Throwable t);

Review Comment:
   We should allow the listener to throw exceptions and fail the operation in 
the  BEFORE  stage.
   
   For instance when the broker is going to delete a topic and the protocol 
handler  wants to prevent the deletion because the topic is a 'system topic' 
(the same for 'create')
   
   
   Handling erros during AFTER is very tricky because you cannot UNDO the 
operation, so I would only swallow every exception.



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/TopicEventsListenerTest.java:
##########
@@ -0,0 +1,687 @@
+/*
+ * 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;
+
+import com.google.common.collect.Sets;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.TimeUnit;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.service.AbstractTopic;
+import org.apache.pulsar.broker.service.BrokerTestBase;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.common.policies.data.InactiveTopicDeleteMode;
+import org.apache.pulsar.common.policies.data.InactiveTopicPolicies;
+import org.apache.pulsar.common.policies.data.RetentionPolicies;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertTrue;
+
+@Slf4j
+public class TopicEventsListenerTest extends BrokerTestBase {
+
+    final static Queue<String> events = new ConcurrentLinkedQueue<>();
+    String topicNameToWatch;
+    String namespace;
+
+    @BeforeMethod
+    @Override
+    protected void setup() throws Exception {
+        super.baseSetup();
+        pulsar.getConfiguration().setForceDeleteNamespaceAllowed(true);
+
+        pulsar.getBrokerService().addTopicEventListener((topic, event, stage, 
t) -> {
+            log.info("got event {}__{} for topic {}", event, stage, topic);
+            if (topic.equals(topicNameToWatch)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("got event {}__{} for topic {} with detailed 
stack",
+                            event, stage, topic, new Exception("tracing event 
source"));
+                }
+                events.add(event.toString() + "__" + stage.toString());
+            }
+        });
+
+        namespace = "prop/" + UUID.randomUUID();
+        admin.namespaces().createNamespace(namespace, Sets.newHashSet("test"));
+        
assertTrue(admin.namespaces().getNamespaces("prop").contains(namespace));
+        admin.namespaces().setRetention(namespace, new RetentionPolicies(3, 
10));
+
+        events.clear();
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        deleteNamespaceWithRetry(namespace, true);
+
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testEventsNonPersistentNonPartitionedTopic() throws Exception {
+        topicNameToWatch = "non-persistent://" + namespace + "/NP-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+            Assert.assertEquals(events.toArray(), new String[]{
+                    "LOAD__BEFORE",
+                    "LOAD__FAILURE",
+                    "LOAD__BEFORE",
+                    "CREATE__BEFORE",
+                    "CREATE__SUCCESS",
+                    "LOAD__SUCCESS"
+            })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsNonPersistentNonPartitionedTopicWithUnload() throws 
Exception {
+        topicNameToWatch = "non-persistent://" + namespace + "/NP-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsNonPersistentNonPartitionedTopicForce() throws 
Exception {
+        topicNameToWatch = "non-persistent://" + namespace + "/NP-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+
+    }
+
+    @Test
+    public void testEventsNonPersistentNonPartitionedTopicWithUnloadForce() 
throws Exception {
+        topicNameToWatch = "non-persistent://" + namespace + "/NP-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentNonPartitionedTopic() throws Exception {
+        topicNameToWatch = "persistent://" + namespace + "/P-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentNonPartitionedTopicWithUnload() throws 
Exception {
+        topicNameToWatch = "persistent://" + namespace + "/P-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentNonPartitionedTopicForce() throws 
Exception {
+        topicNameToWatch = "persistent://" + namespace + "/P-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentNonPartitionedTopicWithUnloadForce() 
throws Exception {
+        topicNameToWatch = "persistent://" + namespace + "/P-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "LOAD__FAILURE",
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicNameToWatch);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().delete(topicNameToWatch, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+
+//---------
+
+    @Test
+    public void testEventsNonPersistentPartitionedTopic() throws Exception {
+        String topicName = "non-persistent://" + namespace + "/P-P";
+        topicNameToWatch = topicName + "-partition-1";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS",
+                })
+        );
+
+        events.clear();
+
+        admin.topics().deletePartitionedTopic(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsNonPersistentPartitionedTopicWithUnload() throws 
Exception {
+        String topicName = "non-persistent://" + namespace + "/P-P";
+        topicNameToWatch = topicName + "-partition-1";
+
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsNonPersistentPartitionedTopicForce() throws 
Exception {
+        String topicName = "non-persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-1";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsNonPersistentPartitionedTopicWithUnloadForce() 
throws Exception {
+        String topicName = "non-persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-1";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentPartitionedTopic() throws Exception {
+        String topicName = "persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-1";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+
+        admin.topics().deletePartitionedTopic(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentPartitionedTopicWithUnload() throws 
Exception {
+        String topicName = "persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-0";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentPartitionedTopicForce() throws Exception {
+        String topicName = "persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-1";
+
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testEventsPersistentPartitionedTopicWithUnloadForce() throws 
Exception {
+        String topicName = "persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-0";
+
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "LOAD__BEFORE",
+                        "CREATE__BEFORE",
+                        "CREATE__SUCCESS",
+                        "LOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().unload(topicName);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS"
+                })
+        );
+
+        events.clear();
+        admin.topics().deletePartitionedTopic(topicName, true);
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "DELETE__BEFORE",
+                        "DELETE__SUCCESS"
+                })
+        );
+    }
+
+    @Test
+    public void testNonPartitionedTopicAutoGC() throws Exception {
+        topicNameToWatch = "persistent://" + namespace + "/P-NP";
+        admin.topics().createNonPartitionedTopic(topicNameToWatch);
+        admin.topics().setInactiveTopicPolicies(topicNameToWatch,
+                new 
InactiveTopicPolicies(InactiveTopicDeleteMode.delete_when_no_subscriptions, 1, 
true));
+
+        // Remove retention
+        admin.namespaces().setRetention(namespace, new RetentionPolicies());
+        events.clear();
+
+        Thread.sleep(1500);
+        runGC();
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                })
+        );
+
+    }
+
+    @Test
+    public void testPartitionedTopicAutoGC() throws Exception {
+        String topicName = "persistent://" + namespace + "/P-NP";
+        topicNameToWatch = topicName + "-partition-1";
+        admin.topics().createPartitionedTopic(topicName, 2);
+        triggerPartitionsCreation(topicName);
+        admin.topics().setInactiveTopicPolicies(topicName,
+                new 
InactiveTopicPolicies(InactiveTopicDeleteMode.delete_when_no_subscriptions, 1, 
true));
+
+        // Remove retention
+        admin.namespaces().setRetention(namespace, new RetentionPolicies());
+        events.clear();
+
+        Thread.sleep(1500);
+        runGC();
+
+        Awaitility.waitAtMost(10, TimeUnit.SECONDS).untilAsserted(() ->
+                Assert.assertEquals(events.toArray(), new String[]{
+                        "UNLOAD__BEFORE",
+                        "UNLOAD__SUCCESS",
+                })
+        );
+
+    }
+
+    private void triggerPartitionsCreation(String topicName) throws Exception {
+        Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topicName)
+                .create();
+        producer.close();
+    }
+
+    void runGC() {
+        try {
+            pulsar.getBrokerService().forEachTopic(topic -> {
+                if (topic instanceof AbstractTopic) {

Review Comment:
   Is there some risk that we delete also system topics?



-- 
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]

Reply via email to