chia7712 commented on code in PR #16661:
URL: https://github.com/apache/kafka/pull/16661#discussion_r1695547956


##########
core/src/test/java/kafka/testkit/KafkaClusterThreadFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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 kafka.testkit;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class KafkaClusterThreadFactory implements ThreadFactory {
+    private final String prefix;
+    private final Set<Long> threadIds = new HashSet<>();

Review Comment:
   Should it be thread-safe?



##########
core/src/test/java/kafka/testkit/KafkaClusterThreadFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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 kafka.testkit;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class KafkaClusterThreadFactory implements ThreadFactory {
+    private final String prefix;
+    private final Set<Long> threadIds = new HashSet<>();
+    private final AtomicLong threadEpoch = new AtomicLong(0);
+
+    KafkaClusterThreadFactory(String prefix) {
+        this.prefix = prefix;
+    }
+
+    @Override
+    public Thread newThread(Runnable r) {
+        String threadName = prefix + threadEpoch.addAndGet(1);
+        Thread thread = new Thread(r, threadName);
+        thread.setDaemon(false);

Review Comment:
   this is default value



##########
core/src/test/java/kafka/testkit/KafkaClusterTestKit.java:
##########
@@ -651,4 +648,12 @@ private void waitForAllFutures(List<Entry<String, 
Future<?>>> futureEntries)
             log.debug("{} successfully shut down.", entry.getKey());
         }
     }
+
+    private void waitForAllThreads() throws InterruptedException {
+        Set<Long> threadIds = threadFactory.getThreadIds();
+        // make sure all threads from threadFactory is shut down
+        TestUtils.waitForCondition(() -> Thread.getAllStackTraces().keySet()

Review Comment:
   we should wait it until those threads disappear from `getAllStackTraces`, 
right? 



##########
core/src/test/java/kafka/testkit/KafkaClusterTestKit.java:
##########
@@ -638,6 +634,7 @@ public void close() throws Exception {
             throw e;
         } finally {
             ThreadUtils.shutdownExecutorServiceQuietly(executorService, 5, 
TimeUnit.MINUTES);
+            waitForAllThreads();

Review Comment:
   we should not throw exception in the final block, since it will abort the 
current exception



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