chia7712 commented on code in PR #16499: URL: https://github.com/apache/kafka/pull/16499#discussion_r1675284548
########## core/src/test/java/kafka/test/junit/ClusterTestExtensions.java: ########## @@ -119,7 +124,27 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex return generatedContexts.stream(); } + @Override + @SuppressWarnings("booleanexpressioncomplexity") + public void beforeEach(ExtensionContext context) { + detectThreadLeak = DetectThreadLeak.of(thread -> { + String name = thread.getName(); + return !name.startsWith("metrics-meter-tick-thread") && + !name.startsWith("scala-") && + !name.startsWith("ForkJoinPool") && + !name.startsWith("junit-") && + !name.startsWith("Attach Listener") && + !name.startsWith("process reaper") && + !name.startsWith("RMI"); + }); + } + @Override + public void afterEach(ExtensionContext context) throws InterruptedException { + TestUtils.waitForCondition(() -> detectThreadLeak.newThreads().isEmpty(), Review Comment: Could you add null check to avoid NPE? ########## core/src/test/java/kafka/test/junit/DetectThreadLeakTest.java: ########## @@ -0,0 +1,61 @@ +/* + * 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 org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DetectThreadLeakTest { + + private class LeakThread implements Runnable { + @Override + public void run() { + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + @Test + public void testThreadLeak() throws InterruptedException { + DetectThreadLeak detectThreadLeak = DetectThreadLeak.of(thread -> true); + Thread leakThread = new Thread(new LeakThread()); + leakThread.start(); + assertTrue(detectThreadLeak.newThreads().contains(leakThread)); + leakThread.interrupt(); + TestUtils.waitForCondition(() -> !leakThread.isAlive(), "Can't interrupt the thread"); + leakThread.isAlive(); Review Comment: It needs assert, right? ########## core/src/test/java/kafka/test/junit/ClusterTestExtensions.java: ########## @@ -119,7 +124,27 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex return generatedContexts.stream(); } + @Override + @SuppressWarnings("booleanexpressioncomplexity") + public void beforeEach(ExtensionContext context) { + detectThreadLeak = DetectThreadLeak.of(thread -> { + String name = thread.getName(); + return !name.startsWith("metrics-meter-tick-thread") && Review Comment: Please add a constant for those constants ########## core/src/test/java/kafka/test/junit/DetectThreadLeakTest.java: ########## @@ -0,0 +1,61 @@ +/* + * 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 org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DetectThreadLeakTest { + + private class LeakThread implements Runnable { + @Override + public void run() { + try { + Thread.sleep(20000); + } catch (InterruptedException e) { + e.printStackTrace(); Review Comment: please remove this and add comment instead ########## core/src/test/java/kafka/test/junit/DetectThreadLeak.java: ########## @@ -0,0 +1,39 @@ +/* + * 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 java.util.List; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public interface DetectThreadLeak { + /** + * @return the new threads after `DetectThreadLeak` is created + */ + List<Thread> newThreads(); + + static DetectThreadLeak of(Predicate<Thread> predicate) { Review Comment: please add comments -- 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