|
| 1 | +package org.testcontainers.utility; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.awaitility.Awaitility; |
| 5 | + |
| 6 | +import java.io.InputStream; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
| 13 | +import java.util.concurrent.locks.LockSupport; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +class DockerClientProviderStrategyUncaughtExceptionHandlerTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + void dockerClientProviderStrategyTestShouldUseDontCatchUncaughtExceptions() throws Exception { |
| 21 | + byte[] bytes; |
| 22 | + try (InputStream is = getClass() |
| 23 | + .getClassLoader() |
| 24 | + .getResourceAsStream("org/testcontainers/dockerclient/DockerClientProviderStrategy.class")) { |
| 25 | + assertThat(is).isNotNull(); |
| 26 | + bytes = is.readAllBytes(); |
| 27 | + } |
| 28 | + |
| 29 | + // Use ISO_8859_1 to preserve a stable 1:1 mapping of bytes to chars for a lightweight constant-pool substring check. |
| 30 | + String content = new String(bytes, StandardCharsets.ISO_8859_1); |
| 31 | + assertThat(content).contains("dontCatchUncaughtExceptions"); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldNotInterceptUncaughtExceptionsFromOtherThreads() throws Exception { |
| 36 | + Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); |
| 37 | + AtomicReference<Throwable> observedException = new AtomicReference<>(); |
| 38 | + CountDownLatch observedExceptionLatch = new CountDownLatch(1); |
| 39 | + Thread.UncaughtExceptionHandler expectedHandler = (t, e) -> { |
| 40 | + if (observedException.compareAndSet(null, e)) { |
| 41 | + observedExceptionLatch.countDown(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + Thread.setDefaultUncaughtExceptionHandler(expectedHandler); |
| 46 | + try { |
| 47 | + AtomicReference<Thread.UncaughtExceptionHandler> changedHandler = new AtomicReference<>(); |
| 48 | + AtomicBoolean monitorStop = new AtomicBoolean(false); |
| 49 | + Thread monitor = new Thread(() -> { |
| 50 | + while (!monitorStop.get()) { |
| 51 | + Thread.UncaughtExceptionHandler current = Thread.getDefaultUncaughtExceptionHandler(); |
| 52 | + if (current != expectedHandler) { |
| 53 | + changedHandler.compareAndSet(null, current); |
| 54 | + break; |
| 55 | + } |
| 56 | + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1)); |
| 57 | + } |
| 58 | + }, "docker-strategy-uncaught-handler-monitor"); |
| 59 | + |
| 60 | + Thread thrower = new Thread(() -> { |
| 61 | + try { |
| 62 | + Thread.sleep(100); |
| 63 | + } catch (InterruptedException ignored) { |
| 64 | + return; |
| 65 | + } |
| 66 | + throw new RuntimeException("boom"); |
| 67 | + }, "docker-strategy-uncaught-handler-thrower"); |
| 68 | + |
| 69 | + monitor.start(); |
| 70 | + thrower.start(); |
| 71 | + |
| 72 | + Awaitility |
| 73 | + .await() |
| 74 | + .dontCatchUncaughtExceptions() |
| 75 | + .pollDelay(Duration.ZERO) |
| 76 | + .pollInterval(Duration.ofMillis(25)) |
| 77 | + .atMost(Duration.ofSeconds(2)) |
| 78 | + .until(() -> { |
| 79 | + Thread.sleep(300); |
| 80 | + return true; |
| 81 | + }); |
| 82 | + |
| 83 | + monitorStop.set(true); |
| 84 | + monitor.join(TimeUnit.SECONDS.toMillis(5)); |
| 85 | + thrower.join(TimeUnit.SECONDS.toMillis(5)); |
| 86 | + |
| 87 | + assertThat(changedHandler.get()).isNull(); |
| 88 | + assertThat(observedExceptionLatch.await(5, TimeUnit.SECONDS)).isTrue(); |
| 89 | + assertThat(observedException.get()).isInstanceOf(RuntimeException.class).hasMessage("boom"); |
| 90 | + } finally { |
| 91 | + Thread.setDefaultUncaughtExceptionHandler(originalHandler); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments