Merge branch '1.3.x'
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.devtools.restart;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -55,6 +57,24 @@ public class SilentExitExceptionHandlerTests {
|
||||
assertThat(testThread.getThrown().getMessage()).isEqualTo("Expected");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preventsNonZeroExitCodeWhenAllOtherThreadsAreDaemonThreads() {
|
||||
try {
|
||||
SilentExitExceptionHandler.exitCurrentThread();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
TestSilentExitExceptionHandler silentExitExceptionHandler = new TestSilentExitExceptionHandler();
|
||||
silentExitExceptionHandler.uncaughtException(Thread.currentThread(), ex);
|
||||
try {
|
||||
assertThat(silentExitExceptionHandler.nonZeroExitCodePrevented).isTrue();
|
||||
}
|
||||
finally {
|
||||
silentExitExceptionHandler.cleanUp();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static abstract class TestThread extends Thread {
|
||||
|
||||
private Throwable thrown;
|
||||
@@ -79,4 +99,58 @@ public class SilentExitExceptionHandlerTests {
|
||||
|
||||
}
|
||||
|
||||
private static class TestSilentExitExceptionHandler
|
||||
extends SilentExitExceptionHandler {
|
||||
|
||||
private boolean nonZeroExitCodePrevented;
|
||||
|
||||
private final Object monitor = new Object();
|
||||
|
||||
TestSilentExitExceptionHandler() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void preventNonZeroExitCode() {
|
||||
this.nonZeroExitCodePrevented = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Thread[] getAllThreads() {
|
||||
final CountDownLatch threadRunning = new CountDownLatch(1);
|
||||
Thread daemonThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (TestSilentExitExceptionHandler.this.monitor) {
|
||||
threadRunning.countDown();
|
||||
try {
|
||||
TestSilentExitExceptionHandler.this.monitor.wait();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
daemonThread.setDaemon(true);
|
||||
daemonThread.start();
|
||||
try {
|
||||
threadRunning.await();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return new Thread[] { Thread.currentThread(), daemonThread };
|
||||
}
|
||||
|
||||
private void cleanUp() {
|
||||
synchronized (this.monitor) {
|
||||
this.monitor.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user