Top Description Inners Constructors Methods
org.junit.jupiter.api.util

public Class PreemptiveTimeoutUtils

extends Object
Class Inheritance
Annotations
@API
status:INTERNAL
since:6.0
Static Imports
java.util.Objects.requireNonNullElse, org.apiguardian.api.API.Status.INTERNAL, org.junit.platform.commons.util.ExceptionUtils.throwAsUncheckedException

Internal utilities for executing code with a preemptive timeout.
Since
6.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
PreemptiveTimeoutUtils.TimeoutFailureFactory<
The type of error or exception created
T extends Throwable
>

Factory for timeout failures.

private static class
PreemptiveTimeoutUtils.TimeoutThreadFactory

The thread factory used for preemptive timeout.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T extends @Nullable Object, E extends Throwable> T
executeWithPreemptiveTimeout(Duration timeout, ThrowingSupplier<T> supplier, @Nullable Supplier<@Nullable String> messageSupplier, PreemptiveTimeoutUtils.TimeoutFailureFactory<E> failureFactory)

Assert that execution of the supplied supplier completes before the given timeout is exceeded.

private static <T extends @Nullable Object, E extends Throwable> T
private static <T extends @Nullable Object> Future<T>
submitTask(ThrowingSupplier<T> supplier, AtomicReference<Thread> threadReference, ExecutorService executorService)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

PreemptiveTimeoutUtilsback to summary
private PreemptiveTimeoutUtils()

Method Detail

executeWithPreemptiveTimeoutback to summary
public static <T extends @Nullable Object, E extends Throwable> T executeWithPreemptiveTimeout(Duration timeout, ThrowingSupplier<T> supplier, @Nullable Supplier<@Nullable String> messageSupplier, PreemptiveTimeoutUtils.TimeoutFailureFactory<E> failureFactory) throws TimeoutFailureFactory-:E

Assert that execution of the supplied supplier completes before the given timeout is exceeded.

See the Preemptive Timeouts section of the class-level Javadoc for further details.

If the assertion passes then the supplier's result is returned.

In the case the assertion does not pass, the supplied TimeoutFailureFactory is invoked to create an exception which is then thrown.

If necessary, the failure message will be retrieved lazily from the supplied messageSupplier.

resolveFutureAndHandleExceptionback to summary
private static <T extends @Nullable Object, E extends Throwable> T resolveFutureAndHandleException(Future<T> future, Duration timeout, @Nullable Supplier<@Nullable String> messageSupplier, Supplier<@Nullable Thread> threadSupplier, PreemptiveTimeoutUtils.TimeoutFailureFactory<E> failureFactory) throws TimeoutFailureFactory-:E, RuntimeException
submitTaskback to summary
private static <T extends @Nullable Object> Future<T> submitTask(ThrowingSupplier<T> supplier, AtomicReference<Thread> threadReference, ExecutorService executorService)
org.junit.jupiter.api.util back to summary

private Class PreemptiveTimeoutUtils.ExecutionTimeoutException

extends JUnitException
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.junit.platform.commons.JUnitException.serialVersionUID.

Annotations
@Serial

Constructor Detail

ExecutionTimeoutExceptionback to summary
pack-priv ExecutionTimeoutException(String message)
org.junit.jupiter.api.util back to summary

public Interface PreemptiveTimeoutUtils.TimeoutFailureFactory<T extends Throwable>

Type Parameters
<T>
The type of error or exception created

Factory for timeout failures.
Since
5.9.1
See Also
PreemptiveTimeoutUtils#executeWithPreemptiveTimeout(Duration, ThrowingSupplier, Supplier, TimeoutFailureFactory)

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

timeout failure; never null
createTimeoutFailure
(Duration timeout, @Nullable Supplier<@Nullable String> messageSupplier, @Nullable Throwable cause, @Nullable Thread testThread)

Create a failure for the given timeout, message, and cause.

Method Detail

createTimeoutFailureback to summary
public T createTimeoutFailure(Duration timeout, @Nullable Supplier<@Nullable String> messageSupplier, @Nullable Throwable cause, @Nullable Thread testThread)

Create a failure for the given timeout, message, and cause.

Returns:T

timeout failure; never null

org.junit.jupiter.api.util back to summary

private Class PreemptiveTimeoutUtils.TimeoutThreadFactory

extends Object
implements ThreadFactory
Class Inheritance
  • java.lang.Object
  • org.junit.jupiter.api.util.PreemptiveTimeoutUtils.TimeoutThreadFactory
All Implemented Interfaces
java.util.concurrent.ThreadFactory

The thread factory used for preemptive timeout.

The factory creates threads with meaningful names, helpful for debugging purposes.

Field Summary

Modifier and TypeField and Description
private static final AtomicInteger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Thread
newThread(Runnable r)

Implements java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

threadNumberback to summary
private static final AtomicInteger threadNumber

Constructor Detail

TimeoutThreadFactoryback to summary
private TimeoutThreadFactory()

Method Detail

newThreadback to summary
public Thread newThread(Runnable r)

Implements java.util.concurrent.ThreadFactory.newThread.

Doc from java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.

Annotations
@Override