< prev index next >

test/java/lang/ProcessBuilder/Basic.java

Print this page
rev 13659 : 8208715: Conversion of milliseconds to nanoseconds in UNIXProcess contains bug
   1 /*
   2  * Copyright (c) 2003, 2013, 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  */


  44 import java.security.*;
  45 import sun.misc.Unsafe;
  46 import java.util.regex.Pattern;
  47 import java.util.regex.Matcher;
  48 import static java.lang.System.getenv;
  49 import static java.lang.System.out;
  50 import static java.lang.Boolean.TRUE;
  51 import static java.util.AbstractMap.SimpleImmutableEntry;
  52 
  53 public class Basic {
  54 
  55     /* used for Windows only */
  56     static final String systemRoot = System.getenv("SystemRoot");
  57 
  58     /* used for Mac OS X only */
  59     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  60 
  61     /* used for AIX only */
  62     static final String libpath = System.getenv("LIBPATH");
  63 









  64     private static String commandOutput(Reader r) throws Throwable {
  65         StringBuilder sb = new StringBuilder();
  66         int c;
  67         while ((c = r.read()) > 0)
  68             if (c != '\r')
  69                 sb.append((char) c);
  70         return sb.toString();
  71     }
  72 
  73     private static String commandOutput(Process p) throws Throwable {
  74         check(p.getInputStream()  == p.getInputStream());
  75         check(p.getOutputStream() == p.getOutputStream());
  76         check(p.getErrorStream()  == p.getErrorStream());
  77         Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
  78         String output = commandOutput(r);
  79         equal(p.waitFor(), 0);
  80         equal(p.exitValue(), 0);
  81         // The debug/fastdebug versions of the VM may write some warnings to stdout
  82         // (i.e. "Warning:  Cannot open log file: hotspot.log" if the VM is started
  83         // in a directory without write permissions). These warnings will confuse tests


2277 
2278             long end = System.nanoTime();
2279             if ((end - start) < TimeUnit.MILLISECONDS.toNanos(10))
2280                 fail("Test failed: waitFor didn't take long enough (" + (end - start) + "ns)");
2281 
2282             p.destroy();
2283 
2284             start = System.nanoTime();
2285             p.waitFor(8, TimeUnit.SECONDS);
2286             end = System.nanoTime();
2287 
2288             int exitValue = p.exitValue();
2289 
2290             if ((end - start) > TimeUnit.SECONDS.toNanos(7))
2291                 fail("Test failed: waitFor took too long on a dead process. (" + (end - start) + "ns)"
2292                 + ", exitValue: " + exitValue);
2293         } catch (Throwable t) { unexpected(t); }
2294 
2295         //----------------------------------------------------------------
2296         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2297         // interrupt works as expected.
2298         //----------------------------------------------------------------
2299         try {
2300             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2301             childArgs.add("sleep");
2302             final Process p = new ProcessBuilder(childArgs).start();
2303             final long start = System.nanoTime();
2304             final CountDownLatch ready = new CountDownLatch(1);
2305             final CountDownLatch done = new CountDownLatch(1);
2306 
2307             final Thread thread = new Thread() {
2308                 public void run() {
2309                     try {
2310                         final boolean result;
2311                         try {
2312                             ready.countDown();
2313                             result = p.waitFor(30000, TimeUnit.MILLISECONDS);
2314                         } catch (InterruptedException e) {
2315                             return;
2316                         }
2317                         fail("waitFor() wasn't interrupted, its return value was: " + result);
2318                     } catch (Throwable t) {
2319                         unexpected(t);
2320                     } finally {
2321                         done.countDown();
2322                     }































2323                 }
2324             };
2325 
2326             thread.start();
2327             ready.await();
2328             Thread.sleep(1000);
2329             thread.interrupt();
2330             done.await();


2331             p.destroy();
2332         } catch (Throwable t) { unexpected(t); }
2333 
2334         //----------------------------------------------------------------
2335         // Check the default implementation for
2336         // Process.waitFor(long, TimeUnit)
2337         //----------------------------------------------------------------
2338         try {
2339             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2340             childArgs.add("sleep");
2341             final Process proc = new ProcessBuilder(childArgs).start();
2342             DelegatingProcess p = new DelegatingProcess(proc);
2343             long start = System.nanoTime();
2344 
2345             p.waitFor(1000, TimeUnit.MILLISECONDS);
2346 
2347             long end = System.nanoTime();
2348             if ((end - start) < 500000000)
2349                 fail("Test failed: waitFor didn't take long enough");
2350 


   1 /*
   2  * Copyright (c) 2003, 2019, 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  */


  44 import java.security.*;
  45 import sun.misc.Unsafe;
  46 import java.util.regex.Pattern;
  47 import java.util.regex.Matcher;
  48 import static java.lang.System.getenv;
  49 import static java.lang.System.out;
  50 import static java.lang.Boolean.TRUE;
  51 import static java.util.AbstractMap.SimpleImmutableEntry;
  52 
  53 public class Basic {
  54 
  55     /* used for Windows only */
  56     static final String systemRoot = System.getenv("SystemRoot");
  57 
  58     /* used for Mac OS X only */
  59     static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
  60 
  61     /* used for AIX only */
  62     static final String libpath = System.getenv("LIBPATH");
  63 
  64     /**
  65      * Returns the number of milliseconds since time given by
  66      * startNanoTime, which must have been previously returned from a
  67      * call to {@link System.nanoTime()}.
  68      */
  69     private static long millisElapsedSince(long startNanoTime) {
  70         return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
  71     }
  72 
  73     private static String commandOutput(Reader r) throws Throwable {
  74         StringBuilder sb = new StringBuilder();
  75         int c;
  76         while ((c = r.read()) > 0)
  77             if (c != '\r')
  78                 sb.append((char) c);
  79         return sb.toString();
  80     }
  81 
  82     private static String commandOutput(Process p) throws Throwable {
  83         check(p.getInputStream()  == p.getInputStream());
  84         check(p.getOutputStream() == p.getOutputStream());
  85         check(p.getErrorStream()  == p.getErrorStream());
  86         Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
  87         String output = commandOutput(r);
  88         equal(p.waitFor(), 0);
  89         equal(p.exitValue(), 0);
  90         // The debug/fastdebug versions of the VM may write some warnings to stdout
  91         // (i.e. "Warning:  Cannot open log file: hotspot.log" if the VM is started
  92         // in a directory without write permissions). These warnings will confuse tests


2286 
2287             long end = System.nanoTime();
2288             if ((end - start) < TimeUnit.MILLISECONDS.toNanos(10))
2289                 fail("Test failed: waitFor didn't take long enough (" + (end - start) + "ns)");
2290 
2291             p.destroy();
2292 
2293             start = System.nanoTime();
2294             p.waitFor(8, TimeUnit.SECONDS);
2295             end = System.nanoTime();
2296 
2297             int exitValue = p.exitValue();
2298 
2299             if ((end - start) > TimeUnit.SECONDS.toNanos(7))
2300                 fail("Test failed: waitFor took too long on a dead process. (" + (end - start) + "ns)"
2301                 + ", exitValue: " + exitValue);
2302         } catch (Throwable t) { unexpected(t); }
2303 
2304         //----------------------------------------------------------------
2305         // Check that Process.waitFor(timeout, TimeUnit.MILLISECONDS)
2306         // interrupt works as expected, if interrupted while waiting.
2307         //----------------------------------------------------------------
2308         try {
2309             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2310             childArgs.add("sleep");
2311             final Process p = new ProcessBuilder(childArgs).start();
2312             final long start = System.nanoTime();
2313             final CountDownLatch aboutToWaitFor = new CountDownLatch(1);

2314 
2315             final Thread thread = new Thread() {
2316                 public void run() {
2317                     try {
2318                         aboutToWaitFor.countDown();
2319                         Thread.currentThread().interrupt();
2320                         boolean result = p.waitFor(30L * 1000L, TimeUnit.MILLISECONDS);




2321                         fail("waitFor() wasn't interrupted, its return value was: " + result);
2322                     } catch (InterruptedException success) {
2323                     } catch (Throwable t) { unexpected(t); }


2324                 }
2325             };
2326 
2327             thread.start();
2328             aboutToWaitFor.await();
2329             thread.interrupt();
2330             thread.join(10L * 1000L);
2331             check(millisElapsedSince(start) < 10L * 1000L);
2332             check(!thread.isAlive());
2333             p.destroy();
2334         } catch (Throwable t) { unexpected(t); }
2335 
2336         //----------------------------------------------------------------
2337         // Check that Process.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS)
2338         // interrupt works as expected, if interrupted while waiting.
2339         //----------------------------------------------------------------
2340         try {
2341             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2342             childArgs.add("sleep");
2343             final Process p = new ProcessBuilder(childArgs).start();
2344             final long start = System.nanoTime();
2345             final CountDownLatch aboutToWaitFor = new CountDownLatch(1);
2346 
2347             final Thread thread = new Thread() {
2348                 public void run() {
2349                     try {
2350                         aboutToWaitFor.countDown();
2351                         Thread.currentThread().interrupt();
2352                         boolean result = p.waitFor(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
2353                         fail("waitFor() wasn't interrupted, its return value was: " + result);
2354                     } catch (InterruptedException success) {
2355                     } catch (Throwable t) { unexpected(t); }
2356                 }
2357             };
2358 
2359             thread.start();
2360             aboutToWaitFor.await();

2361             thread.interrupt();
2362             thread.join(10L * 1000L);
2363             check(millisElapsedSince(start) < 10L * 1000L);
2364             check(!thread.isAlive());
2365             p.destroy();
2366         } catch (Throwable t) { unexpected(t); }
2367 
2368         //----------------------------------------------------------------
2369         // Check the default implementation for
2370         // Process.waitFor(long, TimeUnit)
2371         //----------------------------------------------------------------
2372         try {
2373             List<String> childArgs = new ArrayList<String>(javaChildArgs);
2374             childArgs.add("sleep");
2375             final Process proc = new ProcessBuilder(childArgs).start();
2376             DelegatingProcess p = new DelegatingProcess(proc);
2377             long start = System.nanoTime();
2378 
2379             p.waitFor(1000, TimeUnit.MILLISECONDS);
2380 
2381             long end = System.nanoTime();
2382             if ((end - start) < 500000000)
2383                 fail("Test failed: waitFor didn't take long enough");
2384 


< prev index next >