Skip to content

Commit 8430955

Browse files
authored
Add shutdown of Netty to driver server and unit test code
2 parents 6ddc629 + 14f1c8a commit 8430955

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.nordstrom.ui-tools</groupId>
77
<artifactId>htmlunit-remote</artifactId>
8-
<version>4.29.1-SNAPSHOT</version>
8+
<version>4.30.1-SNAPSHOT</version>
99

1010
<name>htmlunit-remote</name>
1111
<description>This is the remote wrapper for HtmlUnitDriver</description>
@@ -34,6 +34,7 @@
3434
<maven.compiler.target>11</maven.compiler.target>
3535
<selenium.version>4.30.0</selenium.version>
3636
<htmlunit.version>4.30.0</htmlunit.version>
37+
<junit.version>4.13.2</junit.version>
3738
<checkstyle.version>10.15.0</checkstyle.version>
3839
<spotbugs.version>4.8.4</spotbugs.version>
3940
<dependencycheck.version>9.1.0</dependencycheck.version>
@@ -91,7 +92,7 @@
9192
<dependency>
9293
<groupId>junit</groupId>
9394
<artifactId>junit</artifactId>
94-
<version>4.13.2</version>
95+
<version>${junit.version}</version>
9596
<scope>test</scope>
9697
</dependency>
9798
</dependencies>

src/main/java/org/openqa/selenium/htmlunit/remote/HtmlUnitDriverServer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class HtmlUnitDriverServer extends NettyServer {
8282
* @param options {@link BaseServerOptions} object
8383
*/
8484
public HtmlUnitDriverServer(BaseServerOptions options) {
85-
super(options, createHandlers().httpHandler);
85+
super(options, HANDLERS.httpHandler);
8686
}
8787

8888
private static final Type MAP_OF_LONGS = new TypeToken<Map<String, Long>>() {}.getType();
@@ -92,10 +92,20 @@ public HtmlUnitDriverServer(BaseServerOptions options) {
9292
private static final Type MAP_OF_ACTIONS = new TypeToken<Map<String, ActionsCoercer>>() {}.getType();
9393
private static final Type MAP_OF_COOKIES = new TypeToken<Map<String, CookieCoercer>>() {}.getType();
9494

95+
private static final Handlers HANDLERS = createHandlers();
9596
private static final Logger LOG = Logger.getLogger(HtmlUnitDriverServer.class.getName());
96-
9797
private static Map<String, HtmlUnitDriver> driverMap = new ConcurrentHashMap<>();
9898

99+
static {
100+
Runtime.getRuntime().addShutdownHook(new Thread() {
101+
@Override
102+
public void run() {
103+
HANDLERS.close();
104+
LOG.info("HtmlUnitDriverServer handlers have closed");
105+
}
106+
});
107+
}
108+
99109
/**
100110
* Define the handlers for the routes supported by this server.
101111
*
@@ -386,7 +396,6 @@ public HttpResponse execute(final HttpRequest req) throws UncheckedIOException {
386396
public HttpResponse execute(final HttpRequest req) throws UncheckedIOException {
387397
return releaseActions(sessionIdFrom(params));
388398
}
389-
390399
}),
391400
post("/session/{sessionId}/alert/dismiss").to(params -> new HttpHandler() {
392401
@Override
@@ -422,7 +431,7 @@ public HttpResponse execute(final HttpRequest req) throws UncheckedIOException {
422431
) {
423432
@Override
424433
public void close() {
425-
// TODO: Add implementation
434+
// nothing to do here
426435
}
427436
};
428437
}

src/test/java/org/openqa/selenium/htmlunit/WebDriverTestCase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,23 @@ public abstract class WebDriverTestCase extends WebTestCase {
5959
private static final Executor EXECUTOR_POOL = Executors.newFixedThreadPool(4);
6060

6161
private static class ServerHolder {
62-
private static final Server<?> INSTANCE;
62+
private static final Handlers HANDLERS = createHandlers();
63+
private static final Server<?> INSTANCE = new NettyServer(defaultOptions(), HANDLERS.httpHandler);
6364

6465
static {
65-
INSTANCE = new NettyServer(defaultOptions(), createHandlers().httpHandler);
6666
INSTANCE.start();
6767

6868
Runtime.getRuntime().addShutdownHook(
6969
new Thread() {
7070
@Override
7171
public void run() {
7272
INSTANCE.stop();
73+
HANDLERS.close();
74+
LOG.info("Example site web server has stopped");
7375
}
7476
});
77+
78+
LOG.info("Example site web server has started");
7579
}
7680

7781
private static Handlers createHandlers() {

0 commit comments

Comments
 (0)