|
28 | 28 | import org.openqa.selenium.remote.HttpCommandExecutor;
|
29 | 29 | import org.openqa.selenium.remote.Response;
|
30 | 30 |
|
| 31 | +import java.io.Closeable; |
31 | 32 | import java.io.IOException;
|
32 | 33 | import java.net.ConnectException;
|
33 | 34 | import java.util.Map;
|
34 | 35 | import java.util.concurrent.CompletableFuture;
|
35 | 36 | import java.util.concurrent.ExecutionException;
|
| 37 | +import java.util.concurrent.ExecutorService; |
| 38 | +import java.util.concurrent.Executors; |
36 | 39 | import java.util.concurrent.TimeUnit;
|
37 | 40 | import java.util.concurrent.TimeoutException;
|
38 | 41 |
|
|
41 | 44 | * and dies with a single WebDriver session. The service will be restarted upon each new session
|
42 | 45 | * request and shutdown after each quit command.
|
43 | 46 | */
|
44 |
| -public class DriverCommandExecutor extends HttpCommandExecutor { |
| 47 | +public class DriverCommandExecutor extends HttpCommandExecutor implements Closeable { |
45 | 48 |
|
46 | 49 | private final DriverService service;
|
| 50 | + private final ExecutorService executorService = Executors.newFixedThreadPool(2, r -> { |
| 51 | + Thread thread = new Thread(r); |
| 52 | + thread.setName("Driver Command Executor"); |
| 53 | + thread.setDaemon(true); |
| 54 | + return thread; |
| 55 | + }); |
47 | 56 |
|
48 | 57 | /**
|
49 | 58 | * Creates a new DriverCommandExecutor which will communicate with the driver as configured
|
@@ -104,12 +113,12 @@ public Response execute(Command command) throws IOException {
|
104 | 113 | Throwables.throwIfUnchecked(t);
|
105 | 114 | throw new WebDriverException(t);
|
106 | 115 | }
|
107 |
| - }); |
| 116 | + }, executorService); |
108 | 117 |
|
109 | 118 | CompletableFuture<Response> processFinished = CompletableFuture.supplyAsync(() -> {
|
110 | 119 | service.process.waitFor(service.getTimeout().toMillis());
|
111 | 120 | return null;
|
112 |
| - }); |
| 121 | + }, executorService); |
113 | 122 |
|
114 | 123 | try {
|
115 | 124 | Response response = (Response) CompletableFuture.anyOf(commandComplete, processFinished)
|
@@ -152,4 +161,9 @@ public Response execute(Command command) throws IOException {
|
152 | 161 | Response invokeExecute(Command command) throws IOException {
|
153 | 162 | return super.execute(command);
|
154 | 163 | }
|
| 164 | + |
| 165 | + @Override |
| 166 | + public void close() { |
| 167 | + executorService.shutdownNow(); |
| 168 | + } |
155 | 169 | }
|
0 commit comments