Skip to content

Commit

Permalink
Cleanup error handling in JenkinsRule.java
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhyudayaSharma committed Jun 13, 2019
1 parent d591189 commit 998d75d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
15 changes: 7 additions & 8 deletions src/main/java/jenkins/benchmark/jmh/JmhBenchmarkState.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.annotation.CheckForNull;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
import java.util.logging.Level;
Expand Down Expand Up @@ -87,7 +88,7 @@ public final void terminateJenkins() {

private void launchInstance() throws Exception {
ImmutablePair<Server, ServletContext> results = JenkinsRule._createWebServer(contextPath, localPort::setValue,
this::getJenkinsURL, getClass().getClassLoader(), JenkinsRule::_configureUserRealm);
getClass().getClassLoader(), JenkinsRule::_configureUserRealm);

server = results.left;
ServletContext webServer = results.right;
Expand All @@ -97,15 +98,13 @@ private void launchInstance() throws Exception {
JenkinsRule._configureUpdateCenter(jenkins);
jenkins.getActions().add(this);

Objects.requireNonNull(JenkinsLocationConfiguration.get()).setUrl(Objects.requireNonNull(getJenkinsURL()).toString());
String url = Objects.requireNonNull(getJenkinsURL()).toString();
Objects.requireNonNull(JenkinsLocationConfiguration.get()).setUrl(url);
LOGGER.log(Level.INFO, "Running on {0}", url);
}

private URL getJenkinsURL() {
try {
return new URL("http://localhost:" + localPort.getValue() + contextPath + "/");
} catch (Exception e) {
return null;
}
private URL getJenkinsURL() throws MalformedURLException {
return new URL("http://localhost:" + localPort.getValue() + contextPath + "/");
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,13 @@ public void after() throws Exception {
* @since TODO
*/
public static void _stopJenkins(Server server, List<LenientRunnable> tearDowns, Jenkins jenkins) {
final RuntimeException exception = new RuntimeException();

jettyLevel(Level.WARNING);
try {
server.stop();
} catch (Exception e) {
// ignore
exception.addSuppressed(e);
} finally {
jettyLevel(Level.INFO);
}
Expand All @@ -550,7 +552,7 @@ public static void _stopJenkins(Server server, List<LenientRunnable> tearDowns,
try {
r.run();
} catch (Exception e) {
// ignore
exception.addSuppressed(e);
}
}
}
Expand All @@ -559,6 +561,10 @@ public static void _stopJenkins(Server server, List<LenientRunnable> tearDowns,
jenkins.cleanUp();
ExtensionList.clearLegacyInstances();
DescriptorExtensionList.clearLegacyInstances();

if (exception.getSuppressed().length > 0) {
throw exception;
}
}

private static void jettyLevel(Level level) {
Expand Down Expand Up @@ -708,15 +714,10 @@ public File getWebAppRoot() throws Exception {
* that we need for testing.
*/
protected ServletContext createWebServer() throws Exception {
ImmutablePair<Server, ServletContext> results = _createWebServer(contextPath, (x) -> localPort = x, () -> {
try {
return getURL();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unable to get Url", e);
return null;
}
}, getClass().getClassLoader(), this::configureUserRealm);
ImmutablePair<Server, ServletContext> results = _createWebServer(contextPath,
(x) -> localPort = x, getClass().getClassLoader(), this::configureUserRealm);
server = results.left;
LOGGER.log(Level.INFO, "Running on {0}", getURL());
return results.right;
}

Expand All @@ -725,14 +726,13 @@ protected ServletContext createWebServer() throws Exception {
*
* @param contextPath the context path at which to put Jenkins
* @param portSetter the port on which the server runs will be set using this function
* @param urlGetter returns the URL after the port has been set using portSetter
* @param classLoader the class loader for the {@link WebAppContext}
* @param loginServiceSupplier configures the {@link LoginService} for the instance
* @return ImmutablePair consisting of the {@link Server} and the {@link ServletContext}
* @since TODO
*/
public static ImmutablePair<Server, ServletContext> _createWebServer(String contextPath, Consumer<Integer> portSetter,
Supplier<URL> urlGetter, ClassLoader classLoader,
ClassLoader classLoader,
Supplier<LoginService> loginServiceSupplier)
throws Exception {
Server server = new Server(new ThreadPoolImpl(new ThreadPoolExecutor(10, 10, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
Expand Down Expand Up @@ -764,7 +764,6 @@ public Thread newThread(Runnable r) {
server.start();

portSetter.accept(connector.getLocalPort());
LOGGER.log(Level.INFO, "Running on {0}", urlGetter.get());

ServletContext servletContext = context.getServletContext();
return new ImmutablePair<>(server, servletContext);
Expand Down

0 comments on commit 998d75d

Please sign in to comment.