13
0
geforkt von Mirrors/Velocity

Test logic when exception is thrown in thread

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-09-03 23:30:18 -04:00
Ursprung 3b5d20e62d
Commit c30cc66461

Datei anzeigen

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -33,4 +34,36 @@ class RecordingThreadFactoryTest {
Thread.sleep(10); Thread.sleep(10);
assertEquals(0, factory.size()); 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());
}
} }