Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #8088 Add STOP.EXIT System property to configure ShutdownMonitor.exitVm #8089

Merged
merged 3 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,7 @@
import java.util.function.Predicate;

import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.Destroyable;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
Expand Down Expand Up @@ -90,7 +91,7 @@ public static boolean isRegistered(LifeCycle lifeCycle)
private final String host;
private int port;
private String key;
private boolean exitVm;
private boolean exitVm = true;
private boolean alive;

/**
Expand All @@ -107,7 +108,8 @@ private ShutdownMonitor()
this.host = System.getProperty("STOP.HOST", "127.0.0.1");
this.port = Integer.getInteger("STOP.PORT", -1);
this.key = System.getProperty("STOP.KEY", null);
this.exitVm = true;
//only change the default exitVm setting if STOP.EXIT is explicitly set
this.exitVm = Boolean.valueOf(System.getProperty("STOP.EXIT", "true"));
}

private void addLifeCycles(LifeCycle... lifeCycles)
Expand Down Expand Up @@ -308,6 +310,8 @@ private ServerSocket listen()
// establish the port and key that are in use
debug("STOP.PORT=%d", port);
debug("STOP.KEY=%s", key);
//also show if we're exiting the jvm or not
debug("STOP.EXIT=%b", exitVm);
}
}

Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -120,6 +121,80 @@ private void testStartStop(boolean reusePort) throws Exception
assertTrue(!monitor.isAlive());
}

@Test
public void testNoExitSystemProperty() throws Exception
{
System.setProperty("STOP.EXIT", "false");
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
monitor.setPort(0);
assertFalse(monitor.isExitVm());
monitor.start();

try (CloseableServer server = new CloseableServer())
{
server.setStopAtShutdown(true);
server.start();

//shouldn't be registered for shutdown on jvm
assertTrue(ShutdownThread.isRegistered(server));
assertTrue(ShutdownMonitor.isRegistered(server));

String key = monitor.getKey();
int port = monitor.getPort();

stop("stop", port, key, true);
monitor.await();

assertTrue(!monitor.isAlive());
assertTrue(server.stopped);
assertTrue(!server.destroyed);
assertTrue(!ShutdownThread.isRegistered(server));
assertTrue(!ShutdownMonitor.isRegistered(server));
}
}

/*
* Disable these config tests because ShutdownMonitor is a
* static singleton that cannot be unset, and thus would
* need each of these methods executed it its own jvm -
* current surefire settings only fork for a single test
* class.
*
* Undisable to test individually as needed.
*/
@Disabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Perhaps these exit VM tests should be in test-distribution ?

@Test
public void testExitVmDefault() throws Exception
{
//Test that the default is to exit
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
monitor.setPort(0);
assertTrue(monitor.isExitVm());
}

@Disabled
@Test
public void testExitVmTrue() throws Exception
{
//Test setting exit true
System.setProperty("STOP.EXIT", "true");
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
monitor.setPort(0);
assertTrue(monitor.isExitVm());
}

@Disabled
@Test
public void testExitVmFalse() throws Exception
{
//Test setting exit false
System.setProperty("STOP.EXIT", "false");
ShutdownMonitor monitor = ShutdownMonitor.getInstance();
monitor.setPort(0);
assertFalse(monitor.isExitVm());
}

@Disabled
@Test
public void testForceStopCommand() throws Exception
{
Expand Down