From 44dc930225ae39300270d38188323ff905545038 Mon Sep 17 00:00:00 2001 From: Sankha Narayan Guria Date: Sat, 11 Jun 2016 01:10:45 +0530 Subject: [PATCH] GeckoDriverService should wait for the geckodriver to be running before returning (#2255) --- .../selenium/firefox/GeckoDriverService.java | 5 ++++- .../org/openqa/selenium/net/PortProber.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java b/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java index b2b929a720a2f..38d358d001c3e 100644 --- a/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java +++ b/java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java @@ -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; @@ -66,7 +69,7 @@ public static GeckoDriverService createDefaultService() { @Override protected void waitUntilAvailable() throws MalformedURLException { - return; + PortProber.waitForPortUp(getUrl().getPort(), 20, SECONDS); } /** diff --git a/java/client/src/org/openqa/selenium/net/PortProber.java b/java/client/src/org/openqa/selenium/net/PortProber.java index c48081f3d35b9..8a46152ef8ce0 100644 --- a/java/client/src/org/openqa/selenium/net/PortProber.java +++ b/java/client/src/org/openqa/selenium/net/PortProber.java @@ -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; @@ -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); + } + } + } }