Skip to content

Commit f491250

Browse files
authored
Allow starting and stopping of test server to throw an exception (#9895)
1 parent 6313391 commit f491250

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,26 @@ class TapestryTest extends AbstractHttpServerUsingTest<Server> {
3131
HttpServerInstrumentationExtension.forAgent();
3232

3333
@Override
34-
protected Server setupServer() {
34+
protected Server setupServer() throws Exception {
3535
WebAppContext webAppContext = new WebAppContext();
3636
webAppContext.setContextPath(getContextPath());
3737
Server jettyServer = new Server(port);
3838

3939
// set up test application
40-
try {
41-
webAppContext.setBaseResource(Resource.newResource("src/test/webapp"));
42-
for (Connector connector : jettyServer.getConnectors()) {
43-
connector.setHost("localhost");
44-
}
45-
46-
jettyServer.setHandler(webAppContext);
47-
jettyServer.start();
48-
} catch (Exception e) {
49-
throw new RuntimeException(e);
40+
webAppContext.setBaseResource(Resource.newResource("src/test/webapp"));
41+
for (Connector connector : jettyServer.getConnectors()) {
42+
connector.setHost("localhost");
5043
}
44+
45+
jettyServer.setHandler(webAppContext);
46+
jettyServer.start();
47+
5148
return jettyServer;
5249
}
5350

5451
@Override
55-
protected void stopServer(Server server) {
56-
try {
57-
server.stop();
58-
} catch (Exception e) {
59-
throw new RuntimeException(e);
60-
}
52+
protected void stopServer(Server server) throws Exception {
53+
server.stop();
6154
}
6255

6356
@Override

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpServerUsingTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public abstract class AbstractHttpServerUsingTest<SERVER> {
2727
public int port;
2828
public URI address;
2929

30-
protected abstract SERVER setupServer();
30+
protected abstract SERVER setupServer() throws Exception;
3131

32-
protected abstract void stopServer(SERVER server);
32+
protected abstract void stopServer(SERVER server) throws Exception;
3333

3434
protected final InstrumentationTestRunner testing() {
3535
return testing;
@@ -40,7 +40,11 @@ protected void startServer() {
4040
address = buildAddress();
4141
}
4242

43-
server = setupServer();
43+
try {
44+
server = setupServer();
45+
} catch (Exception exception) {
46+
throw new IllegalStateException("Failed to start server", exception);
47+
}
4448
if (server != null) {
4549
logger.info(
4650
getClass().getName()
@@ -57,7 +61,11 @@ protected void cleanupServer() {
5761
logger.info(getClass().getName() + " can't stop null server");
5862
return;
5963
}
60-
stopServer(server);
64+
try {
65+
stopServer(server);
66+
} catch (Exception exception) {
67+
throw new IllegalStateException("Failed to stop server", exception);
68+
}
6169
server = null;
6270
logger.info(getClass().getName() + " http server stopped at: http://localhost:" + port + "/");
6371
}

0 commit comments

Comments
 (0)