Skip to content

Commit

Permalink
Merge pull request #45 from HubSpot/handle_empty_Targets
Browse files Browse the repository at this point in the history
Handle empty session targets
  • Loading branch information
ssalinas authored Jan 19, 2023
2 parents 888528a + 55ea963 commit cce1510
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ public class ChromeDevToolsClient implements Closeable {
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
private final long actionTimeoutMillis;
private final boolean defaultStartNewTarget;

private ChromeDevToolsClient(ObjectMapper objectMapper, ExecutorService executorService, HttpClient httpClient, long actionTimeoutMillis, long sessionConnectTimeoutMillis) {
private ChromeDevToolsClient(ObjectMapper objectMapper, ExecutorService executorService, HttpClient httpClient, long actionTimeoutMillis, long sessionConnectTimeoutMillis, boolean defaultStartNewTarget) {
this.executorService = executorService;
this.objectMapper = objectMapper;
this.httpClient = httpClient;
this.actionTimeoutMillis = actionTimeoutMillis;
this.defaultStartNewTarget = defaultStartNewTarget;
this.httpRetryer = RetryerBuilder.<TargetID>newBuilder()
.retryIfExceptionOfType(ChromeDevToolsException.class)
.retryIfExceptionOfType(HttpRuntimeException.class)
Expand Down Expand Up @@ -78,6 +80,9 @@ public void close() {
}

private TargetID getFirstAvailableTargetId(String host, int port) {
if (defaultStartNewTarget) {
return startNewTarget(host, port);
}
String url = String.format("http://%s:%d/json/list", host, port);
HttpRequest httpRequest = HttpRequest.newBuilder()
.setUrl(url)
Expand All @@ -90,26 +95,48 @@ private TargetID getFirstAvailableTargetId(String host, int port) {
throw new ChromeDevToolsException("Unable to find available chrome session info.");
}

List<ChromeSessionInfo> sessions = response.getAs(new TypeReference<List<ChromeSessionInfo>>() {});
List<ChromeSessionInfo> sessions = response.getAs(new TypeReference<>() {});
if (sessions.size() == 0) {
throw new ChromeDevToolsException("No chrome sessions available.");
return startNewTarget(host, port);
}
return new TargetID(sessions.get(0).getId());
}

private TargetID startNewTarget(String host, int port) {
String url = String.format("http://%s:%d/json/new", host, port);
HttpRequest httpRequest = HttpRequest.newBuilder()
.setUrl(url)
.setMethod(Method.PUT)
.build();
HttpResponse response = httpClient.execute(httpRequest);

if (response.isError()) {
throw new ChromeDevToolsException("Unable to find available chrome session info.");
}

List<ChromeSessionInfo> sessions = response.getAs(new TypeReference<>() {});
if (sessions.size() == 0) {
throw new ChromeDevToolsException("Unable to create new websocket target");
}

return new TargetID(sessions.get(0).getId());
}

public static class Builder {
private ExecutorService executorService;
private ObjectMapper objectMapper;
private HttpClient httpClient;
private long actionTimeoutMillis;
private long sessionConnectTimeoutMillis;
private boolean defaultStartNewTarget;

public Builder() {
this.executorService = ChromeDevToolsClientDefaults.DEFAULT_EXECUTOR_SERVICE;
this.objectMapper = ChromeDevToolsClientDefaults.DEFAULT_OBJECT_MAPPER;
this.httpClient = ChromeDevToolsClientDefaults.DEFAULT_HTTP_CLIENT;
this.actionTimeoutMillis = ChromeDevToolsClientDefaults.DEFAULT_CHROME_ACTION_TIMEOUT_MILLIS;
this.sessionConnectTimeoutMillis = ChromeDevToolsClientDefaults.DEFAULT_HTTP_CONNECTION_RETRY_TIMEOUT_MILLIS;
this.defaultStartNewTarget = ChromeDevToolsClientDefaults.DEFAULT_START_NEW_TARGET;
}

public ChromeDevToolsClient.Builder setExecutorService(ExecutorService executorService) {
Expand Down Expand Up @@ -143,8 +170,13 @@ public Builder setSessionConnectTimeoutMillis(long sessionConnectTimeoutMillis)
return this;
}

public Builder setDefaultStartNewTarget(boolean defaultStartNewTarget) {
this.defaultStartNewTarget = defaultStartNewTarget;
return this;
}

public ChromeDevToolsClient build() {
return new ChromeDevToolsClient(objectMapper, executorService, httpClient, actionTimeoutMillis, sessionConnectTimeoutMillis);
return new ChromeDevToolsClient(objectMapper, executorService, httpClient, actionTimeoutMillis, sessionConnectTimeoutMillis, defaultStartNewTarget);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public class ChromeDevToolsClientDefaults {
public static final HttpClient DEFAULT_HTTP_CLIENT = new NingHttpClient();
public static final int DEFAULT_CHROME_ACTION_TIMEOUT_MILLIS = 60 * 1000;
public static final int DEFAULT_HTTP_CONNECTION_RETRY_TIMEOUT_MILLIS = 5 * 1000;
public static final boolean DEFAULT_START_NEW_TARGET = false;
}

0 comments on commit cce1510

Please sign in to comment.