8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
authorjbachorik
Wed, 13 Nov 2013 13:12:16 +0100
changeset 21660 3de47f8320ef
parent 21659 3b80f3e5b552
child 21661 e6b6a0b1fc26
8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently Reviewed-by: dholmes, ykantser
jdk/test/com/sun/jdi/BadHandshakeTest.java
--- a/jdk/test/com/sun/jdi/BadHandshakeTest.java	Wed Nov 13 10:50:30 2013 +0100
+++ b/jdk/test/com/sun/jdi/BadHandshakeTest.java	Wed Nov 13 13:12:16 2013 +0100
@@ -24,16 +24,12 @@
 /* @test
  * @bug 6306165 6432567
  * @summary Check that a bad handshake doesn't cause a debuggee to abort
+ * @library /lib/testlibrary
  *
  * @build VMConnection BadHandshakeTest Exit0
  * @run main BadHandshakeTest
  *
  */
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.BufferedInputStream;
-import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.InetAddress;
 import com.sun.jdi.Bootstrap;
@@ -44,50 +40,13 @@
 import java.util.Map;
 import java.util.List;
 import java.util.Iterator;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import jdk.testlibrary.Utils;
+import jdk.testlibrary.ProcessTools;
 
 public class BadHandshakeTest {
-
-    static volatile boolean ready = false;
-
-    /*
-     * Helper class to redirect process output/error
-     */
-    static class IOHandler implements Runnable {
-        InputStream in;
-
-        IOHandler(InputStream in) {
-            this.in = in;
-        }
-
-        static void handle(InputStream in) {
-            IOHandler handler = new IOHandler(in);
-            Thread thr = new Thread(handler);
-            thr.setDaemon(true);
-            thr.start();
-        }
-
-        public void run() {
-            try {
-                byte b[] = new byte[100];
-                for (;;) {
-                    int n = in.read(b, 0, 100);
-                    // The first thing that will get read is
-                    //    Listening for transport dt_socket at address: xxxxx
-                    // which shows the debuggee is ready to accept connections.
-                    ready = true;
-                    if (n < 0) {
-                        break;
-                    }
-                    String s = new String(b, 0, n, "UTF-8");
-                    System.out.print(s);
-                }
-            } catch (IOException ioe) {
-                ioe.printStackTrace();
-            }
-        }
-
-    }
-
     /*
      * Find a connector by name
      */
@@ -106,22 +65,31 @@
     /*
      * Launch a server debuggee with the given address
      */
-    private static Process launch(String address, String class_name) throws IOException {
-        String exe =   System.getProperty("java.home")
-                     + File.separator + "bin" + File.separator + "java";
-        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
-            " -agentlib:jdwp=transport=dt_socket" +
-            ",server=y" + ",suspend=y" + ",address=" + address +
-            " " + class_name;
+    private static Process launch(String address, String class_name) throws Exception {
+        String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
+            "-agentlib:jdwp=transport=dt_socket" +
+            ",server=y" + ",suspend=y" + ",address=" + address,
+            class_name
+        });
+
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
 
-        System.out.println("Starting: " + cmd);
-
-        Process p = Runtime.getRuntime().exec(cmd);
+        final AtomicBoolean success = new AtomicBoolean();
+        Process p = ProcessTools.startProcess(
+            class_name,
+            pb,
+            (line) -> {
+                // The first thing that will get read is
+                //    Listening for transport dt_socket at address: xxxxx
+                // which shows the debuggee is ready to accept connections.
+                success.set(line.contains("Listening for transport dt_socket at address:"));
+                return true;
+            },
+            1500,
+            TimeUnit.MILLISECONDS
+        );
 
-        IOHandler.handle(p.getInputStream());
-        IOHandler.handle(p.getErrorStream());
-
-        return p;
+        return success.get() ? p : null;
     }
 
     /*
@@ -131,23 +99,14 @@
      * - verify we saw no error
      */
     public static void main(String args[]) throws Exception {
-        // find a free port
-        ServerSocket ss = new ServerSocket(0);
-        int port = ss.getLocalPort();
-        ss.close();
+        int port = Utils.getFreePort();
 
         String address = String.valueOf(port);
 
         // launch the server debuggee
         Process process = launch(address, "Exit0");
-
-        // wait for the debugge to be ready
-        while (!ready) {
-            try {
-                Thread.sleep(1000);
-            } catch(Exception ee) {
-                throw ee;
-            }
+        if (process == null) {
+            throw new RuntimeException("Unable to start debugee");
         }
 
         // Connect to the debuggee and handshake with garbage