Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/tools/attach/TempDirTest.java
38855 views
/*1* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.sun.tools.attach.*;2425import java.nio.file.Files;26import java.nio.file.Path;27import java.util.Properties;28import java.util.List;29import java.io.File;3031import jdk.testlibrary.OutputAnalyzer;32import jdk.testlibrary.ProcessTools;33import jdk.testlibrary.ProcessThread;3435/*36* @test37* @bug 803310438* @summary Test to make sure attach and jvmstat works correctly when java.io.tmpdir is set39* @library /lib/testlibrary40* @run build Application Shutdown RunnerUtil41* @run main/timeout=200 TempDirTest42*/4344/*45* This test runs with an extra long timeout since it takes a really long time with -Xcomp46* when starting many processes.cd /47*/4849public class TempDirTest {5051private static long startTime;5253public static void main(String args[]) throws Throwable {5455startTime = System.currentTimeMillis();5657Path clientTmpDir = Files.createTempDirectory("TempDirTest-client");58clientTmpDir.toFile().deleteOnExit();59Path targetTmpDir = Files.createTempDirectory("TempDirTest-target");60targetTmpDir.toFile().deleteOnExit();6162// run the test with all possible combinations of setting java.io.tmpdir63runExperiment(null, null);64runExperiment(clientTmpDir, null);65runExperiment(clientTmpDir, targetTmpDir);66runExperiment(null, targetTmpDir);6768}6970private static int counter = 0;7172/*73* The actual test is in the nested class TestMain.74* The responsibility of this class is to:75* 1. Start the Application class in a separate process.76* 2. Find the pid and shutdown port of the running Application.77* 3. Launches the tests in nested class TestMain that will attach to the Application.78* 4. Shut down the Application.79*/80public static void runExperiment(Path clientTmpDir, Path targetTmpDir) throws Throwable {8182System.out.print("### Running tests with overridden tmpdir for");83System.out.print(" client: " + (clientTmpDir == null ? "no" : "yes"));84System.out.print(" target: " + (targetTmpDir == null ? "no" : "yes"));85System.out.println(" ###");8687long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;88System.out.println("Started after " + elapsedTime + "s");8990final String pidFile = "TempDirTest.Application.pid-" + counter++;91ProcessThread processThread = null;92RunnerUtil.ProcessInfo info = null;93try {94String[] tmpDirArg = null;95if (targetTmpDir != null) {96tmpDirArg = new String[] {"-Djava.io.tmpdir=" + targetTmpDir};97}98processThread = RunnerUtil.startApplication(pidFile, tmpDirArg);99info = RunnerUtil.readProcessInfo(pidFile);100launchTests(info.pid, clientTmpDir);101} catch (Throwable t) {102System.out.println("TempDirTest got unexpected exception: " + t);103t.printStackTrace();104throw t;105} finally {106// Make sure the Application process is stopped.107RunnerUtil.stopApplication(info.shutdownPort, processThread);108}109110elapsedTime = (System.currentTimeMillis() - startTime) / 1000;111System.out.println("Completed after " + elapsedTime + "s");112113}114115/**116* Runs the actual tests in nested class TestMain.117* The reason for running the tests in a separate process118* is that we need to modify the class path and119* the -Djava.io.tmpdir property.120*/121private static void launchTests(int pid, Path clientTmpDir) throws Throwable {122final String sep = File.separator;123124// Need to add jdk/lib/tools.jar to classpath.125String classpath =126System.getProperty("test.class.path", "") + File.pathSeparator +127System.getProperty("test.jdk", ".") + sep + "lib" + sep + "tools.jar";128129String[] tmpDirArg = null;130if (clientTmpDir != null) {131tmpDirArg = new String [] {"-Djava.io.tmpdir=" + clientTmpDir};132}133134// Arguments : [-Djava.io.tmpdir=] -classpath cp TempDirTest$TestMain pid135String[] args = RunnerUtil.concat(136tmpDirArg,137new String[] {138"-classpath",139classpath,140"TempDirTest$TestMain",141Integer.toString(pid) });142OutputAnalyzer output = ProcessTools.executeTestJvm(args);143output.shouldHaveExitValue(0);144}145146/**147* This is the actual test. It will attach to the running Application148* and perform a number of basic attach tests.149*/150public static class TestMain {151public static void main(String args[]) throws Exception {152String pid = args[0];153154// Test 1 - list method should list the target VM155System.out.println(" - Test: VirtualMachine.list");156List<VirtualMachineDescriptor> l = VirtualMachine.list();157boolean found = false;158for (VirtualMachineDescriptor vmd: l) {159if (vmd.id().equals(pid)) {160found = true;161break;162}163}164if (found) {165System.out.println(" - " + pid + " found.");166} else {167throw new RuntimeException(pid + " not found in VM list");168}169170// Test 2 - try to attach and verify connection171172System.out.println(" - Attaching to application ...");173VirtualMachine vm = VirtualMachine.attach(pid);174175System.out.println(" - Test: system properties in target VM");176Properties props = vm.getSystemProperties();177String value = props.getProperty("attach.test");178if (value == null || !value.equals("true")) {179throw new RuntimeException("attach.test property not set");180}181System.out.println(" - attach.test property set as expected");182}183}184}185186187