jdk/test/java/lang/ProcessBuilder/InheritIOEHandle.java
changeset 21294 545dfa5b947e
parent 19372 e404c834f1cd
equal deleted inserted replaced
21293:56310f56feac 21294:545dfa5b947e
    33 import java.io.IOException;
    33 import java.io.IOException;
    34 import java.io.InputStreamReader;
    34 import java.io.InputStreamReader;
    35 
    35 
    36 public class InheritIOEHandle {
    36 public class InheritIOEHandle {
    37     private static enum APP {
    37     private static enum APP {
    38         B, C;
    38         A, B, C;
    39     }
    39     }
    40     private static File stopC = new File(".\\StopC.txt");
    40 
       
    41     private static File stopC = new File("StopC.txt");
    41     private static String SIGNAL = "After call child process";
    42     private static String SIGNAL = "After call child process";
    42     private static String JAVA_EXE = System.getProperty("java.home")
    43     private static String JAVA_EXE = System.getProperty("java.home")
    43         + File.separator + "bin"
    44             + File.separator + "bin"
    44         + File.separator + "java";
    45             + File.separator + "java";
    45 
    46 
    46     private static String[] getCommandArray(String processName) {
    47     private static String[] getCommandArray(String processName) {
    47         String[] cmdArray = {
    48         String[] cmdArray = {
    48             JAVA_EXE,
    49                 JAVA_EXE,
    49             "-cp",
    50                 "-cp",
    50             System.getProperty("java.class.path"),
    51                 System.getProperty("java.class.path"),
    51             InheritIOEHandle.class.getName(),
    52                 InheritIOEHandle.class.getName(),
    52             processName
    53                 processName
    53         };
    54         };
    54         return cmdArray;
    55         return cmdArray;
    55     }
    56     }
    56 
    57 
    57     public static void main(String[] args) throws Exception {
    58     public static void main(String[] args) throws Exception {
    58         if (!System.getProperty("os.name").startsWith("Windows")) {
    59         if (!System.getProperty("os.name").startsWith("Windows")) {
    59             return;
    60             return;
    60         }
    61         }
    61 
    62 
    62         if (args.length > 0) {
    63         APP app = (args.length > 0) ? APP.valueOf(args[0]) : APP.A;
    63             APP app = APP.valueOf(args[0]);
    64         switch (app) {
    64             switch (app) {
    65             case A:
       
    66                 performA();
       
    67                 break;
    65             case B:
    68             case B:
    66                 performB();
    69                 performB();
    67                 break;
    70                 break;
    68             case C:
    71             case C:
    69                 performC();
    72                 performC();
    70                 break;
    73                 break;
    71             }
       
    72             return;
       
    73         }
    74         }
    74         performA();
       
    75     }
    75     }
    76 
    76 
    77     private static void performA() {
    77     private static void performA() {
    78         try {
    78         try {
    79             stopC.delete();
    79             stopC.delete();
    85             Process process = builder.start();
    85             Process process = builder.start();
    86 
    86 
    87             process.getOutputStream().close();
    87             process.getOutputStream().close();
    88             process.getErrorStream().close();
    88             process.getErrorStream().close();
    89 
    89 
    90             try (BufferedReader in = new BufferedReader( new InputStreamReader(
    90             boolean isSignalReceived = false;
    91                          process.getInputStream(), "utf-8")))
    91             try (BufferedReader in = new BufferedReader(new InputStreamReader(
       
    92                     process.getInputStream(), "utf-8")))
    92             {
    93             {
    93                 String result;
    94                 String result;
    94                 while ((result = in.readLine()) != null) {
    95                 while ((result = in.readLine()) != null) {
    95                     if (!SIGNAL.equals(result)) {
    96                     if (SIGNAL.equals(result)) {
    96                         throw new Error("Catastrophe in process B! Bad output.");
    97                         isSignalReceived = true;
       
    98                         break;
       
    99                     } else {
       
   100                         throw new RuntimeException("Catastrophe in process B! Bad output.");
    97                     }
   101                     }
    98                 }
   102                 }
       
   103 
       
   104             }
       
   105             if (!isSignalReceived) {
       
   106                 throw new RuntimeException("Signal from B was not received");
    99             }
   107             }
   100 
   108 
   101             // If JDK-7147084 is not fixed that point is unreachable.
   109             // If JDK-7147084 is not fixed that point is unreachable.
       
   110             System.out.println("Received signal from B, creating file StopC");
       
   111             // write signal file
       
   112             boolean isFileStopC = stopC.createNewFile();
       
   113             if (!isFileStopC) {
       
   114                 throw new RuntimeException("Signal file StopC.txt was not created. TEST or INFRA bug");
       
   115             }
   102 
   116 
   103             // write signal file
   117             process.waitFor();
   104             stopC.createNewFile();
       
   105 
   118 
   106             System.err.println("Read stream finished.");
   119             System.err.println("Read stream finished.");
   107         } catch (IOException ex) {
   120         } catch (IOException ex) {
   108             throw new Error("Catastrophe in process A!", ex);
   121             throw new RuntimeException("Catastrophe in process A!", ex);
       
   122         } catch (InterruptedException ex) {
       
   123             throw new RuntimeException("A was interrupted while waiting for B", ex);
   109         }
   124         }
   110     }
   125     }
   111 
   126 
   112     private static void performB() {
   127     private static void performB() {
   113         try {
   128         try {
   119             process.getInputStream().close();
   134             process.getInputStream().close();
   120             process.getOutputStream().close();
   135             process.getOutputStream().close();
   121             process.getErrorStream().close();
   136             process.getErrorStream().close();
   122 
   137 
   123             System.out.println(SIGNAL);
   138             System.out.println(SIGNAL);
       
   139             process.waitFor();
   124 
   140 
   125             // JDK-7147084 subject:
   141             // JDK-7147084 subject:
   126             // Process C inherits the [System.out] handle and
   142             // Process C inherits the [System.out] handle and
   127             // handle close in B does not finalize the streaming for A.
   143             // handle close in B does not finalize the streaming for A.
   128             // (handle reference count > 1).
   144             // (handle reference count > 1).
   129         } catch (IOException ex) {
   145         } catch (IOException ex) {
   130             throw new Error("Catastrophe in process B!", ex);
   146             throw new RuntimeException("Catastrophe in process B!", ex);
       
   147         } catch (InterruptedException ex) {
       
   148             throw new RuntimeException("B was interrupted while waiting for C", ex);
   131         }
   149         }
   132     }
   150     }
   133 
   151 
   134     private static void performC() {
   152     private static void performC() {
   135         // If JDK-7147084 is not fixed the loop is 5min long.
   153         // If JDK-7147084 is not fixed the loop is 5min long.
   136         for (int i = 0; i < 5*60; ++i) {
   154         for (int i = 0; i < 5 * 60; ++i) {
   137             try {
   155             try {
   138                 Thread.sleep(1000);
   156                 Thread.sleep(1000);
   139                 // check for sucess
       
   140                 if (stopC.exists())
       
   141                     break;
       
   142             } catch (InterruptedException ex) {
   157             } catch (InterruptedException ex) {
   143                 // that is ok. Longer sleep - better effect.
   158                 // that is ok. Longer sleep - better effect.
       
   159             }
       
   160             // check for success
       
   161             if (stopC.exists()) {
       
   162                 break;
   144             }
   163             }
   145         }
   164         }
   146     }
   165     }
   147 }
   166 }