Skip to content

Commit

Permalink
Use ProcessHandle.destroy to implement RealJenkinsRule.stopJenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jan 17, 2023
1 parent eb817ec commit e2d5ab5
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.recipes.LocalData;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
Expand Down Expand Up @@ -757,17 +756,14 @@ public void stopJenkins() throws Throwable {
if (proc != null) {
Process _proc = proc;
proc = null;
endpoint("exit").openStream().close();
// Do not use Process.destroy as it closes streams, losing any final output:
_proc.toHandle().destroy();

if (!_proc.waitFor(60, TimeUnit.SECONDS) ) {
System.err.println("Jenkins failed to stop within 60 seconds, attempting to kill the Jenkins process");
_proc.destroyForcibly();
throw new AssertionError("Jenkins failed to terminate within 60 seconds");
}
int exitValue = _proc.exitValue();
if (exitValue != 0) {
throw new AssertionError("nonzero exit code: " + exitValue);
}
}
}

Expand Down Expand Up @@ -961,12 +957,6 @@ public void doStep(StaplerRequest req, StaplerResponse rsp) throws Throwable {
// TODO use raw err if it seems safe enough
Init2.writeSer(rsp.getOutputStream(), new OutputPayload(object, err != null ? new ProxyException(err) : null));
}
public HttpResponse doExit(@QueryParameter String token) throws IOException {
checkToken(token);
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
return Jenkins.get().doSafeExit(null);
}
}
}

public static final class CustomJenkinsRule extends JenkinsRule implements AutoCloseable {
Expand Down

0 comments on commit e2d5ab5

Please sign in to comment.