< prev index next >

jdk/test/java/lang/ProcessBuilder/Basic.java

Print this page
rev 11228 : 8054494: Remove sun.misc.Unsafe.monitorEnter, monitorExit and tryMonitorEnter
Reviewed-by: dholmes
   1 /*
   2  * Copyright (c) 2003, 2014, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570 4244896 5049299 8003488
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic
  33  * @author Martin Buchholz
  34  */
  35 
  36 import java.lang.ProcessBuilder.Redirect;
  37 import static java.lang.ProcessBuilder.Redirect.*;
  38 
  39 import java.io.*;
  40 import java.lang.reflect.Field;
  41 import java.nio.file.Files;
  42 import java.nio.file.Paths;
  43 import java.nio.file.StandardCopyOption;
  44 import java.util.*;
  45 import java.util.concurrent.CountDownLatch;
  46 import java.util.concurrent.TimeUnit;
  47 import java.security.*;
  48 import sun.misc.Unsafe;
  49 import java.util.regex.Pattern;


2042                     final Object deferred;
2043                     Class<?> c = s.getClass();
2044                     if (c.getName().equals(
2045                         "java.lang.UNIXProcess$DeferredCloseInputStream"))
2046                     {
2047                         deferred = s;
2048                     } else {
2049                         Field deferredField = p.getClass().
2050                             getDeclaredField("stdout_inner_stream");
2051                         deferredField.setAccessible(true);
2052                         deferred = deferredField.get(p);
2053                     }
2054                     Field useCountField = deferred.getClass().
2055                         getDeclaredField("useCount");
2056                     useCountField.setAccessible(true);
2057 
2058                     while (useCountField.getInt(deferred) <= 0) {
2059                         Thread.yield();
2060                     }
2061                 } else if (s instanceof BufferedInputStream) {
2062                     Field f = Unsafe.class.getDeclaredField("theUnsafe");
2063                     f.setAccessible(true);
2064                     Unsafe unsafe = (Unsafe)f.get(null);
2065 
2066                     while (unsafe.tryMonitorEnter(s)) {
2067                         unsafe.monitorExit(s);
2068                         Thread.sleep(1);
2069                     }
2070                 }
2071                 p.destroy();
2072                 thread.join();
2073             }
2074         } catch (Throwable t) { unexpected(t); }
2075 
2076         //----------------------------------------------------------------
2077         // Check that subprocesses which create subprocesses of their
2078         // own do not cause parent to hang waiting for file
2079         // descriptors to be closed.
2080         //----------------------------------------------------------------
2081         try {
2082             if (Unix.is()
2083                 && new File("/bin/bash").exists()
2084                 && new File("/bin/sleep").exists()) {
2085                 // Notice that we only destroy the process created by us (i.e.
2086                 // our child) but not our grandchild (i.e. '/bin/sleep'). So
2087                 // pay attention that the grandchild doesn't run too long to
2088                 // avoid polluting the process space with useless processes.


2548     static void fail() {failed++; Thread.dumpStack();}
2549     static void fail(String msg) {System.out.println(msg); fail();}
2550     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
2551     static void check(boolean cond) {if (cond) pass(); else fail();}
2552     static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
2553     static void equal(Object x, Object y) {
2554         if (x == null ? y == null : x.equals(y)) pass();
2555         else fail(">'" + x + "'<" + " not equal to " + "'" + y + "'");}
2556 
2557     public static void main(String[] args) throws Throwable {
2558         try {realMain(args);} catch (Throwable t) {unexpected(t);}
2559         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
2560         if (failed > 0) throw new AssertionError("Some tests failed");}
2561     interface Fun {void f() throws Throwable;}
2562     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
2563         for (Fun f : fs)
2564             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
2565             catch (Throwable t) {
2566                 if (k.isAssignableFrom(t.getClass())) pass();
2567                 else unexpected(t);}}

















2568 }
   1 /*
   2  * Copyright (c) 2003, 2015, 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  */
  23 
  24 /*
  25  * @test
  26  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570 4244896 5049299 8003488 8054494
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic
  33  * @author Martin Buchholz
  34  */
  35 
  36 import java.lang.ProcessBuilder.Redirect;
  37 import static java.lang.ProcessBuilder.Redirect.*;
  38 
  39 import java.io.*;
  40 import java.lang.reflect.Field;
  41 import java.nio.file.Files;
  42 import java.nio.file.Paths;
  43 import java.nio.file.StandardCopyOption;
  44 import java.util.*;
  45 import java.util.concurrent.CountDownLatch;
  46 import java.util.concurrent.TimeUnit;
  47 import java.security.*;
  48 import sun.misc.Unsafe;
  49 import java.util.regex.Pattern;


