From 9f3a561cce81638e6bbc3f0cb68cb9f5bcb76c9e Mon Sep 17 00:00:00 2001 From: Varun Menon Date: Mon, 23 Jun 2014 17:22:35 +0530 Subject: [PATCH] Adding a timeout to Firefox cleanup process. Fixes issue 7272 Signed-off-by: Alexei Barantsev --- .../openqa/selenium/firefox/FirefoxBinary.java | 16 +++++++++++++++- .../src/org/openqa/selenium/os/CommandLine.java | 8 ++++++++ .../src/org/openqa/selenium/os/OsProcess.java | 2 ++ .../src/org/openqa/selenium/os/UnixProcess.java | 15 +++++++++++++++ .../openqa/selenium/os/WindowsProcessGroup.java | 4 ++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/java/client/src/org/openqa/selenium/firefox/FirefoxBinary.java b/java/client/src/org/openqa/selenium/firefox/FirefoxBinary.java index 8cb1241939ade..ec0f5d8f1fcf8 100644 --- a/java/client/src/org/openqa/selenium/firefox/FirefoxBinary.java +++ b/java/client/src/org/openqa/selenium/firefox/FirefoxBinary.java @@ -207,6 +207,20 @@ public void createProfile(String profileName) throws IOException { public void waitFor() throws InterruptedException, IOException { process.waitFor(); } + + /** + * Waits for the process to execute, returning the command output taken from the profile's + * execution. + * + * @param timeout the maximum time to wait in milliseconds + * @throws InterruptedException if we are interrupted while waiting for the process to launch + * @throws IOException if there is a problem with reading the input stream of the launching + * process + */ + + public void waitFor(long timeout) throws InterruptedException, IOException { + process.waitFor(timeout); + } /** * Gets all console output of the binary. Output retrieval is non-destructive and non-blocking. @@ -225,7 +239,7 @@ public String getConsoleOutput() throws IOException { public void clean(FirefoxProfile profile, File profileDir) throws IOException { startProfile(profile, profileDir, "-silent"); try { - waitFor(); + waitFor(timeout); } catch (InterruptedException e) { process.destroy(); throw new WebDriverException(e); diff --git a/java/client/src/org/openqa/selenium/os/CommandLine.java b/java/client/src/org/openqa/selenium/os/CommandLine.java index 85b6a61822da1..43592a494a8e4 100644 --- a/java/client/src/org/openqa/selenium/os/CommandLine.java +++ b/java/client/src/org/openqa/selenium/os/CommandLine.java @@ -123,6 +123,14 @@ public void waitFor() { throw new WebDriverException(e); } } + + public void waitFor(long timeout) { + try { + process.waitFor(timeout); + } catch (InterruptedException e) { + throw new WebDriverException(e); + } + } public boolean isSuccessful() { return 0 == getExitCode(); diff --git a/java/client/src/org/openqa/selenium/os/OsProcess.java b/java/client/src/org/openqa/selenium/os/OsProcess.java index 77c4494ca6a39..bdc27daf912e2 100644 --- a/java/client/src/org/openqa/selenium/os/OsProcess.java +++ b/java/client/src/org/openqa/selenium/os/OsProcess.java @@ -35,6 +35,8 @@ interface OsProcess { void executeAsync(); void waitFor() throws InterruptedException; + + void waitFor(long timeout) throws InterruptedException; int destroy(); diff --git a/java/client/src/org/openqa/selenium/os/UnixProcess.java b/java/client/src/org/openqa/selenium/os/UnixProcess.java index e9d869f05e689..65ff569da4445 100644 --- a/java/client/src/org/openqa/selenium/os/UnixProcess.java +++ b/java/client/src/org/openqa/selenium/os/UnixProcess.java @@ -134,6 +134,21 @@ public void waitFor() throws InterruptedException { handler.waitFor(); } + public void waitFor(long timeout) throws InterruptedException { + long until = System.currentTimeMillis() + timeout; + boolean timedOut = true; + while (System.currentTimeMillis() < until) { + if(handler.hasResult()){ + timedOut = false; + break; + } + Thread.sleep(50); + } + if(timedOut){ + throw new InterruptedException(String.format("Process timed out after waiting for %d ms.",timeout) ); + } + } + public boolean isRunning() { return !handler.hasResult(); } diff --git a/java/client/src/org/openqa/selenium/os/WindowsProcessGroup.java b/java/client/src/org/openqa/selenium/os/WindowsProcessGroup.java index 9d09497242699..558aae359a1be 100644 --- a/java/client/src/org/openqa/selenium/os/WindowsProcessGroup.java +++ b/java/client/src/org/openqa/selenium/os/WindowsProcessGroup.java @@ -156,6 +156,10 @@ public void executeAsync() { public void waitFor() throws InterruptedException { // no-op } + + public void waitFor(long timeout) throws InterruptedException { + // no-op + } public int destroy() { if (!isRunning()) {