Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 65A4FD6B3 for ; Mon, 22 Oct 2012 19:14:54 +0000 (UTC) Received: (qmail 73296 invoked by uid 500); 22 Oct 2012 19:14:54 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 73231 invoked by uid 500); 22 Oct 2012 19:14:54 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 73220 invoked by uid 99); 22 Oct 2012 19:14:54 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Oct 2012 19:14:54 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Oct 2012 19:14:52 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 92CE123888E4 for ; Mon, 22 Oct 2012 19:14:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1401016 - in /commons/proper/exec/trunk/src: changes/changes.xml main/java/org/apache/commons/exec/Watchdog.java test/java/org/apache/commons/exec/DefaultExecutorTest.java Date: Mon, 22 Oct 2012 19:14:09 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121022191409.92CE123888E4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ggregory Date: Mon Oct 22 19:14:08 2012 New Revision: 1401016 URL: http://svn.apache.org/viewvc?rev=1401016&view=rev Log: [EXEC-68] Watchdog kills process immediately if timeout is too large. Modified: commons/proper/exec/trunk/src/changes/changes.xml commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Modified: commons/proper/exec/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/changes/changes.xml?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/changes/changes.xml (original) +++ commons/proper/exec/trunk/src/changes/changes.xml Mon Oct 22 19:14:08 2012 @@ -24,6 +24,9 @@ + + Watchdog kills process immediately if timeout is too large. + Applied the patch from Nickolay Martinov but the timeout disguises the fact that the process might be still runnung - therefore added a sanity check in Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java (original) +++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java Mon Oct 22 19:14:08 2012 @@ -69,25 +69,25 @@ public class Watchdog implements Runnabl } public void run() { - final long until = System.currentTimeMillis() + timeout; - boolean isWaiting; - synchronized (this) { - long now = System.currentTimeMillis(); - isWaiting = until > now; - while (!stopped && isWaiting) { - try { - wait(until - now); - } catch (InterruptedException e) { - } - now = System.currentTimeMillis(); - isWaiting = until > now; - } - } + final long startTime = System.currentTimeMillis(); + boolean isWaiting; + synchronized (this) { + long timeLeft = timeout - (System.currentTimeMillis() - startTime); + isWaiting = timeLeft > 0; + while (!stopped && isWaiting) { + try { + wait(timeLeft); + } catch (InterruptedException e) { + } + timeLeft = timeout - (System.currentTimeMillis() - startTime); + isWaiting = timeLeft > 0; + } + } - // notify the listeners outside of the synchronized block (see EXEC-60) - if (!isWaiting) { - fireTimeoutOccured(); - } - } + // notify the listeners outside of the synchronized block (see EXEC-60) + if (!isWaiting) { + fireTimeoutOccured(); + } + } } Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java (original) +++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Mon Oct 22 19:14:08 2012 @@ -327,6 +327,31 @@ public class DefaultExecutorTest extends } /** + * [EXEC-68] Synchronously starts a short script with a Watchdog attached with an extremely large timeout. Checks + * to see if the script terminated naturally or if it was killed by the Watchdog. Fail if killed by Watchdog. + * + * @throws Exception + * the test failed + */ + public void testExecuteWatchdogVeryLongTimeout() throws Exception { + long timeout = Long.MAX_VALUE; + + CommandLine cl = new CommandLine(testScript); + DefaultExecutor executor = new DefaultExecutor(); + executor.setWorkingDirectory(new File(".")); + ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); + executor.setWatchdog(watchdog); + + try { + executor.execute(cl); + } catch (ExecuteException e) { + assertFalse("Process should exit normally, not be killed by watchdog", watchdog.killedProcess()); + // If the Watchdog did not kill it, something else went wrong. + throw e; + } + } + + /** * Try to start an non-existing application which should result * in an exception. *