Skip to content

Commit c1c9704

Browse files
committedJul 22, 2024
8336479: Provide Process.waitFor(Duration)
Reviewed-by: liach, jpai, rriggs
1 parent 7ea7730 commit c1c9704

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed
 

‎src/java.base/share/classes/java/lang/Process.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.lang.ProcessBuilder.Redirect;
3333
import java.nio.charset.Charset;
3434
import java.nio.charset.UnsupportedCharsetException;
35+
import java.time.Duration;
3536
import java.util.Objects;
3637
import java.util.concurrent.CompletableFuture;
3738
import java.util.concurrent.ForkJoinPool;
@@ -475,6 +476,35 @@ public boolean waitFor(long timeout, TimeUnit unit)
475476
return false;
476477
}
477478

479+
/**
480+
* Causes the current thread to wait, if necessary, until the
481+
* process represented by this {@code Process} object has
482+
* terminated, or the specified waiting duration elapses.
483+
*
484+
* <p>If the process has already terminated then this method returns
485+
* immediately with the value {@code true}. If the process has not
486+
* terminated and the duration is not positive, then
487+
* this method returns immediately with the value {@code false}.
488+
*
489+
* <p>The default implementation of this method polls the {@code exitValue}
490+
* to check if the process has terminated. Concrete implementations of this
491+
* class are strongly encouraged to override this method with a more
492+
* efficient implementation.
493+
*
494+
* @param duration the maximum duration to wait; if not positive,
495+
* this method returns immediately.
496+
* @return {@code true} if the process has exited and {@code false} if
497+
* the waiting duration elapsed before the process has exited.
498+
* @throws InterruptedException if the current thread is interrupted
499+
* while waiting.
500+
* @throws NullPointerException if duration is null
501+
* @since 24
502+
*/
503+
public boolean waitFor(Duration duration) throws InterruptedException {
504+
Objects.requireNonNull(duration, "duration");
505+
return waitFor(TimeUnit.NANOSECONDS.convert(duration), TimeUnit.NANOSECONDS);
506+
}
507+
478508
/**
479509
* Returns the exit value for the process.
480510
*
@@ -577,8 +607,8 @@ public boolean isAlive() {
577607

578608
/**
579609
* This is called from the default implementation of
580-
* {@code waitFor(long, TimeUnit)}, which is specified to poll
581-
* {@code exitValue()}.
610+
* {@code waitFor(long, TimeUnit)} and {@code waitFor(Duration)},
611+
* which are specified to poll {@code exitValue()}.
582612
*/
583613
private boolean hasExited() {
584614
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8336479
27+
* @summary Tests for Process.waitFor(Duration)
28+
* @library /test/lib
29+
* @run junit WaitForDuration
30+
*/
31+
32+
import java.io.IOException;
33+
import java.time.Duration;
34+
import java.util.stream.Stream;
35+
import jdk.test.lib.process.ProcessTools;
36+
37+
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.params.ParameterizedTest;
39+
import org.junit.jupiter.params.provider.Arguments;
40+
import org.junit.jupiter.params.provider.MethodSource;
41+
import static org.junit.jupiter.api.Assertions.*;
42+
43+
public class WaitForDuration {
44+
static Stream<Arguments> durations() {
45+
return Stream.of(
46+
Arguments.of(Duration.ZERO, 3_600_000, false),
47+
Arguments.of(Duration.ofSeconds(-100), 3_600_000, false),
48+
Arguments.of(Duration.ofSeconds(100), 0, true),
49+
Arguments.of(Duration.ofSeconds(Long.MAX_VALUE), 0, true), // nano overflow
50+
Arguments.of(Duration.ofSeconds(Long.MIN_VALUE), 3_600_000, false) // nano underflow
51+
);
52+
}
53+
54+
@ParameterizedTest
55+
@MethodSource("durations")
56+
void testEdgeDurations(Duration d, int sleepMillis, boolean expected)
57+
throws IOException, InterruptedException {
58+
var pb = ProcessTools.createTestJavaProcessBuilder(
59+
WaitForDuration.class.getSimpleName(), Integer.toString(sleepMillis));
60+
assertEquals(expected, pb.start().waitFor(d));
61+
}
62+
63+
@Test
64+
void testNullDuration() throws IOException, InterruptedException {
65+
var pb = ProcessTools.createTestJavaProcessBuilder(
66+
WaitForDuration.class.getSimpleName(), "0");
67+
assertThrows(NullPointerException.class, () -> pb.start().waitFor(null));
68+
}
69+
70+
public static void main(String... args) throws InterruptedException {
71+
Thread.sleep(Integer.parseInt(args[0]));
72+
}
73+
}

‎test/lib/jdk/test/lib/process/ProcessTools.java

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.security.AccessController;
4343
import java.security.PrivilegedActionException;
4444
import java.security.PrivilegedExceptionAction;
45+
import java.time.Duration;
4546
import java.util.ArrayList;
4647
import java.util.Arrays;
4748
import java.util.Collections;
@@ -965,6 +966,15 @@ public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException
965966
return rslt;
966967
}
967968

969+
@Override
970+
public boolean waitFor(Duration duration) throws InterruptedException {
971+
boolean rslt = p.waitFor(duration);
972+
if (rslt) {
973+
waitForStreams();
974+
}
975+
return rslt;
976+
}
977+
968978
private void waitForStreams() throws InterruptedException {
969979
try {
970980
stdoutTask.get();

0 commit comments

Comments
 (0)
Please sign in to comment.