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


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/TopicEventsListenerTest.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * 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.commons.lang3.ArrayUtils;
+import org.apache.pulsar.broker.service.BrokerTestBase;
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminException;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClientException;
+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.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+@Slf4j
+public class TopicEventsListenerTest extends BrokerTestBase {
+
+    final static Queue<String> events = new ConcurrentLinkedQueue<>();
+    volatile String topicNameToWatch;
+    String namespace;
+
+    @DataProvider(name = "topicType")
+    public static Object[][] topicType() {
+        return new Object[][] {
+                {"persistent", "partitioned", Boolean.TRUE},
+                {"persistent", "non-partitioned", Boolean.TRUE},
+                {"non-persistent", "partitioned", Boolean.TRUE},
+                {"non-persistent", "non-partitioned", Boolean.TRUE},
+                {"persistent", "partitioned", Boolean.FALSE},
+                {"persistent", "non-partitioned", Boolean.FALSE},
+                {"non-persistent", "partitioned", Boolean.FALSE},
+                {"non-persistent", "non-partitioned", Boolean.FALSE}
+        };
+    }
+
+    @DataProvider(name = "topicTypeNoDelete")
+    public static Object[][] topicTypeNoDelete() {
+        return new Object[][] {
+                {"persistent", "partitioned"},
+                {"persistent", "non-partitioned"},
+                {"non-persistent", "partitioned"},
+                {"non-persistent", "non-partitioned"}
+        };
+    }
+
+    @BeforeClass
+    @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());
+            }
+        });
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @BeforeMethod
+    protected void setupTest() throws Exception {
+        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));
+        try (PulsarAdmin admin2 = createPulsarAdmin()) {
+            Awaitility.await().untilAsserted(() ->
+                    assertEquals(admin2.namespaces().getRetention(namespace), 
new RetentionPolicies(3, 10)));
+        }
+
+        events.clear();
+    }
+
+    @AfterMethod(alwaysRun = true)
+    protected void cleanupTest() throws Exception {
+        deleteNamespaceWithRetry(namespace, true);
+    }
+
+    @Test(dataProvider = "topicType")
+    public void testEvents(Object[] topicType) throws Exception {
+        String topicTypePersistence = (String) topicType[0];
+        String topicTypePartitioned = (String) topicType[1];
+        boolean forceDelete = (Boolean) topicType[2];

Review Comment:
   ```suggestion
       public void testEvents(String topicTypePersistence, String 
topicTypePartitioned,
                              boolean forceDelete) throws Exception {
   ```
   
   And we can use `boolean` instead of `Boolean` for the 3rd array element in 
`topicType`. The `Object[]` argument can be extracted to variant arguments 
automatically. You can apply similar changes for other tests as well.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java:
##########
@@ -1032,21 +1046,41 @@ public CompletableFuture<Optional<Topic>> 
getTopic(final TopicName topicName, bo
                     return loadOrCreatePersistentTopic(tpName, 
createIfMissing, properties);
                 });
             } else {
-            return topics.computeIfAbsent(topicName.toString(), (name) -> {
+                return topics.computeIfAbsent(topicName.toString(), (name) -> {
+                    topicEventsDispatcher.notify(topicName.toString(), 
TopicEvent.LOAD, EventStage.BEFORE);
                     if (topicName.isPartitioned()) {
                         final TopicName partitionedTopicName = 
TopicName.get(topicName.getPartitionedTopicName());
                         return 
this.fetchPartitionedTopicMetadataAsync(partitionedTopicName).thenCompose((metadata)
 -> {
                             if (topicName.getPartitionIndex() < 
metadata.partitions) {
-                                return createNonPersistentTopic(name);
+                                topicEventsDispatcher
+                                        .notify(topicName.toString(), 
TopicEvent.CREATE, EventStage.BEFORE);
+
+                                CompletableFuture<Optional<Topic>> res = 
createNonPersistentTopic(name);
+
+                                CompletableFuture<Optional<Topic>> eventFuture 
= topicEventsDispatcher
+                                        .notifyOnCompletion(res, 
topicName.toString(), TopicEvent.CREATE);
+                                topicEventsDispatcher
+                                        .notifyOnCompletion(eventFuture, 
topicName.toString(), TopicEvent.LOAD);
+                                return res;
                             }
+                            topicEventsDispatcher.notify(topicName.toString(), 
TopicEvent.LOAD, EventStage.FAILURE);
                             return 
CompletableFuture.completedFuture(Optional.empty());
                         });
                     } else if (createIfMissing) {
-                        return createNonPersistentTopic(name);
+                        topicEventsDispatcher.notify(topicName.toString(), 
TopicEvent.CREATE, EventStage.BEFORE);
+
+                        CompletableFuture<Optional<Topic>> res = 
createNonPersistentTopic(name);
+
+                        CompletableFuture eventFuture = topicEventsDispatcher

Review Comment:
   ```suggestion
                           CompletableFuture<Optional<Topic>> eventFuture = 
topicEventsDispatcher
   ```



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