8054066: com/sun/jdi/DoubleAgentTest.java fails with timeout
authordsamersoff
Thu, 28 Aug 2014 05:04:58 -0700
changeset 26222 314e94fc6805
parent 26221 47fd8b933a5c
child 26223 3830bfd8440a
8054066: com/sun/jdi/DoubleAgentTest.java fails with timeout Summary: The test chaged to use testlibrary Reviewed-by: sla
jdk/test/com/sun/jdi/DoubleAgentTest.java
--- a/jdk/test/com/sun/jdi/DoubleAgentTest.java	Thu Aug 28 10:17:55 2014 +0100
+++ b/jdk/test/com/sun/jdi/DoubleAgentTest.java	Thu Aug 28 05:04:58 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,136 +21,39 @@
  * questions.
  */
 
+import jdk.testlibrary.OutputAnalyzer;
+import jdk.testlibrary.ProcessTools;
+import jdk.testlibrary.Utils;
+
 /* @test
  * @bug 6354345
- * @summary Check that a double agent request fails
- *
- * @build VMConnection DoubleAgentTest Exit0
- * @run driver DoubleAgentTest
+ * @summary Check that multiple -agentlib statements in command line fails
  *
+ * @library /lib/testlibrary
+ * @build jdk.testlibarary.*
+ * @build DoubleAgentTest Exit0
+ * @run driver DoubleAgentTest
  */
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.net.ServerSocket;
 
 public class DoubleAgentTest {
 
-    static Object locker = new Object();
-    static String outputText = "";
-
-    /*
-     * Helper class to redirect process output/error
-     */
-    static class IOHandler implements Runnable {
-        InputStream in;
-
-        IOHandler(InputStream in) {
-            this.in = in;
-        }
-
-        static Thread handle(InputStream in) {
-            IOHandler handler = new IOHandler(in);
-            Thread thr = new Thread(handler);
-            thr.setDaemon(true);
-            thr.start();
-            return thr;
-        }
+    private static final String TEST_CLASSES = System.getProperty(
+            "test.classes", ".");
 
-        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.
-                    synchronized(locker) {
-                        locker.notify();
-                    }
-                    if (n < 0) {
-                        break;
-                    }
-                    String s = new String(b, 0, n, "UTF-8");
-                    System.out.print(s);
-                    synchronized(outputText) {
-                        outputText += s;
-                    }
-                }
-            } catch (IOException ioe) {
-                ioe.printStackTrace();
-            }
-        }
+    public static void main(String[] args) throws Throwable {
+        int port = Utils.getFreePort();
 
-    }
-
-    /*
-     * 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 jdwpOption = "-agentlib:jdwp=transport=dt_socket"
-                         + ",server=y" + ",suspend=y" + ",address=" + address;
-        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions()
-                         + " " + jdwpOption
-                         + " " + jdwpOption
-                         + " " + class_name;
-
-        System.out.println("Starting: " + cmd);
-
-        Process p = Runtime.getRuntime().exec(cmd);
-
-        return p;
-    }
-
-    /*
-     * - pick a TCP port
-     * - Launch a server debuggee that should fail
-     * - verify we saw error
-     */
-    public static void main(String args[]) throws Exception {
-        // find a free port
-        ServerSocket ss = new ServerSocket(0);
-        int port = ss.getLocalPort();
-        ss.close();
-
-        String address = String.valueOf(port);
+                         + ",server=y" + ",suspend=n" + ",address=*:" + String.valueOf(port);
 
-        // launch the server debuggee
-        Process process = launch(address, "Exit0");
-        Thread t1 = IOHandler.handle(process.getInputStream());
-        Thread t2 = IOHandler.handle(process.getErrorStream());
-
-        // wait for the debugge to be ready
-        synchronized(locker) {
-            locker.wait();
-        }
+        OutputAnalyzer output = ProcessTools.executeTestJvm("-classpath",
+                TEST_CLASSES,
+                jdwpOption, // Notice jdwpOption specified twice
+                jdwpOption,
+                "Exit0");
 
-        int exitCode = process.waitFor();
-        try {
-            t1.join();
-            t2.join();
-        } catch ( InterruptedException e ) {
-            e.printStackTrace();
-            throw new Exception("Debuggee failed InterruptedException");
-        }
-
-        if ( outputText.contains("capabilities") ) {
-            throw new Exception(
-                "Debuggee failed with ERROR about capabilities: " + outputText);
-        }
-
-        if ( !outputText.contains("ERROR") ) {
-            throw new Exception(
-                "Debuggee does not have ERROR in the output: " + outputText);
-        }
-
-        if ( exitCode == 0 ) {
-            throw new Exception(
-                "Debuggee should have failed with an non-zero exit code");
-        }
-
+        output.shouldContain("Cannot load this JVM TI agent twice");
+        output.shouldHaveExitValue(1);
     }
 
 }