Skip to content

Fix support for JVM shutdown hooks #4474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.platform.console.options.CommandFacade;
import org.junit.platform.console.options.CommandResult;
import org.junit.platform.console.tasks.ConsoleTestExecutor;
import org.junit.platform.console.tasks.CustomClassLoaderCloseStrategy;

/**
* The {@code ConsoleLauncher} is a stand-alone application for launching the
Expand All @@ -30,17 +31,20 @@
public class ConsoleLauncher {

public static void main(String... args) {
CommandResult<?> result = newCommandFacade().run(args);
CommandFacade facade = newCommandFacade(CustomClassLoaderCloseStrategy.KEEP_OPEN);
CommandResult<?> result = facade.run(args);
System.exit(result.getExitCode());
}

@API(status = INTERNAL, since = "1.0")
public static CommandResult<?> run(PrintWriter out, PrintWriter err, String... args) {
return newCommandFacade().run(args, out, err);
CommandFacade facade = newCommandFacade(CustomClassLoaderCloseStrategy.CLOSE_AFTER_CALLING_LAUNCHER);
return facade.run(args, out, err);
}

private static CommandFacade newCommandFacade() {
return new CommandFacade(ConsoleTestExecutor::new);
private static CommandFacade newCommandFacade(CustomClassLoaderCloseStrategy classLoaderCleanupStrategy) {
return new CommandFacade((discoveryOptions, outputOptions) -> new ConsoleTestExecutor(discoveryOptions,
outputOptions, classLoaderCleanupStrategy));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,48 @@ public class ConsoleTestExecutor {
private final TestDiscoveryOptions discoveryOptions;
private final TestConsoleOutputOptions outputOptions;
private final Supplier<Launcher> launcherSupplier;
private final CustomClassLoaderCloseStrategy classLoaderCloseStrategy;

public ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions) {
this(discoveryOptions, outputOptions, LauncherFactory::create);
this(discoveryOptions, outputOptions, CustomClassLoaderCloseStrategy.CLOSE_AFTER_CALLING_LAUNCHER);
}

public ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions,
CustomClassLoaderCloseStrategy classLoaderCloseStrategy) {
this(discoveryOptions, outputOptions, classLoaderCloseStrategy, LauncherFactory::create);
}

// for tests only
ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions,
Supplier<Launcher> launcherSupplier) {
this(discoveryOptions, outputOptions, CustomClassLoaderCloseStrategy.CLOSE_AFTER_CALLING_LAUNCHER,
launcherSupplier);
}

private ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions,
CustomClassLoaderCloseStrategy classLoaderCloseStrategy, Supplier<Launcher> launcherSupplier) {
this.discoveryOptions = discoveryOptions;
this.outputOptions = outputOptions;
this.launcherSupplier = launcherSupplier;
this.classLoaderCloseStrategy = classLoaderCloseStrategy;
}

public void discover(PrintWriter out) {
new CustomContextClassLoaderExecutor(createCustomClassLoader()).invoke(() -> {
createCustomContextClassLoaderExecutor().invoke(() -> {
discoverTests(out);
return null;
});
}

public TestExecutionSummary execute(PrintWriter out, Optional<Path> reportsDir) {
return new CustomContextClassLoaderExecutor(createCustomClassLoader()) //
return createCustomContextClassLoaderExecutor() //
.invoke(() -> executeTests(out, reportsDir));
}

private CustomContextClassLoaderExecutor createCustomContextClassLoaderExecutor() {
return new CustomContextClassLoaderExecutor(createCustomClassLoader(), classLoaderCloseStrategy);
}

private void discoverTests(PrintWriter out) {
Launcher launcher = launcherSupplier.get();
Optional<DetailsPrintingListener> commandLineTestPrinter = createDetailsPrintingListener(out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.platform.console.tasks;

import static org.apiguardian.api.API.Status.INTERNAL;

import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;

/**
* Defines the strategy for closing custom class loaders created for test
* discovery and execution.
*/
@API(status = INTERNAL, since = "1.13")
public enum CustomClassLoaderCloseStrategy {

/**
* Close the custom class loader after calling the
* {@link org.junit.platform.launcher.Launcher} for test discovery or
* execution.
*/
CLOSE_AFTER_CALLING_LAUNCHER {

@Override
public void handle(ClassLoader customClassLoader) {
if (customClassLoader instanceof AutoCloseable) {
close((AutoCloseable) customClassLoader);
}
}

private void close(AutoCloseable customClassLoader) {
try {
customClassLoader.close();
}
catch (Exception e) {
throw new JUnitException("Failed to close custom class loader", e);
}
}
},

/**
* Rely on the JVM to release resources held by the custom class loader when
* it terminates.
*
* <p>This mode is only safe to use when calling {@link System#exit(int)}
* afterward.
*/
KEEP_OPEN {
@Override
public void handle(ClassLoader customClassLoader) {
// do nothing
}
};

/**
* Handle the class loader according to the strategy.
*/
public abstract void handle(ClassLoader classLoader);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
import java.util.Optional;
import java.util.function.Supplier;

import org.junit.platform.commons.JUnitException;

/**
* @since 1.0
*/
class CustomContextClassLoaderExecutor {

private final Optional<ClassLoader> customClassLoader;
private final CustomClassLoaderCloseStrategy closeStrategy;

CustomContextClassLoaderExecutor(Optional<ClassLoader> customClassLoader) {
this(customClassLoader, CustomClassLoaderCloseStrategy.CLOSE_AFTER_CALLING_LAUNCHER);
}

CustomContextClassLoaderExecutor(Optional<ClassLoader> customClassLoader,
CustomClassLoaderCloseStrategy closeStrategy) {
this.customClassLoader = customClassLoader;
this.closeStrategy = closeStrategy;
}

<T> T invoke(Supplier<T> supplier) {
Expand All @@ -43,18 +48,7 @@ private <T> T replaceThreadContextClassLoaderAndInvoke(ClassLoader customClassLo
}
finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
if (customClassLoader instanceof AutoCloseable) {
close((AutoCloseable) customClassLoader);
}
}
}

private static void close(AutoCloseable customClassLoader) {
try {
customClassLoader.close();
}
catch (Exception e) {
throw new JUnitException("Failed to close custom class loader", e);
closeStrategy.handle(customClassLoader);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junit.platform.console.tasks;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -28,7 +29,7 @@
class CustomContextClassLoaderExecutorTests {

@Test
void invokeWithoutCustomClassLoaderDoesNotSetClassLoader() throws Exception {
void invokeWithoutCustomClassLoaderDoesNotSetClassLoader() {
var originalClassLoader = Thread.currentThread().getContextClassLoader();
var executor = new CustomContextClassLoaderExecutor(Optional.empty());

Expand All @@ -42,7 +43,7 @@ void invokeWithoutCustomClassLoaderDoesNotSetClassLoader() throws Exception {
}

@Test
void invokeWithCustomClassLoaderSetsCustomAndResetsToOriginal() throws Exception {
void invokeWithCustomClassLoaderSetsCustomAndResetsToOriginal() {
var originalClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader customClassLoader = URLClassLoader.newInstance(new URL[0]);
var executor = new CustomContextClassLoaderExecutor(Optional.of(customClassLoader));
Expand All @@ -57,7 +58,7 @@ void invokeWithCustomClassLoaderSetsCustomAndResetsToOriginal() throws Exception
}

@Test
void invokeWithCustomClassLoaderAndEnsureItIsClosedAfterUsage() throws Exception {
void invokeWithCustomClassLoaderAndEnsureItIsClosedAfterUsage() {
var closed = new AtomicBoolean(false);
ClassLoader localClassLoader = new URLClassLoader(new URL[0]) {
@Override
Expand All @@ -73,4 +74,23 @@ public void close() throws IOException {
assertEquals(4711, result);
assertTrue(closed.get());
}

@Test
void invokeWithCustomClassLoaderAndKeepItOpenAfterUsage() {
var closed = new AtomicBoolean(false);
ClassLoader localClassLoader = new URLClassLoader(new URL[0]) {
@Override
public void close() throws IOException {
closed.set(true);
super.close();
}
};
var executor = new CustomContextClassLoaderExecutor(Optional.of(localClassLoader),
CustomClassLoaderCloseStrategy.KEEP_OPEN);

int result = executor.invoke(() -> 4711);

assertEquals(4711, result);
assertFalse(closed.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package other;

public class OtherwiseNotReferencedClass {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class JupiterIntegration {

@Test
void successful() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
new other.OtherwiseNotReferencedClass();
}));
}

@Test
Expand Down
Loading