6860309: TEST_BUG: Insufficient sleep time in java/lang/Runtime/exec/StreamsSurviveDestroy.java
authoralanb
Sat, 19 Nov 2011 20:03:00 +0000
changeset 11029 377f04adbe18
parent 11028 937190393a12
child 11030 4c787684c8eb
6860309: TEST_BUG: Insufficient sleep time in java/lang/Runtime/exec/StreamsSurviveDestroy.java Reviewed-by: alanb, dholmes, forax Contributed-by: gary.adams@oracle.com
jdk/test/java/lang/Runtime/exec/StreamsSurviveDestroy.java
--- a/jdk/test/java/lang/Runtime/exec/StreamsSurviveDestroy.java	Sat Nov 19 19:59:33 2011 +0000
+++ b/jdk/test/java/lang/Runtime/exec/StreamsSurviveDestroy.java	Sat Nov 19 20:03:00 2011 +0000
@@ -28,6 +28,7 @@
  */
 
 import java.io.*;
+import java.util.concurrent.*;
 
 
 public class StreamsSurviveDestroy {
@@ -40,15 +41,17 @@
         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();
         }
@@ -59,6 +62,7 @@
 
         public void run() {
             byte[] buf = new byte[4242];
+            latch.countDown();
             for (;;) {
                 try {
                     int n = in.read(buf);
@@ -95,13 +99,17 @@
     }
 
     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();
@@ -111,13 +119,17 @@
     }
 
     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();
@@ -129,13 +141,17 @@
     }
 
     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();
@@ -147,13 +163,16 @@
     }
 
     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);
@@ -176,7 +195,5 @@
         testCloseBeforeDestroy();
         testCloseAfterDestroy();
         testInterrupt();
-
     }
-
 }