test/java/lang/ProcessBuilder/Basic.java
Print this page
@@ -36,10 +36,11 @@
import static java.lang.ProcessBuilder.Redirect.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import java.security.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.getenv;
import static java.lang.System.out;
@@ -2088,12 +2089,91 @@
// Restore "normal" state without a security manager
policy.setPermissions(new RuntimePermission("setSecurityManager"));
System.setSecurityManager(null);
+ //----------------------------------------------------------------
+ // Check that Process.isAlive() &
+ // Process.waitFor(0, TimeUnit.MILLISECONDS) work as expected.
+ //----------------------------------------------------------------
+ try {
+ List<String> childArgs = new ArrayList<String>(javaChildArgs);
+ childArgs.add("sleep");
+ final Process p = new ProcessBuilder(childArgs).start();
+ long start = System.nanoTime();
+ if (!p.isAlive() || p.waitFor(0, TimeUnit.MILLISECONDS)) {
+ fail("Test failed: Process exited prematurely");
}
+ long end = System.nanoTime();
+ // give waitFor(timeout) a wide berth (100ms)
+ if ((end - start) > 100000000)
+ fail("Test failed: waitFor took too long");
+ p.destroy();
+ p.waitFor();
+
+ if (p.isAlive() ||
+ !p.waitFor(0, TimeUnit.MILLISECONDS))
+ {
+ fail("Test failed: Process still alive - please terminate " +
+ p.toString() + " manually");
+ }
+ } catch (Throwable t) { unexpected(t); }
+
+ //----------------------------------------------------------------
+ // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
+ // works as expected.
+ //----------------------------------------------------------------
+ try {
+ List<String> childArgs = new ArrayList<String>(javaChildArgs);
+ childArgs.add("sleep");
+ final Process p = new ProcessBuilder(childArgs).start();
+ long start = System.nanoTime();
+
+ p.waitFor(1000, TimeUnit.MILLISECONDS);
+
+ long end = System.nanoTime();
+ if ((end - start) < 500)
+ fail("Test failed: waitFor didn't take long enough");
+
+ p.destroy();
+
+ start = System.nanoTime();
+ p.waitFor(1000, TimeUnit.MILLISECONDS);
+ end = System.nanoTime();
+ if ((end - start) > 100000000)
+ fail("Test failed: waitFor took too long on a dead process.");
+ } catch (Throwable t) { unexpected(t); }
+
+ //----------------------------------------------------------------
+ // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
+ // interrupt works as expected.
+ //----------------------------------------------------------------
+ try {
+ List<String> childArgs = new ArrayList<String>(javaChildArgs);
+ childArgs.add("sleep");
+ final Process p = new ProcessBuilder(childArgs).start();
+ final long start = System.nanoTime();
+
+ final Thread thread = new Thread() {
+ public void run() {
+ try {
+ try {
+ p.waitFor(10000, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ return;
+ }
+ fail("waitFor() wasn't interrupted");
+ } catch (Throwable t) { unexpected(t); }}};
+
+ thread.start();
+ Thread.sleep(1000);
+ thread.interrupt();
+ p.destroy();
+ } catch (Throwable t) { unexpected(t); }
+ }
+
static void closeStreams(Process p) {
try {
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();