test/java/lang/Runtime/exec/StreamsSurviveDestroy.java
Print this page
@@ -20,16 +20,17 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
- * @bug 4820217
+ * @bug 4820217 6860309
* @summary Ensure that pending reads on stdout and stderr streams
* return -1 when the process is destroyed
*/
import java.io.*;
+import java.util.concurrent.*;
public class StreamsSurviveDestroy {
private static class Copier extends Thread {
@@ -38,29 +39,32 @@
InputStream in;
OutputStream out;
boolean wantInterrupt;
boolean acceptException;
Exception exc = null;
+ CountDownLatch latch;
Copier(String name, InputStream in, OutputStream out,
- boolean ae, boolean wi)
+ boolean ae, boolean wi, CountDownLatch l)
{
this.name = name;
this.in = in;
this.out = out;
this.acceptException = ae;
this.wantInterrupt = wi;
+ this.latch = l;
setName(name);
start();
}
private void log(String s) {
System.err.println(" " + name + ": " + s);
}
public void run() {
byte[] buf = new byte[4242];
+ latch.countDown();
for (;;) {
try {
int n = in.read(buf);
if (n < 0) {
System.err.println(" EOF");
@@ -93,33 +97,41 @@
}
}
static void test() throws Exception {
+ CountDownLatch latch = new CountDownLatch(2);
+
System.err.println("test");
Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err,
- false, false);
+ false, false, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
- false, false);
- Thread.sleep(100);
+ false, false, latch);
+ latch.await(); // Wait till both Copiers about to read
+ Thread.sleep(100);// Give both Copiers a chance to start read
+
p.destroy();
System.err.println(" exit: " + p.waitFor());
cp1.join();
cp1.check();
cp2.join();
cp2.check();
}
static void testCloseBeforeDestroy() throws Exception {
+ CountDownLatch latch = new CountDownLatch(2);
+
System.err.println("testCloseBeforeDestroy");
Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err,
- true, false);
+ true, false, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
- true, false);
- Thread.sleep(100);
+ true, false, latch);
+ latch.await(); // Wait till both Copiers about to read
+ Thread.sleep(100);// Give both Copiers a chance to start read
+
p.getInputStream().close();
p.getErrorStream().close();
p.destroy();
System.err.println(" exit: " + p.waitFor());
cp1.join();
@@ -127,17 +139,21 @@
cp2.join();
cp2.check();
}
static void testCloseAfterDestroy() throws Exception {
+ CountDownLatch latch = new CountDownLatch(2);
System.err.println("testCloseAfterDestroy");
Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err,
- true, false);
+ true, false,latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
- true, false);
- Thread.sleep(100);
+ true, false, latch);
+
+ latch.await(); // Wait till both Copiers about to read
+ Thread.sleep(100);// Give both Copiers a chance to start read
+
p.destroy();
p.getInputStream().close();
p.getErrorStream().close();
System.err.println(" exit: " + p.waitFor());
cp1.join();
@@ -145,17 +161,20 @@
cp2.join();
cp2.check();
}
static void testInterrupt() throws Exception {
+ CountDownLatch latch = new CountDownLatch(2);
System.err.println("testInterrupt");
Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err,
- false, true);
+ false, true, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
- false, true);
- Thread.sleep(100);
+ false, true, latch);
+ latch.await(); // Wait till both Copiers about to read
+ Thread.sleep(100);// Give both Copiers a chance to start read
+
cp1.interrupt();
cp2.interrupt();
Thread.sleep(100);
p.destroy();
System.err.println(" exit: " + p.waitFor());
@@ -174,9 +193,7 @@
test();
testCloseBeforeDestroy();
testCloseAfterDestroy();
testInterrupt();
-
}
-
}