-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PreInterruptCallback extension to allow to hook into the @timeout extension before the executing Thread is interrupted. The default implementation of PreInterruptCallback will simply print the stacks of all Thread to System.out. It is disabled by default and must be enabled with: junit.jupiter.extensions.preinterruptcallback.default.enabled = true Issue: #2938
- Loading branch information
Showing
20 changed files
with
472 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/PreInterruptCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2015-2023 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.jupiter.api.extension; | ||
|
||
import static org.apiguardian.api.API.Status.EXPERIMENTAL; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
/** | ||
* {@code PreInterruptCallback} defines the API for {@link Extension | ||
* Extensions} that wish to react on {@link Thread#interrupt()} calls issued by Jupiter | ||
* before the {@link Thread#interrupt()} is executed. | ||
* | ||
* <p>This can be used to e.g. dump stacks for diagnostics, when the {@link org.junit.jupiter.api.Timeout} | ||
* extension is used.</p> | ||
* | ||
* <p>There is also a default implementation available, which will dump the stacks of all {@link Thread Threads} | ||
* to {@code System.out}. This default implementation need to be enabled with the jupiter property: | ||
* {@code junit.jupiter.extensions.preinterruptcallback.default.enabled} | ||
* | ||
* | ||
* @since 5.11 | ||
* @see org.junit.jupiter.api.Timeout | ||
*/ | ||
@API(status = EXPERIMENTAL, since = "5.11") | ||
public interface PreInterruptCallback extends Extension { | ||
|
||
/** | ||
* Callback that is invoked <em>before</em> a {@link Thread} is interrupted with {@link Thread#interrupt()}. | ||
* | ||
* <p>Caution: There is no guarantee on which {@link Thread} this callback will be executed.</p> | ||
* | ||
* @param threadToInterrupt the target {@link Thread}, which will get interrupted. | ||
* @param context the current extension context; never {@code null} | ||
*/ | ||
void beforeThreadInterrupt(Thread threadToInterrupt, ExtensionContext context) throws Exception; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...-engine/src/main/java/org/junit/jupiter/engine/extension/DefaultPreInterruptCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2015-2023 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.jupiter.engine.extension; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.PreInterruptCallback; | ||
import org.junit.jupiter.engine.Constants; | ||
|
||
/** | ||
* The default implementation for {@link PreInterruptCallback}, | ||
* which will print the stacks of all {@link Thread}s to {@code System.out}. | ||
* | ||
* <p>Note: This is disabled by default, and must be enabled with | ||
* {@link Constants#EXTENSIONS_DEFAULT_PRE_INTERRUPT_CALLBACK_ENABLED_PROPERTY_NAME} | ||
* | ||
* @since 5.11 | ||
*/ | ||
public class DefaultPreInterruptCallback implements PreInterruptCallback { | ||
private static final String NL = "\n"; | ||
|
||
@Override | ||
public void beforeThreadInterrupt(Thread threadToInterrupt, ExtensionContext context) { | ||
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Thread "); | ||
appendThreadName(sb, threadToInterrupt); | ||
sb.append(" will be interrupted."); | ||
sb.append(NL); | ||
for (Map.Entry<Thread, StackTraceElement[]> entry : stackTraces.entrySet()) { | ||
Thread thread = entry.getKey(); | ||
StackTraceElement[] stack = entry.getValue(); | ||
if (stack.length > 0) { | ||
sb.append(NL); | ||
appendThreadName(sb, thread); | ||
for (StackTraceElement stackTraceElement : stack) { | ||
sb.append(NL); | ||
//Do the same prefix as java.lang.Throwable.printStackTrace(java.lang.Throwable.PrintStreamOrWriter) | ||
sb.append("\tat "); | ||
sb.append(stackTraceElement.toString()); | ||
|
||
} | ||
sb.append(NL); | ||
} | ||
} | ||
System.out.println(sb); | ||
} | ||
|
||
/** | ||
* Appends the {@link Thread} name and ID in a similar fashion as {@code jstack}. | ||
* @param sb the buffer | ||
* @param th the thread to append | ||
*/ | ||
private void appendThreadName(StringBuilder sb, Thread th) { | ||
sb.append("\""); | ||
sb.append(th.getName()); | ||
sb.append("\""); | ||
sb.append(" #"); | ||
sb.append(th.getId()); | ||
if (th.isDaemon()) { | ||
sb.append(" daemon"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...gine/src/main/java/org/junit/jupiter/engine/extension/PreInterruptCallbackInvocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2015-2023 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.jupiter.engine.extension; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* @since 5.11 | ||
*/ | ||
@FunctionalInterface | ||
interface PreInterruptCallbackInvocation { | ||
PreInterruptCallbackInvocation NOOP = (t, e) -> { | ||
}; | ||
|
||
void executePreInterruptCallback(Thread threadToInterrupt, Consumer<Throwable> errorHandler); | ||
} |
46 changes: 46 additions & 0 deletions
46
...c/main/java/org/junit/jupiter/engine/extension/PreInterruptCallbackInvocationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2015-2023 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.jupiter.engine.extension; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.PreInterruptCallback; | ||
import org.junit.platform.commons.util.UnrecoverableExceptions; | ||
|
||
/** | ||
* @since 5.11 | ||
*/ | ||
final class PreInterruptCallbackInvocationFactory { | ||
|
||
private PreInterruptCallbackInvocationFactory() { | ||
|
||
} | ||
|
||
static PreInterruptCallbackInvocation create(ExtensionContext extensionContext) { | ||
ExtensionRegistry registry = MutableExtensionRegistry.getRegistryFromExtensionContext(extensionContext); | ||
if (registry == null) { | ||
return PreInterruptCallbackInvocation.NOOP; | ||
} | ||
List<PreInterruptCallback> callbacks = registry.getExtensions(PreInterruptCallback.class); | ||
return (thread, errorHandler) -> { | ||
for (PreInterruptCallback callback : callbacks) { | ||
try { | ||
callback.beforeThreadInterrupt(thread, extensionContext); | ||
} | ||
catch (Throwable ex) { | ||
UnrecoverableExceptions.rethrowIfUnrecoverable(ex); | ||
errorHandler.accept(ex); | ||
} | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.