|
| 1 | +package de.cronn.testutils; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.Callable; |
| 6 | +import java.util.concurrent.ExecutorService; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.Future; |
| 9 | +import java.util.concurrent.ThreadFactory; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 12 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 13 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 14 | +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; |
| 15 | + |
| 16 | +public class ExecutorServiceExtension implements BeforeEachCallback, AfterEachCallback { |
| 17 | + |
| 18 | + private final long testTimeoutMillis; |
| 19 | + private ExecutorService executorService; |
| 20 | + private List<Future<?>> futures; |
| 21 | + |
| 22 | + public ExecutorServiceExtension(long testTimeoutMillis) { |
| 23 | + this.testTimeoutMillis = testTimeoutMillis; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void afterEach(ExtensionContext context) { |
| 28 | + ExecutorServiceUtils.shutdownOrThrow(executorService, getTestName(context), testTimeoutMillis); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void beforeEach(ExtensionContext context) { |
| 33 | + ThreadFactory threadFactory = new CustomizableThreadFactory(getTestName(context)); |
| 34 | + executorService = Executors.newCachedThreadPool(threadFactory); |
| 35 | + futures = new ArrayList<>(); |
| 36 | + } |
| 37 | + |
| 38 | + private String getTestName(ExtensionContext context) { |
| 39 | + return TestNameUtils.getTestName(context.getRequiredTestClass(), context.getRequiredTestMethod().getName()); |
| 40 | + } |
| 41 | + |
| 42 | + public Future<Void> submit(Runnable runnable) { |
| 43 | + return submit(() -> { |
| 44 | + runnable.run(); |
| 45 | + return null; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + public <T> Future<T> submit(Callable<T> callable) { |
| 50 | + Future<T> future = executorService.submit(callable); |
| 51 | + futures.add(future); |
| 52 | + return future; |
| 53 | + } |
| 54 | + |
| 55 | + public List<Future<?>> getFutures() { |
| 56 | + return futures; |
| 57 | + } |
| 58 | + |
| 59 | + public void awaitAllFutures() throws Exception { |
| 60 | + for (Future<?> future : getFutures()) { |
| 61 | + future.get(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + class TestNameUtils { |
| 66 | + |
| 67 | + private TestNameUtils() { |
| 68 | + } |
| 69 | + |
| 70 | + public static String getTestName(Class<?> aClass, String methodName) { |
| 71 | + return join(enclosingClassesUpstream(aClass), methodName); |
| 72 | + } |
| 73 | + |
| 74 | + private static String enclosingClassesUpstream(Class<?> aClass) { |
| 75 | + String classHierarchy = aClass.getSimpleName(); |
| 76 | + Class<?> enclosingClass = aClass.getEnclosingClass(); |
| 77 | + while (enclosingClass != null) { |
| 78 | + classHierarchy = join(enclosingClass.getSimpleName(), classHierarchy); |
| 79 | + enclosingClass = enclosingClass.getEnclosingClass(); |
| 80 | + } |
| 81 | + return classHierarchy; |
| 82 | + } |
| 83 | + |
| 84 | + private static String join(String element, String other) { |
| 85 | + return other.startsWith("_") ? (element + other) : (element + "_" + other); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments