| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package org.junit.jupiter.api; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; |
| 14 |
import static org.junit.platform.commons.util.ExceptionUtils.throwAsUncheckedException; |
| 15 |
|
| 16 |
import java.time.Duration; |
| 17 |
import java.util.concurrent.ExecutionException; |
| 18 |
import java.util.concurrent.ExecutorService; |
| 19 |
import java.util.concurrent.Executors; |
| 20 |
import java.util.concurrent.Future; |
| 21 |
import java.util.concurrent.ThreadFactory; |
| 22 |
import java.util.concurrent.TimeUnit; |
| 23 |
import java.util.concurrent.TimeoutException; |
| 24 |
import java.util.concurrent.atomic.AtomicInteger; |
| 25 |
import java.util.concurrent.atomic.AtomicReference; |
| 26 |
import java.util.function.Supplier; |
| 27 |
|
| 28 |
import org.junit.jupiter.api.function.Executable; |
| 29 |
import org.junit.jupiter.api.function.ThrowingSupplier; |
| 30 |
import org.junit.platform.commons.JUnitException; |
| 31 |
import org.opentest4j.AssertionFailedError; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class AssertTimeoutPreemptively { |
| 41 |
|
| 42 |
static void assertTimeoutPreemptively(Duration timeout, Executable executable) { |
| 43 |
assertTimeoutPreemptively(timeout, executable, (String) null); |
| 44 |
} |
| 45 |
|
| 46 |
static void assertTimeoutPreemptively(Duration timeout, Executable executable, String message) { |
| 47 |
assertTimeoutPreemptively(timeout, () -> { |
| 48 |
executable.execute(); |
| 49 |
return null; |
| 50 |
}, message); |
| 51 |
} |
| 52 |
|
| 53 |
static void assertTimeoutPreemptively(Duration timeout, Executable executable, Supplier<String> messageSupplier) { |
| 54 |
assertTimeoutPreemptively(timeout, () -> { |
| 55 |
executable.execute(); |
| 56 |
return null; |
| 57 |
}, messageSupplier); |
| 58 |
} |
| 59 |
|
| 60 |
static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier) { |
| 61 |
return assertTimeoutPreemptively(timeout, supplier, null, AssertTimeoutPreemptively::createAssertionFailure); |
| 62 |
} |
| 63 |
|
| 64 |
static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier, String message) { |
| 65 |
return assertTimeoutPreemptively(timeout, supplier, message == null ? null : () -> message, |
| 66 |
AssertTimeoutPreemptively::createAssertionFailure); |
| 67 |
} |
| 68 |
|
| 69 |
static <T> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier, |
| 70 |
Supplier<String> messageSupplier) { |
| 71 |
return assertTimeoutPreemptively(timeout, supplier, messageSupplier, |
| 72 |
AssertTimeoutPreemptively::createAssertionFailure); |
| 73 |
} |
| 74 |
|
| 75 |
static <T, E extends Throwable> T assertTimeoutPreemptively(Duration timeout, ThrowingSupplier<T> supplier, |
| 76 |
Supplier<String> messageSupplier, Assertions.TimeoutFailureFactory<E> failureFactory) throws E { |
| 77 |
AtomicReference<Thread> threadReference = new AtomicReference<>(); |
| 78 |
ExecutorService executorService = Executors.newSingleThreadExecutor(new TimeoutThreadFactory()); |
| 79 |
|
| 80 |
try { |
| 81 |
Future<T> future = submitTask(supplier, threadReference, executorService); |
| 82 |
return resolveFutureAndHandleException(future, timeout, messageSupplier, threadReference::get, |
| 83 |
failureFactory); |
| 84 |
} |
| 85 |
finally { |
| 86 |
executorService.shutdownNow(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private static <T> Future<T> submitTask(ThrowingSupplier<T> supplier, AtomicReference<Thread> threadReference, |
| 91 |
ExecutorService executorService) { |
| 92 |
return executorService.submit(() -> { |
| 93 |
try { |
| 94 |
threadReference.set(Thread.currentThread()); |
| 95 |
return supplier.get(); |
| 96 |
} |
| 97 |
catch (Throwable throwable) { |
| 98 |
throw throwAsUncheckedException(throwable); |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
private static <T, E extends Throwable> T resolveFutureAndHandleException(Future<T> future, Duration timeout, |
| 104 |
Supplier<String> messageSupplier, Supplier<Thread> threadSupplier, |
| 105 |
Assertions.TimeoutFailureFactory<E> failureFactory) throws E { |
| 106 |
try { |
| 107 |
return future.get(timeout.toMillis(), TimeUnit.MILLISECONDS); |
| 108 |
} |
| 109 |
catch (TimeoutException ex) { |
| 110 |
Thread thread = threadSupplier.get(); |
| 111 |
ExecutionTimeoutException cause = null; |
| 112 |
if (thread != null) { |
| 113 |
cause = new ExecutionTimeoutException("Execution timed out in thread " + thread.getName()); |
| 114 |
cause.setStackTrace(thread.getStackTrace()); |
| 115 |
} |
| 116 |
throw failureFactory.createTimeoutFailure(timeout, messageSupplier, cause); |
| 117 |
} |
| 118 |
catch (ExecutionException ex) { |
| 119 |
throw throwAsUncheckedException(ex.getCause()); |
| 120 |
} |
| 121 |
catch (Throwable ex) { |
| 122 |
throw throwAsUncheckedException(ex); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private static AssertionFailedError createAssertionFailure(Duration timeout, Supplier<String> messageSupplier, |
| 127 |
Throwable cause) { |
| 128 |
return assertionFailure() |
| 129 |
.message(messageSupplier) |
| 130 |
.reason("execution timed out after " + timeout.toMillis() + " ms") |
| 131 |
.cause(cause) |
| 132 |
.build(); |
| 133 |
} |
| 134 |
|
| 135 |
private static class ExecutionTimeoutException extends JUnitException { |
| 136 |
|
| 137 |
private static final long serialVersionUID = 1L; |
| 138 |
|
| 139 |
ExecutionTimeoutException(String message) { |
| 140 |
super(message); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private static class TimeoutThreadFactory implements ThreadFactory { |
| 150 |
private static final AtomicInteger threadNumber = new AtomicInteger(1); |
| 151 |
|
| 152 |
public Thread newThread(Runnable r) { |
| 153 |
return new Thread(r, "junit-timeout-thread-" + threadNumber.getAndIncrement()); |
| 154 |
} |
| 155 |
} |
| 156 |
} |