2042                     final Object deferred;
2043                     Class<?> c = s.getClass();
2044                     if (c.getName().equals(
2045                         "java.lang.UNIXProcess$DeferredCloseInputStream"))
2046                     {
2047                         deferred = s;
2048                     } else {
2049                         Field deferredField = p.getClass().
2050                             getDeclaredField("stdout_inner_stream");
2051                         deferredField.setAccessible(true);
2052                         deferred = deferredField.get(p);
2053                     }
2054                     Field useCountField = deferred.getClass().
2055                         getDeclaredField("useCount");
2056                     useCountField.setAccessible(true);
2057 
2058                     while (useCountField.getInt(deferred) <= 0) {
2059                         Thread.yield();
2060                     }
2061                 } else if (s instanceof BufferedInputStream) {
2062                     // Wait until after the s.read occurs in "thread" by
2063                     // checking when the input stream monitor is acquired
2064                     // (BufferedInputStream.read is synchronized)
2065                     while (!isLocked(s, 10)) {
2066                         Thread.sleep(100);


2067                     }
2068                 }
2069                 p.destroy();
2070                 thread.join();
2071             }
2072         } catch (Throwable t) { unexpected(t); }
2073 
2074         //----------------------------------------------------------------
2075         // Check that subprocesses which create subprocesses of their
2076         // own do not cause parent to hang waiting for file
2077         // descriptors to be closed.
2078         //----------------------------------------------------------------
2079         try {
2080             if (Unix.is()
2081                 && new File("/bin/bash").exists()
2082                 && new File("/bin/sleep").exists()) {
2083                 // Notice that we only destroy the process created by us (i.e.
2084                 // our child) but not our grandchild (i.e. '/bin/sleep'). So
2085                 // pay attention that the grandchild doesn't run too long to
2086                 // avoid polluting the process space with useless processes.


2546     static void fail() {failed++; Thread.dumpStack();}
2547     static void fail(String msg) {System.out.println(msg); fail();}
2548     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
2549     static void check(boolean cond) {if (cond) pass(); else fail();}
2550     static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
2551     static void equal(Object x, Object y) {
2552         if (x == null ? y == null : x.equals(y)) pass();
2553         else fail(">'" + x + "'<" + " not equal to " + "'" + y + "'");}
2554 
2555     public static void main(String[] args) throws Throwable {
2556         try {realMain(args);} catch (Throwable t) {unexpected(t);}
2557         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
2558         if (failed > 0) throw new AssertionError("Some tests failed");}
2559     interface Fun {void f() throws Throwable;}
2560     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
2561         for (Fun f : fs)
2562             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
2563             catch (Throwable t) {
2564                 if (k.isAssignableFrom(t.getClass())) pass();
2565                 else unexpected(t);}}
2566 
2567     static boolean isLocked(final Object monitor, final long millis) throws InterruptedException {
2568         return new Thread() {
2569             volatile boolean unlocked;
2570 
2571             @Override
2572             public void run() {
2573                 synchronized (monitor) { unlocked = true; }
2574             }
2575 
2576             boolean isLocked() throws InterruptedException {
2577                 start();
2578                 join(millis);
2579                 return !unlocked;
2580             }
2581         }.isLocked();
2582     }
2583 }
< prev index next >