Path: blob/master/test/jdk/java/lang/ProcessBuilder/InheritIOEHandle.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 714708426* @run main/othervm InheritIOEHandle27* @summary inherit IOE handles and MS CreateProcess limitations (kb315939)28*/2930import java.io.BufferedReader;31import java.io.File;32import java.io.IOException;33import java.io.InputStreamReader;3435public class InheritIOEHandle {36private static enum APP {37A, B, C;38}3940private static File stopC = new File("StopC.txt");41private static String SIGNAL = "After call child process";42private static String JAVA_EXE = System.getProperty("java.home")43+ File.separator + "bin"44+ File.separator + "java";4546private static String[] getCommandArray(String processName) {47String[] cmdArray = {48JAVA_EXE,49"-cp",50System.getProperty("java.class.path"),51InheritIOEHandle.class.getName(),52processName53};54return cmdArray;55}5657public static void main(String[] args) throws Exception {58if (!System.getProperty("os.name").startsWith("Windows")) {59return;60}6162APP app = (args.length > 0) ? APP.valueOf(args[0]) : APP.A;63switch (app) {64case A:65performA();66break;67case B:68performB();69break;70case C:71performC();72break;73}74}7576private static void performA() {77try {78stopC.delete();7980ProcessBuilder builder = new ProcessBuilder(81getCommandArray(APP.B.name()));82builder.redirectErrorStream(true);8384Process process = builder.start();8586process.getOutputStream().close();87process.getErrorStream().close();8889boolean isSignalReceived = false;90try (BufferedReader in = new BufferedReader(new InputStreamReader(91process.getInputStream(), "utf-8")))92{93String result;94while ((result = in.readLine()) != null) {95if (SIGNAL.equals(result)) {96isSignalReceived = true;97break;98} else {99throw new RuntimeException("Catastrophe in process B! Bad output.");100}101}102103}104if (!isSignalReceived) {105throw new RuntimeException("Signal from B was not received");106}107108// If JDK-7147084 is not fixed that point is unreachable.109System.out.println("Received signal from B, creating file StopC");110// write signal file111boolean isFileStopC = stopC.createNewFile();112if (!isFileStopC) {113throw new RuntimeException("Signal file StopC.txt was not created. TEST or INFRA bug");114}115116process.waitFor();117118System.err.println("Read stream finished.");119} catch (IOException ex) {120throw new RuntimeException("Catastrophe in process A!", ex);121} catch (InterruptedException ex) {122throw new RuntimeException("A was interrupted while waiting for B", ex);123}124}125126private static void performB() {127try {128ProcessBuilder builder = new ProcessBuilder(129getCommandArray(APP.C.name()));130131Process process = builder.start();132133process.getInputStream().close();134process.getOutputStream().close();135process.getErrorStream().close();136137System.out.println(SIGNAL);138process.waitFor();139140// JDK-7147084 subject:141// Process C inherits the [System.out] handle and142// handle close in B does not finalize the streaming for A.143// (handle reference count > 1).144} catch (IOException ex) {145throw new RuntimeException("Catastrophe in process B!", ex);146} catch (InterruptedException ex) {147throw new RuntimeException("B was interrupted while waiting for C", ex);148}149}150151private static void performC() {152// If JDK-7147084 is not fixed the loop is 5min long.153for (int i = 0; i < 5 * 60; ++i) {154try {155Thread.sleep(1000);156} catch (InterruptedException ex) {157// that is ok. Longer sleep - better effect.158}159// check for success160if (stopC.exists()) {161break;162}163}164}165}166167168