jdk/test/java/lang/ProcessBuilder/Basic.java
changeset 13149 27d52f97a5cc
parent 12882 35d381df9422
child 13581 b3fe3cd75b37
equal deleted inserted replaced
13033:365efcc2d50c 13149:27d52f97a5cc
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
    26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
    27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
    27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
    28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
    28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
    29  *      4947220 7018606 7034570
    29  *      4947220 7018606 7034570 4244896
    30  * @summary Basic tests for Process and Environment Variable code
    30  * @summary Basic tests for Process and Environment Variable code
    31  * @run main/othervm/timeout=300 Basic
    31  * @run main/othervm/timeout=300 Basic
    32  * @author Martin Buchholz
    32  * @author Martin Buchholz
    33  */
    33  */
    34 
    34 
    36 import static java.lang.ProcessBuilder.Redirect.*;
    36 import static java.lang.ProcessBuilder.Redirect.*;
    37 
    37 
    38 import java.io.*;
    38 import java.io.*;
    39 import java.util.*;
    39 import java.util.*;
    40 import java.util.concurrent.CountDownLatch;
    40 import java.util.concurrent.CountDownLatch;
       
    41 import java.util.concurrent.TimeUnit;
    41 import java.security.*;
    42 import java.security.*;
    42 import java.util.regex.Pattern;
    43 import java.util.regex.Pattern;
    43 import java.util.regex.Matcher;
    44 import java.util.regex.Matcher;
    44 import static java.lang.System.getenv;
    45 import static java.lang.System.getenv;
    45 import static java.lang.System.out;
    46 import static java.lang.System.out;
   634 
   635 
   635         /** Returns true if we can expect English OS error strings */
   636         /** Returns true if we can expect English OS error strings */
   636         static boolean is() { return is; }
   637         static boolean is() { return is; }
   637     }
   638     }
   638 
   639 
       
   640     static class DelegatingProcess extends Process {
       
   641         final Process p;
       
   642 
       
   643         DelegatingProcess(Process p) {
       
   644             this.p = p;
       
   645         }
       
   646 
       
   647         @Override
       
   648         public void destroy() {
       
   649             p.destroy();
       
   650         }
       
   651 
       
   652         @Override
       
   653         public int exitValue() {
       
   654             return p.exitValue();
       
   655         }
       
   656 
       
   657         @Override
       
   658         public int waitFor() throws InterruptedException {
       
   659             return p.waitFor();
       
   660         }
       
   661 
       
   662         @Override
       
   663         public OutputStream getOutputStream() {
       
   664             return p.getOutputStream();
       
   665         }
       
   666 
       
   667         @Override
       
   668         public InputStream getInputStream() {
       
   669             return p.getInputStream();
       
   670         }
       
   671 
       
   672         @Override
       
   673         public InputStream getErrorStream() {
       
   674             return p.getErrorStream();
       
   675         }
       
   676     }
       
   677 
   639     private static boolean matches(String str, String regex) {
   678     private static boolean matches(String str, String regex) {
   640         return Pattern.compile(regex).matcher(str).find();
   679         return Pattern.compile(regex).matcher(str).find();
   641     }
   680     }
   642 
   681 
   643     private static String matchAndExtract(String str, String regex) {
   682     private static String matchAndExtract(String str, String regex) {
  2088 
  2127 
  2089         // Restore "normal" state without a security manager
  2128         // Restore "normal" state without a security manager
  2090         policy.setPermissions(new RuntimePermission("setSecurityManager"));
  2129         policy.setPermissions(new RuntimePermission("setSecurityManager"));
  2091         System.setSecurityManager(null);
  2130         System.setSecurityManager(null);
  2092 
  2131 
       
  2132         //----------------------------------------------------------------
       
  2133         // Check that Process.isAlive() &
       
  2134         // Process.waitFor(0, TimeUnit.MILLISECONDS) work as expected.
       
  2135         //----------------------------------------------------------------
       
  2136         try {
       
  2137             List<String> childArgs = new ArrayList<String>(javaChildArgs);
       
  2138             childArgs.add("sleep");
       
  2139             final Process p = new ProcessBuilder(childArgs).start();
       
  2140             long start = System.nanoTime();
       
  2141             if (!p.isAlive() || p.waitFor(0, TimeUnit.MILLISECONDS)) {
       
  2142                 fail("Test failed: Process exited prematurely");
       
  2143             }
       
  2144             long end = System.nanoTime();
       
  2145             // give waitFor(timeout) a wide berth (100ms)
       
  2146             if ((end - start) > 100000000)
       
  2147                 fail("Test failed: waitFor took too long");
       
  2148 
       
  2149             p.destroy();
       
  2150             p.waitFor();
       
  2151 
       
  2152             if (p.isAlive() ||
       
  2153                 !p.waitFor(0, TimeUnit.MILLISECONDS))
       
  2154             {
       
  2155                 fail("Test failed: Process still alive - please terminate " +
       
  2156                     p.toString() + " manually");
       
  2157             }
       
  2158         } catch (Throwable t) { unexpected(t); }
       
  2159 
       
  2160         //----------------------------------------------------------------
       
  2161         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
       
  2162         // works as expected.
       
  2163         //----------------------------------------------------------------
       
  2164         try {
       
  2165             List<String> childArgs = new ArrayList<String>(javaChildArgs);
       
  2166             childArgs.add("sleep");
       
  2167             final Process p = new ProcessBuilder(childArgs).start();
       
  2168             long start = System.nanoTime();
       
  2169 
       
  2170             p.waitFor(1000, TimeUnit.MILLISECONDS);
       
  2171 
       
  2172             long end = System.nanoTime();
       
  2173             if ((end - start) < 500000000)
       
  2174                 fail("Test failed: waitFor didn't take long enough");
       
  2175 
       
  2176             p.destroy();
       
  2177 
       
  2178             start = System.nanoTime();
       
  2179             p.waitFor(1000, TimeUnit.MILLISECONDS);
       
  2180             end = System.nanoTime();
       
  2181             if ((end - start) > 100000000)
       
  2182                 fail("Test failed: waitFor took too long on a dead process.");
       
  2183         } catch (Throwable t) { unexpected(t); }
       
  2184 
       
  2185         //----------------------------------------------------------------
       
  2186         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
       
  2187         // interrupt works as expected.
       
  2188         //----------------------------------------------------------------
       
  2189         try {
       
  2190             List<String> childArgs = new ArrayList<String>(javaChildArgs);
       
  2191             childArgs.add("sleep");
       
  2192             final Process p = new ProcessBuilder(childArgs).start();
       
  2193             final long start = System.nanoTime();
       
  2194 
       
  2195             final Thread thread = new Thread() {
       
  2196                 public void run() {
       
  2197                     try {
       
  2198                         try {
       
  2199                             p.waitFor(10000, TimeUnit.MILLISECONDS);
       
  2200                         } catch (InterruptedException e) {
       
  2201                             return;
       
  2202                         }
       
  2203                         fail("waitFor() wasn't interrupted");
       
  2204                     } catch (Throwable t) { unexpected(t); }}};
       
  2205 
       
  2206             thread.start();
       
  2207             Thread.sleep(1000);
       
  2208             thread.interrupt();
       
  2209             p.destroy();
       
  2210         } catch (Throwable t) { unexpected(t); }
       
  2211 
       
  2212         //----------------------------------------------------------------
       
  2213         // Check the default implementation for
       
  2214         // Process.waitFor(long, TimeUnit)
       
  2215         //----------------------------------------------------------------
       
  2216         try {
       
  2217             List<String> childArgs = new ArrayList<String>(javaChildArgs);
       
  2218             childArgs.add("sleep");
       
  2219             final Process proc = new ProcessBuilder(childArgs).start();
       
  2220             DelegatingProcess p = new DelegatingProcess(proc);
       
  2221             long start = System.nanoTime();
       
  2222 
       
  2223             p.waitFor(1000, TimeUnit.MILLISECONDS);
       
  2224 
       
  2225             long end = System.nanoTime();
       
  2226             if ((end - start) < 500000000)
       
  2227                 fail("Test failed: waitFor didn't take long enough");
       
  2228 
       
  2229             p.destroy();
       
  2230 
       
  2231             start = System.nanoTime();
       
  2232             p.waitFor(1000, TimeUnit.MILLISECONDS);
       
  2233             end = System.nanoTime();
       
  2234             // allow for the less accurate default implementation
       
  2235             if ((end - start) > 200000000)
       
  2236                 fail("Test failed: waitFor took too long on a dead process.");
       
  2237         } catch (Throwable t) { unexpected(t); }
  2093     }
  2238     }
  2094 
  2239 
  2095     static void closeStreams(Process p) {
  2240     static void closeStreams(Process p) {
  2096         try {
  2241         try {
  2097             p.getOutputStream().close();
  2242             p.getOutputStream().close();