Skip to content

Commit

Permalink
Fix error that causes the build to fail if Appium is not yet able to …
Browse files Browse the repository at this point in the history
…respond to network requests when we perform our initial status checks.
  • Loading branch information
Ardesco committed Feb 17, 2017
1 parent 48587ed commit 7a2497b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/main/java/com/lazerycode/appium/utility/UtilityFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;

public class UtilityFunctions {

public static void waitForAppiumToStart(int appiumStartupTicks, String appiumIpAddress, String appiumPort) throws IOException, InterruptedException, MojoExecutionException {
for (int attempts = 0; attempts < appiumStartupTicks; attempts++) {
Thread.sleep(500);
Expand All @@ -26,15 +28,19 @@ public static void checkFileExists(File someFile) throws MojoExecutionException
}
}

private static int getAppiumStatus(String appiumIpAddress, String appiumPort) throws IOException {

protected static int getAppiumStatus(String appiumIpAddress, String appiumPort) throws IOException {
OkHttpClient client = new OkHttpClient();
URL appiumURL = new URL("http://" + appiumIpAddress + ":" + appiumPort + "/wd/hub/status");

Request request = new Request.Builder()
.url(appiumURL)
.build();
try {
Request request = new Request.Builder()
.url(appiumURL)
.build();

return client.newCall(request).execute().code();
return client.newCall(request).execute().code();
} catch (ConnectException ex) {
//Appium not yet responding
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;

import static com.lazerycode.appium.utility.UtilityFunctions.checkFileExists;
import static com.lazerycode.appium.utility.UtilityFunctions.getAppiumStatus;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class UtilityFunctionsTest {

Expand All @@ -22,4 +28,17 @@ public void checkFileExistsReturnsCorrectResultForFileThatExists() throws Except
public void checkFileExistsReturnsCorrectResultForFileThatDoesNotExist() throws Exception {
checkFileExists(new File("/foo/bar/invalid"));
}

@Test
public void checkThatAStatusIsReturnedEvenIfServerConnectIsImpossible() throws Exception {
int appiumStatus = getAppiumStatus("0.0.0.0", findRandomOpenLocalPort());

assertThat(appiumStatus, is(equalTo(0)));
}

private String findRandomOpenLocalPort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
return Integer.toString(socket.getLocalPort());
}
}
}

0 comments on commit 7a2497b

Please sign in to comment.