diff --git a/proxy/src/test/java/com/velocitypowered/proxy/util/concurrency/RecordingThreadFactoryTest.java b/proxy/src/test/java/com/velocitypowered/proxy/util/concurrency/RecordingThreadFactoryTest.java index cac1ef295..33fabfcf3 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/util/concurrency/RecordingThreadFactoryTest.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/util/concurrency/RecordingThreadFactoryTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; import static org.junit.jupiter.api.Assertions.*; @@ -33,4 +34,36 @@ class RecordingThreadFactoryTest { Thread.sleep(10); assertEquals(0, factory.size()); } + + @Test + void cleanUpAfterExceptionThrown() throws Exception { + CountDownLatch started = new CountDownLatch(1); + CountDownLatch endThread = new CountDownLatch(1); + CountDownLatch hasEnded = new CountDownLatch(1); + RecordingThreadFactory factory = new RecordingThreadFactory((ThreadFactory) r -> { + Thread t = new Thread(r); + t.setUncaughtExceptionHandler((t1, e) -> hasEnded.countDown()); + return t; + }); + factory.newThread(() -> { + started.countDown(); + assertTrue(factory.currentlyInFactory()); + assertEquals(1, factory.size()); + try { + endThread.await(); + } catch (InterruptedException e) { + fail(e); + } + throw new RuntimeException(""); + }).start(); + started.await(); + assertFalse(factory.currentlyInFactory()); + assertEquals(1, factory.size()); + endThread.countDown(); + hasEnded.await(); + + // Wait a little bit to ensure the thread got shut down + Thread.sleep(10); + assertEquals(0, factory.size()); + } } \ No newline at end of file