Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ ext {
repo = file("$rootDir/.git").isDirectory() ? Grgit.open(currentDir: project.getRootDir()) : null

commitId = determineCommitId()

junitExtensionsAutodetectionEnabled = "junit.jupiter.extensions.autodetection.enabled"
}

allprojects {
Expand Down Expand Up @@ -439,6 +441,7 @@ subprojects {
displayGranularity = 0
}
logTestStdout.rehydrate(delegate, owner, this)()
systemProperty(junitExtensionsAutodetectionEnabled, true)

exclude testsToExclude

Expand Down Expand Up @@ -467,6 +470,7 @@ subprojects {
displayGranularity = 0
}
logTestStdout.rehydrate(delegate, owner, this)()
systemProperty(junitExtensionsAutodetectionEnabled, true)

exclude testsToExclude

Expand Down Expand Up @@ -510,6 +514,7 @@ subprojects {
displayGranularity = 0
}
logTestStdout.rehydrate(delegate, owner, this)()
systemProperty(junitExtensionsAutodetectionEnabled, true)

exclude testsToExclude

Expand Down
84 changes: 84 additions & 0 deletions core/src/test/java/kafka/test/junit/LeakTestingExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 kafka.utils.TestUtils;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import scala.Tuple2;

import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LeakTestingExtension implements BeforeEachCallback, AfterEachCallback {
private static final Set<String> EXPECTED_THREAD_NAMES = new HashSet<>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hy @gharris1727 @wernerdv
Can you please help me understand why it is expected to have kafka-scheduler or ExpirationReaper or ReplicaFetcherThread at the end or beginning of test? Isn't presence of these threads an indication of a thread leak? (for example, expiration reaper threads may mean that we don't close controllerAPIs correctly)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't notice that this is a set of threads that are allowed to leak, where the original TestUtils.verifyNoUnexpectedThreads was a set of threads which weren't allowed to leak. I assumed the mechanism was copy-pasted.

I don't know if this set needs to exist, especially now that the assertion is stateful. Perhaps we can empty this set completely to see what the effect is, and then add in what is necessary. I did see some flakiness with an early prototype of #14783 which sometimes detected the gradle runner opening sockets. Perhaps there are similar things which create background threads.

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-"
)
);
private static final String THREADS_KEY = "threads";

@Override
public void beforeEach(ExtensionContext context) {
getStore(context).put(THREADS_KEY, Thread.getAllStackTraces().keySet());
}

@Override
@SuppressWarnings("unchecked")
public void afterEach(ExtensionContext context) {
Set<Thread> initialThreads = getStore(context).remove(THREADS_KEY, Set.class);
Tuple2<Set<Thread>, Object> unexpectedThreads = TestUtils.computeUntilTrue(
() -> unexpectedThreads(initialThreads),
DEFAULT_MAX_WAIT_MS,
100L,
Set::isEmpty
);

assertTrue(unexpectedThreads._1.isEmpty(), "Found unexpected threads after executing test: " +
unexpectedThreads._1.stream().map(Objects::toString).collect(Collectors.joining(", ")));
}

private Set<Thread> unexpectedThreads(Set<Thread> initialThreads) {
Set<Thread> threads = new HashSet<>(Thread.getAllStackTraces().keySet());
return threads.stream()
.filter(t -> !initialThreads.contains(t))
.filter(t -> {
for (String s: EXPECTED_THREAD_NAMES) {
if (t.getName().contains(s))
return false;
}
return true;
})
.collect(Collectors.toSet());
}

private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

kafka.test.junit.LeakTestingExtension