|
12 | 12 | */
|
13 | 13 | package org.assertj.core.api.future;
|
14 | 14 |
|
| 15 | +import static java.lang.String.format; |
| 16 | + |
| 17 | +import java.lang.Thread.UncaughtExceptionHandler; |
15 | 18 | import java.util.concurrent.ExecutorService;
|
16 | 19 | import java.util.concurrent.Executors;
|
17 | 20 |
|
| 21 | +import java.util.concurrent.ThreadFactory; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
18 | 23 | import org.junit.jupiter.api.AfterEach;
|
19 | 24 | import org.junit.jupiter.api.BeforeEach;
|
| 25 | +import org.junit.platform.commons.logging.Logger; |
| 26 | +import org.junit.platform.commons.logging.LoggerFactory; |
20 | 27 |
|
21 | 28 | abstract class AbstractFutureTest {
|
22 |
| - |
23 | 29 | protected ExecutorService executorService;
|
| 30 | + private final ThreadFactory factory; |
| 31 | + |
| 32 | + AbstractFutureTest() { |
| 33 | + factory = new FutureTestThreadFactory(); |
| 34 | + } |
24 | 35 |
|
25 | 36 | @BeforeEach
|
26 | 37 | void beforeEach() {
|
27 |
| - executorService = Executors.newSingleThreadExecutor(); |
| 38 | + executorService = Executors.newSingleThreadExecutor(factory); |
28 | 39 | }
|
29 | 40 |
|
30 | 41 | @AfterEach
|
31 | 42 | void afterEach() {
|
32 | 43 | executorService.shutdownNow();
|
33 | 44 | }
|
| 45 | + |
| 46 | + private class FutureTestThreadFactory implements ThreadFactory, UncaughtExceptionHandler { |
| 47 | + private final Logger logger; |
| 48 | + private final AtomicInteger count; |
| 49 | + |
| 50 | + private FutureTestThreadFactory() { |
| 51 | + logger = LoggerFactory.getLogger(AbstractFutureTest.this.getClass()); |
| 52 | + count = new AtomicInteger(0); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Thread newThread(Runnable runnable) { |
| 57 | + Thread thread = new Thread(runnable); |
| 58 | + thread.setName(AbstractFutureTest.this.getClass().getName() + "." + count.incrementAndGet()); |
| 59 | + thread.setDaemon(true); |
| 60 | + thread.setPriority(Thread.MAX_PRIORITY); |
| 61 | + thread.setUncaughtExceptionHandler(this); |
| 62 | + thread.setContextClassLoader(Thread.currentThread().getContextClassLoader()); |
| 63 | + return thread; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void uncaughtException(Thread thread, Throwable ex) { |
| 68 | + logger.info( |
| 69 | + ex, |
| 70 | + () -> format("Thread %s [%s] threw an exception", thread.getName(), thread.getId()) |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
34 | 74 | }
|
0 commit comments