8228625: [TESTBUG] sun/tools/jhsdb/JShellHeapDumpTest.java fails with RuntimeException 'JShellToolProvider' missing from stdout/stderr
authorcjplummer
Thu, 26 Sep 2019 14:41:09 -0700
changeset 58364 2c87d191056c
parent 58363 cf7c6f013d6d
child 58365 73950479184b
8228625: [TESTBUG] sun/tools/jhsdb/JShellHeapDumpTest.java fails with RuntimeException 'JShellToolProvider' missing from stdout/stderr Summary: Give jshell process time to fully startup and stablize before requesting heap dump Reviewed-by: sspitsyn, amenkov
test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java
--- a/test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java	Thu Sep 26 12:13:57 2019 -0700
+++ b/test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java	Thu Sep 26 14:41:09 2019 -0700
@@ -39,6 +39,7 @@
 import java.util.Map;
 
 import jdk.test.lib.JDKToolLauncher;
+import jdk.test.lib.JDKToolFinder;
 import jdk.test.lib.process.OutputAnalyzer;
 import jdk.test.lib.process.ProcessTools;
 import jdk.test.lib.hprof.parser.HprofReader;
@@ -47,40 +48,43 @@
 
 public class JShellHeapDumpTest {
 
-    protected static Process process;
-
-    private static long pid;
+    protected static Process jShellProcess;
 
     public static void launch(String expectedMessage, List<String> toolArgs)
         throws IOException {
 
         try {
             launchJshell();
+            long jShellPID = jShellProcess.pid();
 
-            System.out.println("Starting " + toolArgs.get(0) + " against " + pid);
+            System.out.println("Starting " + toolArgs.get(0) + " against " + jShellPID);
             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
 
             for (String cmd : toolArgs) {
                 launcher.addToolArg(cmd);
             }
 
-            launcher.addToolArg("--pid=" + Long.toString(pid));
+            launcher.addToolArg("--pid=" + Long.toString(jShellPID));
 
             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
-            processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
-            System.out.println("stdout:");
+            System.out.println("jhsdb jmap stdout:");
             System.out.println(output.getStdout());
-            System.out.println("stderr:");
+            System.out.println("jhsdb jmap stderr:");
             System.out.println(output.getStderr());
+            System.out.println("###### End of all output:");
             output.shouldNotContain("null");
             output.shouldHaveExitValue(0);
         } catch (Exception ex) {
             throw new RuntimeException("Test ERROR " + ex, ex);
         } finally {
-           if (process.isAlive()) {
-             process.destroy();
-           }
+            if (jShellProcess.isAlive()) {
+                System.out.println("Destroying jshell");
+                jShellProcess.destroy();
+                System.out.println("Jshell destroyed");
+            } else {
+                System.out.println("Jshell not alive");
+            }
         }
     }
 
@@ -102,38 +106,45 @@
     }
 
     public static void testHeapDump() throws IOException {
-        File dump = new File("jhsdb.jmap.heap." +
+        File hprofFile = new File("jhsdb.jmap.heap." +
                              System.currentTimeMillis() + ".hprof");
-        if (dump.exists()) {
-            dump.delete();
+        if (hprofFile.exists()) {
+            hprofFile.delete();
         }
 
         launch("heap written to", "jmap",
-               "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
+               "--binaryheap", "--dumpfile=" + hprofFile.getAbsolutePath());
+
+        assertTrue(hprofFile.exists() && hprofFile.isFile(),
+                   "Could not create dump file " + hprofFile.getAbsolutePath());
 
-        assertTrue(dump.exists() && dump.isFile(),
-                   "Could not create dump file " + dump.getAbsolutePath());
+        printStackTraces(hprofFile.getAbsolutePath());
 
-        printStackTraces(dump.getAbsolutePath());
-
-        dump.delete();
+        System.out.println("hprof file size: " + hprofFile.length());
+        hprofFile.delete();
     }
 
     public static void launchJshell() throws IOException {
         System.out.println("Starting Jshell");
-        String jdkPath = System.getProperty("test.jdk");
-        if (jdkPath == null) {
-          // we are not under jtreg, try env
-            Map<String, String> env = System.getenv();
-            jdkPath = env.get("TESTJAVA");
+        long startTime = System.currentTimeMillis();
+        try {
+            ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));
+            jShellProcess = ProcessTools.startProcess("JShell", pb,
+                                                      s -> {  // warm-up predicate
+                                                          return s.contains("Welcome to JShell");
+                                                      });
+        } catch (Exception ex) {
+            throw new RuntimeException("Test ERROR " + ex, ex);
         }
-        if (jdkPath == null) {
-            throw new RuntimeException("Can't determine jdk path neither test.jdk property no TESTJAVA env are set");
+
+        long elapsedTime = System.currentTimeMillis() - startTime;
+        System.out.println("Jshell Started in " + elapsedTime + "ms");
+
+        // Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
+        try {
+            Thread.sleep(2000);
+        } catch (Exception e) {
         }
-        String osname = System.getProperty("os.name");
-        String jshell = jdkPath + ((osname.startsWith("window")) ? "/bin/jshell.exe" : "/bin/jshell");
-        process = Runtime.getRuntime().exec(jshell);
-        pid = process.pid();
     }
 
     public static void main(String[] args) throws Exception {