Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/exec/StreamsSurviveDestroy.java
47209 views
/*1* Copyright (c) 2003, 2011, 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/* @test24* @bug 482021725* @summary Ensure that pending reads on stdout and stderr streams26* return -1 when the process is destroyed27*/2829import java.io.*;30import java.util.concurrent.*;313233public class StreamsSurviveDestroy {3435private static class Copier extends Thread {3637String name;38InputStream in;39OutputStream out;40boolean wantInterrupt;41boolean acceptException;42Exception exc = null;43CountDownLatch latch;4445Copier(String name, InputStream in, OutputStream out,46boolean ae, boolean wi, CountDownLatch l)47{48this.name = name;49this.in = in;50this.out = out;51this.acceptException = ae;52this.wantInterrupt = wi;53this.latch = l;54setName(name);55start();56}5758private void log(String s) {59System.err.println(" " + name + ": " + s);60}6162public void run() {63byte[] buf = new byte[4242];64latch.countDown();65for (;;) {66try {67int n = in.read(buf);68if (n < 0) {69System.err.println(" EOF");70break;71}72out.write(buf, 0, n);73} catch (IOException x) {74if (wantInterrupt) {75if (x instanceof InterruptedIOException) {76log("Interrupted as expected");77return;78}79exc = new Exception(name80+ ": Not interrupted as expected");81return;82}83exc = x;84if (acceptException) {85log("Thrown, but okay: " + x);86return;87}88return;89}90}91}9293public void check() throws Exception {94if (!acceptException && exc != null)95throw new Exception(name + ": Exception thrown", exc);96}9798}99100static void test() throws Exception {101CountDownLatch latch = new CountDownLatch(2);102103System.err.println("test");104Process p = Runtime.getRuntime().exec("/bin/cat");105Copier cp1 = new Copier("out", p.getInputStream(), System.err,106false, false, latch);107Copier cp2 = new Copier("err", p.getErrorStream(), System.err,108false, false, latch);109latch.await(); // Wait till both Copiers about to read110Thread.sleep(100);// Give both Copiers a chance to start read111112p.destroy();113System.err.println(" exit: " + p.waitFor());114cp1.join();115cp1.check();116cp2.join();117cp2.check();118}119120static void testCloseBeforeDestroy() throws Exception {121CountDownLatch latch = new CountDownLatch(2);122123System.err.println("testCloseBeforeDestroy");124Process p = Runtime.getRuntime().exec("/bin/cat");125Copier cp1 = new Copier("out", p.getInputStream(), System.err,126true, false, latch);127Copier cp2 = new Copier("err", p.getErrorStream(), System.err,128true, false, latch);129latch.await(); // Wait till both Copiers about to read130Thread.sleep(100);// Give both Copiers a chance to start read131132p.getInputStream().close();133p.getErrorStream().close();134p.destroy();135System.err.println(" exit: " + p.waitFor());136cp1.join();137cp1.check();138cp2.join();139cp2.check();140}141142static void testCloseAfterDestroy() throws Exception {143CountDownLatch latch = new CountDownLatch(2);144System.err.println("testCloseAfterDestroy");145Process p = Runtime.getRuntime().exec("/bin/cat");146Copier cp1 = new Copier("out", p.getInputStream(), System.err,147true, false,latch);148Copier cp2 = new Copier("err", p.getErrorStream(), System.err,149true, false, latch);150151latch.await(); // Wait till both Copiers about to read152Thread.sleep(100);// Give both Copiers a chance to start read153154p.destroy();155p.getInputStream().close();156p.getErrorStream().close();157System.err.println(" exit: " + p.waitFor());158cp1.join();159cp1.check();160cp2.join();161cp2.check();162}163164static void testInterrupt() throws Exception {165CountDownLatch latch = new CountDownLatch(2);166System.err.println("testInterrupt");167Process p = Runtime.getRuntime().exec("/bin/cat");168Copier cp1 = new Copier("out", p.getInputStream(), System.err,169false, true, latch);170Copier cp2 = new Copier("err", p.getErrorStream(), System.err,171false, true, latch);172latch.await(); // Wait till both Copiers about to read173Thread.sleep(100);// Give both Copiers a chance to start read174175cp1.interrupt();176cp2.interrupt();177Thread.sleep(100);178p.destroy();179System.err.println(" exit: " + p.waitFor());180cp1.join();181cp1.check();182cp2.join();183cp2.check();184}185186public static void main(String[] args) throws Exception {187188// Applies only to Solaris; Linux and Windows189// behave a little differently190if (!System.getProperty("os.name").equals("SunOS"))191return;192193test();194testCloseBeforeDestroy();195testCloseAfterDestroy();196testInterrupt();197}198}199200201