test/java/lang/ProcessBuilder/Basic.java

Print this page




2223 
2224             p.destroy();
2225 
2226             start = System.nanoTime();
2227             p.waitFor(1000, TimeUnit.MILLISECONDS);
2228             end = System.nanoTime();
2229             if ((end - start) > 900000000)
2230                 fail("Test failed: waitFor took too long on a dead process.");
2231         } catch (Throwable t) { unexpected(t); }
2232 
2233         //----------------------------------------------------------------
2234         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2235         // interrupt works as expected.
2236         //----------------------------------------------------------------
2237         try {
2238             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2239             childArgs.add("sleep");
2240             final Process p = new ProcessBuilder(childArgs).start();
2241             final long start = System.nanoTime();
2242             final CountDownLatch latch = new CountDownLatch(1);

2243 
2244             final Thread thread = new Thread() {
2245                 public void run() {
2246                     try {

2247                         try {
2248                             latch.countDown();
2249                             p.waitFor(30000, TimeUnit.MILLISECONDS);
2250                         } catch (InterruptedException e) {
2251                             return;
2252                         }
2253                         fail("waitFor() wasn't interrupted");
2254                     } catch (Throwable t) { unexpected(t); }}};






2255 
2256             thread.start();
2257             latch.await();
2258             Thread.sleep(1000);
2259             thread.interrupt();

2260             p.destroy();
2261         } catch (Throwable t) { unexpected(t); }
2262 
2263         //----------------------------------------------------------------
2264         // Check the default implementation for
2265         // Process.waitFor(long, TimeUnit)
2266         //----------------------------------------------------------------
2267         try {
2268             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2269             childArgs.add("sleep");
2270             final Process proc = new ProcessBuilder(childArgs).start();
2271             DelegatingProcess p = new DelegatingProcess(proc);
2272             long start = System.nanoTime();
2273 
2274             p.waitFor(1000, TimeUnit.MILLISECONDS);
2275 
2276             long end = System.nanoTime();
2277             if ((end - start) < 500000000)
2278                 fail("Test failed: waitFor didn't take long enough");
2279 




2223 
2224             p.destroy();
2225 
2226             start = System.nanoTime();
2227             p.waitFor(1000, TimeUnit.MILLISECONDS);
2228             end = System.nanoTime();
2229             if ((end - start) > 900000000)
2230                 fail("Test failed: waitFor took too long on a dead process.");
2231         } catch (Throwable t) { unexpected(t); }
2232 
2233         //----------------------------------------------------------------
2234         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2235         // interrupt works as expected.
2236         //----------------------------------------------------------------
2237         try {
2238             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2239             childArgs.add("sleep");
2240             final Process p = new ProcessBuilder(childArgs).start();
2241             final long start = System.nanoTime();
2242             final CountDownLatch latch = new CountDownLatch(1);
2243             final CountDownLatch done = new CountDownLatch(1);
2244 
2245             final Thread thread = new Thread() {
2246                 public void run() {
2247                     try {
2248                         final boolean result;
2249                         try {
2250                             latch.countDown();
2251                             result = p.waitFor(30000, TimeUnit.MILLISECONDS);
2252                         } catch (InterruptedException e) {
2253                             return;
2254                         }
2255                         fail("waitFor() wasn't interrupted, its return value was: " + result);
2256                     } catch (Throwable t) {
2257                         unexpected(t);
2258                     } finally {
2259                         done.countDown();
2260                     }
2261                 }
2262             };
2263 
2264             thread.start();
2265             latch.await();
2266             Thread.sleep(1000);
2267             thread.interrupt();
2268             done.await();
2269             p.destroy();
2270         } catch (Throwable t) { unexpected(t); }
2271 
2272         //----------------------------------------------------------------
2273         // Check the default implementation for
2274         // Process.waitFor(long, TimeUnit)
2275         //----------------------------------------------------------------
2276         try {
2277             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2278             childArgs.add("sleep");
2279             final Process proc = new ProcessBuilder(childArgs).start();
2280             DelegatingProcess p = new DelegatingProcess(proc);
2281             long start = System.nanoTime();
2282 
2283             p.waitFor(1000, TimeUnit.MILLISECONDS);
2284 
2285             long end = System.nanoTime();
2286             if ((end - start) < 500000000)
2287                 fail("Test failed: waitFor didn't take long enough");
2288