提交 d9671322 编写于 作者: A alanb

6860309: TEST_BUG: Insufficient sleep time in java/lang/Runtime/exec/StreamsSurviveDestroy.java

Reviewed-by: alanb, dholmes, forax
Contributed-by: gary.adams@oracle.com
上级 9ace6f0c
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
*/ */
import java.io.*; import java.io.*;
import java.util.concurrent.*;
public class StreamsSurviveDestroy { public class StreamsSurviveDestroy {
...@@ -40,15 +41,17 @@ public class StreamsSurviveDestroy { ...@@ -40,15 +41,17 @@ public class StreamsSurviveDestroy {
boolean wantInterrupt; boolean wantInterrupt;
boolean acceptException; boolean acceptException;
Exception exc = null; Exception exc = null;
CountDownLatch latch;
Copier(String name, InputStream in, OutputStream out, Copier(String name, InputStream in, OutputStream out,
boolean ae, boolean wi) boolean ae, boolean wi, CountDownLatch l)
{ {
this.name = name; this.name = name;
this.in = in; this.in = in;
this.out = out; this.out = out;
this.acceptException = ae; this.acceptException = ae;
this.wantInterrupt = wi; this.wantInterrupt = wi;
this.latch = l;
setName(name); setName(name);
start(); start();
} }
...@@ -59,6 +62,7 @@ public class StreamsSurviveDestroy { ...@@ -59,6 +62,7 @@ public class StreamsSurviveDestroy {
public void run() { public void run() {
byte[] buf = new byte[4242]; byte[] buf = new byte[4242];
latch.countDown();
for (;;) { for (;;) {
try { try {
int n = in.read(buf); int n = in.read(buf);
...@@ -95,13 +99,17 @@ public class StreamsSurviveDestroy { ...@@ -95,13 +99,17 @@ public class StreamsSurviveDestroy {
} }
static void test() throws Exception { static void test() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
System.err.println("test"); System.err.println("test");
Process p = Runtime.getRuntime().exec("/bin/cat"); Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err, Copier cp1 = new Copier("out", p.getInputStream(), System.err,
false, false); false, false, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err, Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
false, false); false, false, latch);
Thread.sleep(100); latch.await(); // Wait till both Copiers about to read
Thread.sleep(100);// Give both Copiers a chance to start read
p.destroy(); p.destroy();
System.err.println(" exit: " + p.waitFor()); System.err.println(" exit: " + p.waitFor());
cp1.join(); cp1.join();
...@@ -111,13 +119,17 @@ public class StreamsSurviveDestroy { ...@@ -111,13 +119,17 @@ public class StreamsSurviveDestroy {
} }
static void testCloseBeforeDestroy() throws Exception { static void testCloseBeforeDestroy() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
System.err.println("testCloseBeforeDestroy"); System.err.println("testCloseBeforeDestroy");
Process p = Runtime.getRuntime().exec("/bin/cat"); Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err, Copier cp1 = new Copier("out", p.getInputStream(), System.err,
true, false); true, false, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err, Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
true, false); true, false, latch);
Thread.sleep(100); latch.await(); // Wait till both Copiers about to read
Thread.sleep(100);// Give both Copiers a chance to start read
p.getInputStream().close(); p.getInputStream().close();
p.getErrorStream().close(); p.getErrorStream().close();
p.destroy(); p.destroy();
...@@ -129,13 +141,17 @@ public class StreamsSurviveDestroy { ...@@ -129,13 +141,17 @@ public class StreamsSurviveDestroy {
} }
static void testCloseAfterDestroy() throws Exception { static void testCloseAfterDestroy() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
System.err.println("testCloseAfterDestroy"); System.err.println("testCloseAfterDestroy");
Process p = Runtime.getRuntime().exec("/bin/cat"); Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err, Copier cp1 = new Copier("out", p.getInputStream(), System.err,
true, false); true, false,latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err, Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
true, false); true, false, latch);
Thread.sleep(100);
latch.await(); // Wait till both Copiers about to read
Thread.sleep(100);// Give both Copiers a chance to start read
p.destroy(); p.destroy();
p.getInputStream().close(); p.getInputStream().close();
p.getErrorStream().close(); p.getErrorStream().close();
...@@ -147,13 +163,16 @@ public class StreamsSurviveDestroy { ...@@ -147,13 +163,16 @@ public class StreamsSurviveDestroy {
} }
static void testInterrupt() throws Exception { static void testInterrupt() throws Exception {
CountDownLatch latch = new CountDownLatch(2);
System.err.println("testInterrupt"); System.err.println("testInterrupt");
Process p = Runtime.getRuntime().exec("/bin/cat"); Process p = Runtime.getRuntime().exec("/bin/cat");
Copier cp1 = new Copier("out", p.getInputStream(), System.err, Copier cp1 = new Copier("out", p.getInputStream(), System.err,
false, true); false, true, latch);
Copier cp2 = new Copier("err", p.getErrorStream(), System.err, Copier cp2 = new Copier("err", p.getErrorStream(), System.err,
false, true); false, true, latch);
Thread.sleep(100); latch.await(); // Wait till both Copiers about to read
Thread.sleep(100);// Give both Copiers a chance to start read
cp1.interrupt(); cp1.interrupt();
cp2.interrupt(); cp2.interrupt();
Thread.sleep(100); Thread.sleep(100);
...@@ -176,7 +195,5 @@ public class StreamsSurviveDestroy { ...@@ -176,7 +195,5 @@ public class StreamsSurviveDestroy {
testCloseBeforeDestroy(); testCloseBeforeDestroy();
testCloseAfterDestroy(); testCloseAfterDestroy();
testInterrupt(); testInterrupt();
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册