Allow starting and stopping of test server to throw an exception (#9895)

This commit is contained in:
Lauri Tulmin 2023-11-17 17:53:48 +02:00 committed by GitHub
parent 6313391d71
commit f491250efa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 21 deletions

View File

@ -31,13 +31,12 @@ class TapestryTest extends AbstractHttpServerUsingTest<Server> {
HttpServerInstrumentationExtension.forAgent();
@Override
protected Server setupServer() {
protected Server setupServer() throws Exception {
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath(getContextPath());
Server jettyServer = new Server(port);
// set up test application
try {
webAppContext.setBaseResource(Resource.newResource("src/test/webapp"));
for (Connector connector : jettyServer.getConnectors()) {
connector.setHost("localhost");
@ -45,19 +44,13 @@ class TapestryTest extends AbstractHttpServerUsingTest<Server> {
jettyServer.setHandler(webAppContext);
jettyServer.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
return jettyServer;
}
@Override
protected void stopServer(Server server) {
try {
protected void stopServer(Server server) throws Exception {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override

View File

@ -27,9 +27,9 @@ public abstract class AbstractHttpServerUsingTest<SERVER> {
public int port;
public URI address;
protected abstract SERVER setupServer();
protected abstract SERVER setupServer() throws Exception;
protected abstract void stopServer(SERVER server);
protected abstract void stopServer(SERVER server) throws Exception;
protected final InstrumentationTestRunner testing() {
return testing;
@ -40,7 +40,11 @@ public abstract class AbstractHttpServerUsingTest<SERVER> {
address = buildAddress();
}
try {
server = setupServer();
} catch (Exception exception) {
throw new IllegalStateException("Failed to start server", exception);
}
if (server != null) {
logger.info(
getClass().getName()
@ -57,7 +61,11 @@ public abstract class AbstractHttpServerUsingTest<SERVER> {
logger.info(getClass().getName() + " can't stop null server");
return;
}
try {
stopServer(server);
} catch (Exception exception) {
throw new IllegalStateException("Failed to stop server", exception);
}
server = null;
logger.info(getClass().getName() + " http server stopped at: http://localhost:" + port + "/");
}