Skip to content

Commit 7094fd6

Browse files
committed
fixes
1 parent 5c05daf commit 7094fd6

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/main/java/com/browserstack/local/LocalBinary.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,23 @@ class LocalBinary {
4747
LocalBinary(String path, String key) throws LocalException {
4848
this.key = key;
4949
initialize();
50-
if (path != "") {
51-
getBinaryOnPath(path);
52-
} else {
53-
getBinary();
50+
downloadAndVerifyBinary(path);
51+
}
52+
53+
private void downloadAndVerifyBinary(String path) throws LocalException {
54+
try {
55+
if (path != "") {
56+
getBinaryOnPath(path);
57+
} else {
58+
getBinary();
59+
}
60+
checkBinary();
61+
} catch (Throwable e) {
62+
if (fallbackEnabled) throw e;
63+
fallbackEnabled = true;
64+
downloadFailureThrowable = e;
65+
downloadAndVerifyBinary(path);
5466
}
55-
checkBinary();
5667
}
5768

5869
private void initialize() throws LocalException {
@@ -181,6 +192,11 @@ private boolean makePath(String path) {
181192
}
182193

183194
private void fetchSourceUrl() {
195+
if ((!fallbackEnabled && sourceUrl) || (fallbackEnabled && !downloadFailureThrowable)) {
196+
/* Retry because binary (from any of the endpoints) validation failed */
197+
return;
198+
}
199+
184200
URL url = new URL("https://local.browserstack.com/binary/api/v1/endpoint");
185201
URLConnection connection = url.openConnection();
186202

@@ -207,6 +223,7 @@ private void fetchSourceUrl() {
207223
String responseBody = response.toString();
208224
JSONObject json = new JSONObject(responseBody);
209225
this.sourceUrl = json.getJSONObject("data").getString("endpoint");
226+
if(fallbackEnabled) downloadFailureThrowable = null;
210227
}
211228
}
212229

@@ -227,15 +244,8 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
227244
URL url = new URL(sourceUrl + '/' + binaryFileName);
228245

229246
File f = new File(source);
230-
try {
231-
newCopyToFile(url, f);
232-
} catch (IOException e) {
233-
if (fallbackEnabled) throw e;
234-
/* Binary download failed due to a server error */
235-
fallbackEnabled = true;
236-
downloadFailureThrowable = e;
237-
downloadBinary(destParentDir, custom);
238-
}
247+
newCopyToFile(url, f);
248+
239249
changePermissions(binaryPath);
240250
} catch (Throwable e) {
241251
throw new LocalException("Error trying to download BrowserStackLocal binary: " + e.getMessage());

0 commit comments

Comments
 (0)