test/java/lang/ProcessBuilder/Basic.java

Print this page




2201         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2202         // works as expected.
2203         //----------------------------------------------------------------
2204         try {
2205             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2206             childArgs.add("sleep");
2207             final Process p = new ProcessBuilder(childArgs).start();
2208             long start = System.nanoTime();
2209 
2210             p.waitFor(1000, TimeUnit.MILLISECONDS);
2211 
2212             long end = System.nanoTime();
2213             if ((end - start) < 500000000)
2214                 fail("Test failed: waitFor didn't take long enough");
2215 
2216             p.destroy();
2217 
2218             start = System.nanoTime();
2219             p.waitFor(1000, TimeUnit.MILLISECONDS);
2220             end = System.nanoTime();
2221             if ((end - start) > 100000000)
2222                 fail("Test failed: waitFor took too long on a dead process.");
2223         } catch (Throwable t) { unexpected(t); }
2224 
2225         //----------------------------------------------------------------
2226         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2227         // interrupt works as expected.
2228         //----------------------------------------------------------------
2229         try {
2230             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2231             childArgs.add("sleep");
2232             final Process p = new ProcessBuilder(childArgs).start();
2233             final long start = System.nanoTime();

2234 
2235             final Thread thread = new Thread() {
2236                 public void run() {
2237                     try {
2238                         try {

2239                             p.waitFor(10000, TimeUnit.MILLISECONDS);
2240                         } catch (InterruptedException e) {
2241                             return;
2242                         }
2243                         fail("waitFor() wasn't interrupted");
2244                     } catch (Throwable t) { unexpected(t); }}};
2245 
2246             thread.start();

2247             Thread.sleep(1000);
2248             thread.interrupt();
2249             p.destroy();
2250         } catch (Throwable t) { unexpected(t); }
2251 
2252         //----------------------------------------------------------------
2253         // Check the default implementation for
2254         // Process.waitFor(long, TimeUnit)
2255         //----------------------------------------------------------------
2256         try {
2257             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2258             childArgs.add("sleep");
2259             final Process proc = new ProcessBuilder(childArgs).start();
2260             DelegatingProcess p = new DelegatingProcess(proc);
2261             long start = System.nanoTime();
2262 
2263             p.waitFor(1000, TimeUnit.MILLISECONDS);
2264 
2265             long end = System.nanoTime();
2266             if ((end - start) < 500000000)




2201         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2202         // works as expected.
2203         //----------------------------------------------------------------
2204         try {
2205             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2206             childArgs.add("sleep");
2207             final Process p = new ProcessBuilder(childArgs).start();
2208             long start = System.nanoTime();
2209 
2210             p.waitFor(1000, TimeUnit.MILLISECONDS);
2211 
2212             long end = System.nanoTime();
2213             if ((end - start) < 500000000)
2214                 fail("Test failed: waitFor didn't take long enough");
2215 
2216             p.destroy();
2217 
2218             start = System.nanoTime();
2219             p.waitFor(1000, TimeUnit.MILLISECONDS);
2220             end = System.nanoTime();
2221             if ((end - start) > 900000000)
2222                 fail("Test failed: waitFor took too long on a dead process.");
2223         } catch (Throwable t) { unexpected(t); }
2224 
2225         //----------------------------------------------------------------
2226         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2227         // interrupt works as expected.
2228         //----------------------------------------------------------------
2229         try {
2230             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2231             childArgs.add("sleep");
2232             final Process p = new ProcessBuilder(childArgs).start();
2233             final long start = System.nanoTime();
2234             final CountDownLatch latch = new CountDownLatch(1);
2235 
2236             final Thread thread = new Thread() {
2237                 public void run() {
2238                     try {
2239                         try {
2240                             latch.countDown();
2241                             p.waitFor(10000, TimeUnit.MILLISECONDS);
2242                         } catch (InterruptedException e) {
2243                             return;
2244                         }
2245                         fail("waitFor() wasn't interrupted");
2246                     } catch (Throwable t) { unexpected(t); }}};
2247 
2248             thread.start();
2249             latch.await();
2250             Thread.sleep(1000);
2251             thread.interrupt();
2252             p.destroy();
2253         } catch (Throwable t) { unexpected(t); }
2254 
2255         //----------------------------------------------------------------
2256         // Check the default implementation for
2257         // Process.waitFor(long, TimeUnit)
2258         //----------------------------------------------------------------
2259         try {
2260             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2261             childArgs.add("sleep");
2262             final Process proc = new ProcessBuilder(childArgs).start();
2263             DelegatingProcess p = new DelegatingProcess(proc);
2264             long start = System.nanoTime();
2265 
2266             p.waitFor(1000, TimeUnit.MILLISECONDS);
2267 
2268             long end = System.nanoTime();
2269             if ((end - start) < 500000000)