test/java/lang/Runtime/exec/StreamsSurviveDestroy.java

Print this page




  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 /* @test
  25  * @bug 4820217
  26  * @summary Ensure that pending reads on stdout and stderr streams
  27  *          return -1 when the process is destroyed
  28  */
  29 
  30 import java.io.*;
  31 import java.util.concurrent.*;
  32 
  33 
  34 public class StreamsSurviveDestroy {
  35 
  36     private static class Copier extends Thread {
  37 
  38         String name;
  39         InputStream in;
  40         OutputStream out;
  41         boolean wantInterrupt;
  42         boolean acceptException;
  43         Exception exc = null;
  44         CountDownLatch latch;
  45 
  46         Copier(String name, InputStream in, OutputStream out,
  47                boolean ae, boolean wi, CountDownLatch l)
  48         {
  49             this.name = name;
  50             this.in = in;
  51             this.out = out;
  52             this.acceptException = ae;
  53             this.wantInterrupt = wi;


  85                     if (acceptException) {
  86                         log("Thrown, but okay: " + x);
  87                         return;
  88                     }
  89                     return;
  90                 }
  91             }
  92         }
  93 
  94         public void check() throws Exception {
  95             if (!acceptException && exc != null)
  96                 throw new Exception(name + ": Exception thrown", exc);
  97         }
  98 
  99     }
 100 
 101     static void test() throws Exception {
 102         CountDownLatch latch = new CountDownLatch(2);
 103 
 104         System.err.println("test");
 105         Process p = Runtime.getRuntime().exec("/bin/cat");
 106         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 107                                 false, false, latch);
 108         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 109                                 false, false, latch);
 110         latch.await();    // Wait till both Copiers about to read
 111         Thread.sleep(100);// Give both Copiers a chance to start read
 112 
 113         p.destroy();
 114         System.err.println("  exit: " + p.waitFor());
 115         cp1.join();
 116         cp1.check();
 117         cp2.join();
 118         cp2.check();
 119     }
 120 
 121     static void testCloseBeforeDestroy() throws Exception {
 122         CountDownLatch latch = new CountDownLatch(2);
 123 
 124         System.err.println("testCloseBeforeDestroy");
 125         Process p = Runtime.getRuntime().exec("/bin/cat");
 126         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 127                                 true, false, latch);
 128         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 129                                 true, false, latch);
 130         latch.await();    // Wait till both Copiers about to read
 131         Thread.sleep(100);// Give both Copiers a chance to start read
 132 
 133         p.getInputStream().close();
 134         p.getErrorStream().close();
 135         p.destroy();
 136         System.err.println("  exit: " + p.waitFor());
 137         cp1.join();
 138         cp1.check();
 139         cp2.join();
 140         cp2.check();
 141     }
 142 
 143     static void testCloseAfterDestroy() throws Exception {
 144         CountDownLatch latch = new CountDownLatch(2);
 145         System.err.println("testCloseAfterDestroy");
 146         Process p = Runtime.getRuntime().exec("/bin/cat");
 147         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 148                                 true, false,latch);
 149         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 150                                 true, false, latch);
 151 
 152         latch.await();    // Wait till both Copiers about to read
 153         Thread.sleep(100);// Give both Copiers a chance to start read
 154 
 155         p.destroy();
 156         p.getInputStream().close();
 157         p.getErrorStream().close();
 158         System.err.println("  exit: " + p.waitFor());
 159         cp1.join();
 160         cp1.check();
 161         cp2.join();
 162         cp2.check();
 163     }
 164 
 165     static void testInterrupt() throws Exception {
 166         CountDownLatch latch = new CountDownLatch(2);
 167         System.err.println("testInterrupt");
 168         Process p = Runtime.getRuntime().exec("/bin/cat");
 169         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 170                                 false, true, latch);
 171         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 172                                 false, true, latch);
 173         latch.await();    // Wait till both Copiers about to read
 174         Thread.sleep(100);// Give both Copiers a chance to start read
 175 
 176         cp1.interrupt();
 177         cp2.interrupt();
 178         Thread.sleep(100);
 179         p.destroy();
 180         System.err.println("  exit: " + p.waitFor());
 181         cp1.join();
 182         cp1.check();
 183         cp2.join();
 184         cp2.check();
 185     }
 186 
 187     public static void main(String[] args) throws Exception {
 188 
 189         // Applies only to Solaris; Linux and Windows
 190         // behave a little differently
 191         if (!System.getProperty("os.name").equals("SunOS"))

 192             return;

 193 
 194         test();
 195         testCloseBeforeDestroy();
 196         testCloseAfterDestroy();
 197         testInterrupt();
 198     }
 199 }


  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 /* @test
  25  * @bug 4820217
  26  * @summary Ensure that pending reads on stdout and stderr streams
  27  *          return -1 when the process is destroyed
  28  */
  29 
  30 import java.io.*;
  31 import java.util.concurrent.*;
  32 

  33 public class StreamsSurviveDestroy {
  34 
  35     private static class Copier extends Thread {
  36 
  37         String name;
  38         InputStream in;
  39         OutputStream out;
  40         boolean wantInterrupt;
  41         boolean acceptException;
  42         Exception exc = null;
  43         CountDownLatch latch;
  44 
  45         Copier(String name, InputStream in, OutputStream out,
  46                boolean ae, boolean wi, CountDownLatch l)
  47         {
  48             this.name = name;
  49             this.in = in;
  50             this.out = out;
  51             this.acceptException = ae;
  52             this.wantInterrupt = wi;


  84                     if (acceptException) {
  85                         log("Thrown, but okay: " + x);
  86                         return;
  87                     }
  88                     return;
  89                 }
  90             }
  91         }
  92 
  93         public void check() throws Exception {
  94             if (!acceptException && exc != null)
  95                 throw new Exception(name + ": Exception thrown", exc);
  96         }
  97 
  98     }
  99 
 100     static void test() throws Exception {
 101         CountDownLatch latch = new CountDownLatch(2);
 102 
 103         System.err.println("test");
 104         Process p = Runtime.getRuntime().exec(UnixCommands.cat());
 105         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 106                                 false, false, latch);
 107         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 108                                 false, false, latch);
 109         latch.await();    // Wait till both Copiers about to read
 110         Thread.sleep(100);// Give both Copiers a chance to start read
 111 
 112         p.destroy();
 113         System.err.println("  exit: " + p.waitFor());
 114         cp1.join();
 115         cp1.check();
 116         cp2.join();
 117         cp2.check();
 118     }
 119 
 120     static void testCloseBeforeDestroy() throws Exception {
 121         CountDownLatch latch = new CountDownLatch(2);
 122 
 123         System.err.println("testCloseBeforeDestroy");
 124         Process p = Runtime.getRuntime().exec(UnixCommands.cat());
 125         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 126                                 true, false, latch);
 127         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 128                                 true, false, latch);
 129         latch.await();    // Wait till both Copiers about to read
 130         Thread.sleep(100);// Give both Copiers a chance to start read
 131 
 132         p.getInputStream().close();
 133         p.getErrorStream().close();
 134         p.destroy();
 135         System.err.println("  exit: " + p.waitFor());
 136         cp1.join();
 137         cp1.check();
 138         cp2.join();
 139         cp2.check();
 140     }
 141 
 142     static void testCloseAfterDestroy() throws Exception {
 143         CountDownLatch latch = new CountDownLatch(2);
 144         System.err.println("testCloseAfterDestroy");
 145         Process p = Runtime.getRuntime().exec(UnixCommands.cat());
 146         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 147                                 true, false,latch);
 148         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 149                                 true, false, latch);
 150 
 151         latch.await();    // Wait till both Copiers about to read
 152         Thread.sleep(100);// Give both Copiers a chance to start read
 153 
 154         p.destroy();
 155         p.getInputStream().close();
 156         p.getErrorStream().close();
 157         System.err.println("  exit: " + p.waitFor());
 158         cp1.join();
 159         cp1.check();
 160         cp2.join();
 161         cp2.check();
 162     }
 163 
 164     static void testInterrupt() throws Exception {
 165         CountDownLatch latch = new CountDownLatch(2);
 166         System.err.println("testInterrupt");
 167         Process p = Runtime.getRuntime().exec(UnixCommands.cat());
 168         Copier cp1 = new Copier("out", p.getInputStream(), System.err,
 169                                 false, true, latch);
 170         Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
 171                                 false, true, latch);
 172         latch.await();    // Wait till both Copiers about to read
 173         Thread.sleep(100);// Give both Copiers a chance to start read
 174 
 175         cp1.interrupt();
 176         cp2.interrupt();
 177         Thread.sleep(100);
 178         p.destroy();
 179         System.err.println("  exit: " + p.waitFor());
 180         cp1.join();
 181         cp1.check();
 182         cp2.join();
 183         cp2.check();
 184     }
 185 
 186     public static void main(String[] args) throws Exception {
 187 
 188         // Applies only to Solaris; Linux and Windows
 189         // behave a little differently
 190         if (!System.getProperty("os.name").equals("SunOS")) {
 191             System.err.println("For SunOS only");
 192             return;
 193         }
 194 
 195         test();
 196         testCloseBeforeDestroy();
 197         testCloseAfterDestroy();
 198         testInterrupt();
 199     }
 200 }