This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git

commit 216c0000318a1f270037832d4288875fc5f1cd58
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Dec 13 08:32:39 2023 -0500

    Add DefaultExecuteResultHandler.waitFor(Duration)
    
    Deprecate DefaultExecuteResultHandler.waitFor(long)
---
 src/changes/changes.xml                            |  9 +++++----
 .../commons/exec/DefaultExecuteResultHandler.java  | 22 ++++++++++++++++++++++
 .../apache/commons/exec/DefaultExecutorTest.java   |  2 ++
 .../org/apache/commons/exec/issues/Exec49Test.java |  6 ++++--
 4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6a641b99..66642f28 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,9 +27,8 @@
     <body>
         <release version="1.4.0" date="20YY-MM-DD" description="Maintenance 
and feature Release (Java 8 or above)">
             <!-- ADD -->
-            <action dev="ggregory" type="add" due-to="Gary Gregory">
-                Add ShutdownHookProcessDestroyer.isEmpty().
-            </action>
+            <action dev="ggregory" type="add" due-to="Gary Gregory">Add 
ShutdownHookProcessDestroyer.isEmpty().</action>
+            <action dev="ggregory" type="add" due-to="Gary Gregory">Add 
DefaultExecuteResultHandler.waitFor(Duration).</action>
             <!-- FIX -->
             <action issue="EXEC-105" type="fix" date="2023-07-16" 
due-to="Dimitrios Efthymiou">
                 Fix code snippet in tutorial page.
@@ -48,7 +47,9 @@
             </action>
             <action dev="ggregory" type="fix" due-to="step-security-bot, Gary 
Gregory">
                 [StepSecurity] ci: Harden GitHub Actions #107.
-            </action>            
+            </action>
+            <!-- REMOVE -->
+            <action dev="ggregory" type="remove" due-to="Gary 
Gregory">Deprecate DefaultExecuteResultHandler.waitFor(long).</action>
             <!-- UPDATE -->
             <action dev="ggregory" type="update" due-to="Gary Gregory, 
Dependabot">
                 Bump github actions #52.
diff --git 
a/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java 
b/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
index bd3002a8..d95cf132 100644
--- a/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
+++ b/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
@@ -17,6 +17,9 @@
 
 package org.apache.commons.exec;
 
+import java.time.Duration;
+import java.time.Instant;
+
 /**
  * A default implementation of 'ExecuteResultHandler' used for asynchronous 
process handling.
  */
@@ -110,6 +113,22 @@ public class DefaultExecuteResultHandler implements 
ExecuteResultHandler {
         }
     }
 
+    /**
+     * Causes the current thread to wait, if necessary, until the process has 
terminated. This method returns immediately if the process has already 
terminated.
+     * If the process has not yet terminated, the calling thread will be 
blocked until the process exits.
+     *
+     * @param timeout the maximum time to wait
+     * @throws InterruptedException if the current thread is {@linkplain 
Thread#interrupt() interrupted} by another thread while it is waiting, then the 
wait is
+     *                              ended and an {@link InterruptedException} 
is thrown.
+     * @since 1.4.0
+     */
+    public void waitFor(final Duration timeout) throws InterruptedException {
+        final Instant until = Instant.now().plus(timeout);
+        while (!hasResult() && Instant.now().isBefore(until)) {
+            Thread.sleep(SLEEP_TIME_MS);
+        }
+    }
+
     /**
      * Causes the current thread to wait, if necessary, until the process has 
terminated. This method returns immediately if the process has already 
terminated.
      * If the process has not yet terminated, the calling thread will be 
blocked until the process exits.
@@ -117,11 +136,14 @@ public class DefaultExecuteResultHandler implements 
ExecuteResultHandler {
      * @param timeoutMillis the maximum time to wait in milliseconds
      * @throws InterruptedException if the current thread is {@linkplain 
Thread#interrupt() interrupted} by another thread while it is waiting, then the 
wait is
      *                              ended and an {@link InterruptedException} 
is thrown.
+     * @deprecated Use {@link #waitFor(Duration)}.
      */
+    @Deprecated
     public void waitFor(final long timeoutMillis) throws InterruptedException {
         final long untilMillis = System.currentTimeMillis() + timeoutMillis;
         while (!hasResult() && System.currentTimeMillis() < untilMillis) {
             Thread.sleep(SLEEP_TIME_MS);
         }
     }
+
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java 
b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
index 67340977..769b319e 100644
--- a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
+++ b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
@@ -35,6 +35,7 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -51,6 +52,7 @@ public class DefaultExecutorTest {
 
     /** Maximum time to wait (15s) */
     private static final int WAITFOR_TIMEOUT = 15000;
+    private static final Duration WAITFOR_TIMEOUT_D = 
Duration.ofMillis(WAITFOR_TIMEOUT);
 
     // Get suitable exit codes for the OS
     private static int SUCCESS_STATUS; // test script successful exit code
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java 
b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
index 38e7afd5..716a1c40 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
@@ -20,6 +20,7 @@ package org.apache.commons.exec.issues;
 import java.io.ByteArrayOutputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
+import java.time.Duration;
 
 import org.apache.commons.exec.CommandLine;
 import org.apache.commons.exec.DefaultExecuteResultHandler;
@@ -34,6 +35,7 @@ import org.junit.Test;
  */
 public class Exec49Test {
 
+    private static final Duration WAIT = Duration.ofSeconds(10);
     private final Executor exec = new DefaultExecutor();
 
     /**
@@ -69,7 +71,7 @@ public class Exec49Test {
             }
             pis.close();
 
-            handler.waitFor(10000);
+            handler.waitFor(WAIT);
             handler.getExitValue(); // will fail if process has not finished
         }
     }
@@ -107,7 +109,7 @@ public class Exec49Test {
             }
             pis.close();
 
-            handler.waitFor(10000);
+            handler.waitFor(WAIT);
             handler.getExitValue(); // will fail if process has not finished
         }
     }

Reply via email to