chia7712 commented on code in PR #16499: URL: https://github.com/apache/kafka/pull/16499#discussion_r1667734868
########## core/src/test/java/kafka/test/junit/DetectThreadLeak.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.test.junit; + +import org.apache.kafka.test.TestUtils; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +public class DetectThreadLeak { Review Comment: The purpose of this class is accurate, so maybe we can simplify it to a `FunctionalInterface`. For example: ```java interface DetectThreadLeak { // return the new threads after `DetectThreadLeak` is created List<Thread> newThreads(); } ``` Also, we can create `DetectThreadLeak` with filter ```java DetectThreadLeak.filter(Predicator<Thread>); ``` ########## core/src/test/java/kafka/test/junit/DetectThreadLeak.java: ########## @@ -0,0 +1,71 @@ +/* + * 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.test.junit; + +import org.apache.kafka.test.TestUtils; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +public class DetectThreadLeak { + private Set<Thread> threads; + private Set<String> expectedThreadNames = new HashSet<>( + Arrays.asList( + "Attach Listener", "client-metrics-reaper", "executor-", "feature-zk-node-event-process-thread", + "ForkJoinPool", "JMX", "junit-", "metrics-meter-tick-thread", "pool-", "process reaper", "RMI", + "scala-" + ) + ); + + public DetectThreadLeak(Optional<Set<String>> overrideExpectedThreadNames) { + overrideExpectedThreadNames.ifPresent(strings -> expectedThreadNames = strings); + System.out.println("Expected thread names: " + expectedThreadNames); + } + + public void beforeEach() { + Thread.getAllStackTraces().keySet().forEach(thread -> { Review Comment: Maybe we can store the <thread id, thread name> instead of whole thread? ########## core/src/test/java/kafka/test/junit/ClusterTestExtensions.java: ########## @@ -82,7 +84,9 @@ * SomeIntegrationTest will be instantiated, lifecycle methods (before/after) will be run, and "someTest" will be invoked. * */ -public class ClusterTestExtensions implements TestTemplateInvocationContextProvider { +public class ClusterTestExtensions implements TestTemplateInvocationContextProvider, BeforeEachCallback, AfterEachCallback { Review Comment: Maybe this hook should be move to `RaftClusterInvocationContext` and `ZkClusterInvocationContext`. The tests using those clusters are what we care about. -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org