Skip to content

Commit

Permalink
GeckoDriverService should wait for the geckodriver to be running befo…
Browse files Browse the repository at this point in the history
…re returning (#2255)
  • Loading branch information
ngsankha authored and lukeis committed Jun 10, 2016
1 parent 195f9bd commit 44dc930
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.openqa.selenium.firefox;

import static java.util.concurrent.TimeUnit.SECONDS;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.firefox.internal.Executable;
import org.openqa.selenium.remote.service.DriverService;

Expand Down Expand Up @@ -66,7 +69,7 @@ public static GeckoDriverService createDefaultService() {

@Override
protected void waitUntilAvailable() throws MalformedURLException {
return;
PortProber.waitForPortUp(getUrl().getPort(), 20, SECONDS);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions java/client/src/org/openqa/selenium/net/PortProber.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.SocketTimeoutException;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -156,4 +157,22 @@ public static boolean pollPort(int port, int timeout, TimeUnit unit) {

return false;
}

public static void waitForPortUp(int port, int timeout, TimeUnit unit) {
long end = System.currentTimeMillis() + unit.toMillis(timeout);
while (System.currentTimeMillis() < end) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost", port), 1000);
socket.close();
return;
} catch (ConnectException e) {
// Ignore this
} catch (SocketTimeoutException e) {
// Ignore this
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit 44dc930

Please sign in to comment.