Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a430110

Browse files
committed
destroy the server if bind fails
1 parent 9b70f8e commit a430110

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/test/java/com/gargoylesoftware/htmlunit/HttpWebConnectionProxyTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public void setup() throws Exception {
4545
WebDriverTestCase.stopWebServers();
4646
startWebServer("src/test/resources/testfiles/noproxyroot/");
4747

48-
proxyWebServer_ = createWebServer(PORT_PROXY_SERVER,
48+
final Server proxyWebServer = createWebServer(PORT_PROXY_SERVER,
4949
"src/test/resources/testfiles/proxyroot/", null, null, null);
50-
WebServerTestCase.tryStart(PORT_PROXY_SERVER, proxyWebServer_);
50+
WebServerTestCase.tryStart(PORT_PROXY_SERVER, proxyWebServer);
51+
proxyWebServer_ = proxyWebServer;
5152

5253
final WebClient webClient = getWebClient();
5354

src/test/java/com/gargoylesoftware/htmlunit/WebServerTestCase.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected void startWebServer(final String resourceBase) throws Exception {
7878
if (server_ != null) {
7979
throw new IllegalStateException("startWebServer() can not be called twice");
8080
}
81-
server_ = buildServer(PORT);
81+
final Server server = buildServer(PORT);
8282

8383
final WebAppContext context = new WebAppContext();
8484
context.setContextPath("/");
@@ -92,10 +92,11 @@ protected void startWebServer(final String resourceBase) throws Exception {
9292

9393
final HandlerList handlers = new HandlerList();
9494
handlers.setHandlers(new Handler[]{resourceHandler, context});
95-
server_.setHandler(handlers);
96-
server_.setHandler(resourceHandler);
95+
server.setHandler(handlers);
96+
server.setHandler(resourceHandler);
9797

98-
tryStart(PORT, server_);
98+
tryStart(PORT, server);
99+
server_ = server;
99100
}
100101

101102
/**
@@ -202,7 +203,7 @@ protected void startWebServer(final String resourceBase, final String[] classpat
202203
if (server_ != null) {
203204
throw new IllegalStateException("startWebServer() can not be called twice");
204205
}
205-
server_ = buildServer(PORT);
206+
final Server server = buildServer(PORT);
206207

207208
final WebAppContext context = new WebAppContext();
208209
context.setContextPath("/");
@@ -220,9 +221,10 @@ protected void startWebServer(final String resourceBase, final String[] classpat
220221
}
221222
}
222223
context.setClassLoader(loader);
223-
server_.setHandler(context);
224+
server.setHandler(context);
224225

225-
tryStart(PORT, server_);
226+
tryStart(PORT, server);
227+
server_ = server;
226228
}
227229

228230
/**
@@ -378,6 +380,10 @@ public static void tryStart(final int port, final Server server) throws Exceptio
378380
}
379381
catch (final BindException e) {
380382
if (System.currentTimeMillis() > maxWait) {
383+
// destroy the server to free all associated resources
384+
server.stop();
385+
server.destroy();
386+
381387
throw (BindException) new BindException("Port " + port + " is already in use").initCause(e);
382388
}
383389
Thread.sleep(200);

0 commit comments

Comments
 (0)