23
24 /*
25 * @test
26 * @summary Basic test for jhsdb launcher
27 * @library /test/lib/share/classes
28 * @library /lib/testlibrary
29 * @build jdk.testlibrary.*
30 * @build jdk.test.lib.apps.*
31 * @run main BasicLauncherTest
32 */
33
34 import static jdk.testlibrary.Asserts.assertTrue;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Arrays;
43 import jdk.testlibrary.JDKToolLauncher;
44 import jdk.testlibrary.Utils;
45 import jdk.testlibrary.OutputAnalyzer;
46 import jdk.testlibrary.ProcessTools;
47 import jdk.test.lib.apps.LingeredApp;
48 import jdk.testlibrary.Platform;
49
50 public class BasicLauncherTest {
51
52 private static LingeredApp theApp = null;
53 private static boolean useJavaLauncher = false;
54
55 private static JDKToolLauncher createSALauncher() {
56 JDKToolLauncher launcher = null;
57 if (useJavaLauncher) {
58 // Use java launcher if we need to pass additional parameters to VM
59 // for debugging purpose
60 // e.g. -Xlog:class+load=info:file=/tmp/BasicLauncherTest.log
61 launcher = JDKToolLauncher.createUsingTestJDK("java");
62 launcher.addToolArg("sun.jvm.hotspot.SALauncher");
94 System.out.println(line.trim());
95 }
96
97 toolProcess.waitFor();
98
99 if (toolProcess.exitValue() != 0) {
100 throw new RuntimeException("FAILED CLHSDB terminated with non-zero exit code " + toolProcess.exitValue());
101 }
102 } catch (Exception ex) {
103 throw new RuntimeException("Test ERROR " + ex, ex);
104 } finally {
105 LingeredApp.stopApp(theApp);
106 }
107 }
108
109 /**
110 *
111 * @param vmArgs - vm and java arguments to launch test app
112 * @return exit code of tool
113 */
114 public static void launch(String expectedMessage, List<String> toolArgs)
115 throws IOException {
116
117 System.out.println("Starting LingeredApp");
118 try {
119 theApp = LingeredApp.startApp(Arrays.asList("-Xmx256m"));
120
121 System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());
122 JDKToolLauncher launcher = createSALauncher();
123
124 for (String cmd : toolArgs) {
125 launcher.addToolArg(cmd);
126 }
127
128 launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
129
130 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
131 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
132 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;
133 output.shouldContain(expectedMessage);
134 output.shouldHaveExitValue(0);
135
136 } catch (Exception ex) {
137 throw new RuntimeException("Test ERROR " + ex, ex);
138 } finally {
139 LingeredApp.stopApp(theApp);
140 }
141 }
142
143 public static void launch(String expectedMessage, String... toolArgs)
144 throws IOException {
145
146 launch(expectedMessage, Arrays.asList(toolArgs));
147 }
148
149 public static void launchNotOSX(String expectedMessage, String... toolArgs)
150 throws IOException {
151
152 if (Platform.isOSX()) {
153 // Coredump stackwalking is not implemented for Darwin
154 System.out.println("This test is not expected to work on OS X. Skipping");
155 return;
156 }
157 }
158
159 public static void testHeapDump() throws IOException {
160 File dump = new File("jhsdb.jmap.dump." +
161 System.currentTimeMillis() + ".hprof");
162 if (dump.exists()) {
163 dump.delete();
164 }
165 dump.deleteOnExit();
166
167 launch("heap written to", "jmap",
168 "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
169
170 assertTrue(dump.exists() && dump.isFile(),
171 "Could not create dump file " + dump.getAbsolutePath());
172 }
173
174 public static void main(String[] args)
175 throws IOException {
176
177 if (!Platform.shouldSAAttach()) {
178 // Silently skip the test if we don't have enough permissions to attach
179 System.err.println("Error! Insufficient permissions to attach.");
180 return;
181 }
182
183 launchCLHSDB();
184
185 launch("compiler detected", "jmap", "--clstats");
186 launchNotOSX("No deadlocks found", "jstack");
187 launch("compiler detected", "jmap");
188 launch("Java System Properties", "jinfo");
189 launch("java.threads", "jsnap");
190
191 testHeapDump();
192
193 // The test throws RuntimeException on error.
194 // IOException is thrown if LingeredApp can't start because of some bad
195 // environment condition
196 System.out.println("Test PASSED");
197 }
198 }
|
23
24 /*
25 * @test
26 * @summary Basic test for jhsdb launcher
27 * @library /test/lib/share/classes
28 * @library /lib/testlibrary
29 * @build jdk.testlibrary.*
30 * @build jdk.test.lib.apps.*
31 * @run main BasicLauncherTest
32 */
33
34 import static jdk.testlibrary.Asserts.assertTrue;
35
36 import java.io.BufferedReader;
37 import java.io.IOException;
38 import java.io.InputStreamReader;
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Arrays;
43 import java.util.Optional;
44 import jdk.testlibrary.JDKToolLauncher;
45 import jdk.testlibrary.Utils;
46 import jdk.testlibrary.OutputAnalyzer;
47 import jdk.testlibrary.ProcessTools;
48 import jdk.test.lib.apps.LingeredApp;
49 import jdk.testlibrary.Platform;
50
51 public class BasicLauncherTest {
52
53 private static LingeredApp theApp = null;
54 private static boolean useJavaLauncher = false;
55
56 private static JDKToolLauncher createSALauncher() {
57 JDKToolLauncher launcher = null;
58 if (useJavaLauncher) {
59 // Use java launcher if we need to pass additional parameters to VM
60 // for debugging purpose
61 // e.g. -Xlog:class+load=info:file=/tmp/BasicLauncherTest.log
62 launcher = JDKToolLauncher.createUsingTestJDK("java");
63 launcher.addToolArg("sun.jvm.hotspot.SALauncher");
95 System.out.println(line.trim());
96 }
97
98 toolProcess.waitFor();
99
100 if (toolProcess.exitValue() != 0) {
101 throw new RuntimeException("FAILED CLHSDB terminated with non-zero exit code " + toolProcess.exitValue());
102 }
103 } catch (Exception ex) {
104 throw new RuntimeException("Test ERROR " + ex, ex);
105 } finally {
106 LingeredApp.stopApp(theApp);
107 }
108 }
109
110 /**
111 *
112 * @param vmArgs - vm and java arguments to launch test app
113 * @return exit code of tool
114 */
115 public static void launch(String expectedMessage,
116 Optional<String> unexpectedMessage, List<String> toolArgs)
117 throws IOException {
118
119 System.out.println("Starting LingeredApp");
120 try {
121 theApp = LingeredApp.startApp(Arrays.asList("-Xmx256m"));
122
123 System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());
124 JDKToolLauncher launcher = createSALauncher();
125
126 for (String cmd : toolArgs) {
127 launcher.addToolArg(cmd);
128 }
129
130 launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
131
132 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
133 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
134 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;
135 output.shouldContain(expectedMessage);
136 unexpectedMessage.ifPresent(output::shouldNotContain);
137 output.shouldHaveExitValue(0);
138
139 } catch (Exception ex) {
140 throw new RuntimeException("Test ERROR " + ex, ex);
141 } finally {
142 LingeredApp.stopApp(theApp);
143 }
144 }
145
146 public static void launch(String expectedMessage,
147 String unexpectedMessage, String... toolArgs)
148 throws IOException {
149
150 launch(expectedMessage, Optional.ofNullable(unexpectedMessage),
151 Arrays.asList(toolArgs));
152 }
153
154 public static void launchNotOSX(String expectedMessage,
155 String unexpectedMessage, String... toolArgs)
156 throws IOException {
157
158 if (Platform.isOSX()) {
159 // Coredump stackwalking is not implemented for Darwin
160 System.out.println("This test is not expected to work on OS X. Skipping");
161 return;
162 }
163
164 launch(expectedMessage, unexpectedMessage, toolArgs);
165 }
166
167 public static void testHeapDump() throws IOException {
168 File dump = new File("jhsdb.jmap.dump." +
169 System.currentTimeMillis() + ".hprof");
170 if (dump.exists()) {
171 dump.delete();
172 }
173 dump.deleteOnExit();
174
175 launch("heap written to", null, "jmap",
176 "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
177
178 assertTrue(dump.exists() && dump.isFile(),
179 "Could not create dump file " + dump.getAbsolutePath());
180 }
181
182 public static void main(String[] args)
183 throws IOException {
184
185 if (!Platform.shouldSAAttach()) {
186 // Silently skip the test if we don't have enough permissions to attach
187 System.err.println("Error! Insufficient permissions to attach.");
188 return;
189 }
190
191 launchCLHSDB();
192
193 launch("compiler detected", null, "jmap", "--clstats");
194 launchNotOSX("No deadlocks found", null, "jstack");
195 launch("compiler detected", null, "jmap");
196 launch("Java System Properties",
197 "System Properties info not available", "jinfo");
198 launch("java.threads", null, "jsnap");
199
200 testHeapDump();
201
202 // The test throws RuntimeException on error.
203 // IOException is thrown if LingeredApp can't start because of some bad
204 // environment condition
205 System.out.println("Test PASSED");
206 }
207 }
|