Skip to content

Commit 6212eb3

Browse files
committed
8228625: [TESTBUG] sun/tools/jhsdb/JShellHeapDumpTest.java fails with RuntimeException 'JShellToolProvider' missing from stdout/stderr
Give jshell process time to fully startup and stablize before requesting heap dump Reviewed-by: sspitsyn, amenkov
1 parent b03fbff commit 6212eb3

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040

4141
import jdk.test.lib.JDKToolLauncher;
42+
import jdk.test.lib.JDKToolFinder;
4243
import jdk.test.lib.process.OutputAnalyzer;
4344
import jdk.test.lib.process.ProcessTools;
4445
import jdk.test.lib.hprof.parser.HprofReader;
@@ -47,40 +48,43 @@
4748

4849
public class JShellHeapDumpTest {
4950

50-
protected static Process process;
51-
52-
private static long pid;
51+
protected static Process jShellProcess;
5352

5453
public static void launch(String expectedMessage, List<String> toolArgs)
5554
throws IOException {
5655

5756
try {
5857
launchJshell();
58+
long jShellPID = jShellProcess.pid();
5959

60-
System.out.println("Starting " + toolArgs.get(0) + " against " + pid);
60+
System.out.println("Starting " + toolArgs.get(0) + " against " + jShellPID);
6161
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
6262

6363
for (String cmd : toolArgs) {
6464
launcher.addToolArg(cmd);
6565
}
6666

67-
launcher.addToolArg("--pid=" + Long.toString(pid));
67+
launcher.addToolArg("--pid=" + Long.toString(jShellPID));
6868

6969
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
70-
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
7170
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
72-
System.out.println("stdout:");
71+
System.out.println("jhsdb jmap stdout:");
7372
System.out.println(output.getStdout());
74-
System.out.println("stderr:");
73+
System.out.println("jhsdb jmap stderr:");
7574
System.out.println(output.getStderr());
75+
System.out.println("###### End of all output:");
7676
output.shouldNotContain("null");
7777
output.shouldHaveExitValue(0);
7878
} catch (Exception ex) {
7979
throw new RuntimeException("Test ERROR " + ex, ex);
8080
} finally {
81-
if (process.isAlive()) {
82-
process.destroy();
83-
}
81+
if (jShellProcess.isAlive()) {
82+
System.out.println("Destroying jshell");
83+
jShellProcess.destroy();
84+
System.out.println("Jshell destroyed");
85+
} else {
86+
System.out.println("Jshell not alive");
87+
}
8488
}
8589
}
8690

@@ -102,38 +106,45 @@ public static void printStackTraces(String file) throws IOException {
102106
}
103107

104108
public static void testHeapDump() throws IOException {
105-
File dump = new File("jhsdb.jmap.heap." +
109+
File hprofFile = new File("jhsdb.jmap.heap." +
106110
System.currentTimeMillis() + ".hprof");
107-
if (dump.exists()) {
108-
dump.delete();
111+
if (hprofFile.exists()) {
112+
hprofFile.delete();
109113
}
110114

111115
launch("heap written to", "jmap",
112-
"--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
116+
"--binaryheap", "--dumpfile=" + hprofFile.getAbsolutePath());
113117

114-
assertTrue(dump.exists() && dump.isFile(),
115-
"Could not create dump file " + dump.getAbsolutePath());
118+
assertTrue(hprofFile.exists() && hprofFile.isFile(),
119+
"Could not create dump file " + hprofFile.getAbsolutePath());
116120

117-
printStackTraces(dump.getAbsolutePath());
121+
printStackTraces(hprofFile.getAbsolutePath());
118122

119-
dump.delete();
123+
System.out.println("hprof file size: " + hprofFile.length());
124+
hprofFile.delete();
120125
}
121126

122127
public static void launchJshell() throws IOException {
123128
System.out.println("Starting Jshell");
124-
String jdkPath = System.getProperty("test.jdk");
125-
if (jdkPath == null) {
126-
// we are not under jtreg, try env
127-
Map<String, String> env = System.getenv();
128-
jdkPath = env.get("TESTJAVA");
129+
long startTime = System.currentTimeMillis();
130+
try {
131+
ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));
132+
jShellProcess = ProcessTools.startProcess("JShell", pb,
133+
s -> { // warm-up predicate
134+
return s.contains("Welcome to JShell");
135+
});
136+
} catch (Exception ex) {
137+
throw new RuntimeException("Test ERROR " + ex, ex);
129138
}
130-
if (jdkPath == null) {
131-
throw new RuntimeException("Can't determine jdk path neither test.jdk property no TESTJAVA env are set");
139+
140+
long elapsedTime = System.currentTimeMillis() - startTime;
141+
System.out.println("Jshell Started in " + elapsedTime + "ms");
142+
143+
// Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
144+
try {
145+
Thread.sleep(2000);
146+
} catch (Exception e) {
132147
}
133-
String osname = System.getProperty("os.name");
134-
String jshell = jdkPath + ((osname.startsWith("window")) ? "/bin/jshell.exe" : "/bin/jshell");
135-
process = Runtime.getRuntime().exec(jshell);
136-
pid = process.pid();
137148
}
138149

139150
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)