Java Code Examples for jdk.testlibrary.ProcessTools#startProcess()

The following examples show how to use jdk.testlibrary.ProcessTools#startProcess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JMXStartStopTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run the "jcmd" command
 * @param target The target application name (or PID)
 * @param c {@linkplain Consumer} instance; may be null
 * @param command Command with parameters; space separated string
 * @throws IOException
 * @throws InterruptedException
 */
private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
    dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));

    JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
    l.addToolArg(target);
    for(String cmd : command) {
        l.addToolArg(cmd);
    }
    Process p = ProcessTools.startProcess(
        "jcmd",
        new ProcessBuilder(l.getCommand()),
        c
    );

    p.waitFor();
    dbg_print("[jcmd] --------");
}
 
Example 2
Source File: BadHandshakeTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static Process launch(String address, String class_name) throws Exception {
    String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
        "-agentlib:jdwp=transport=dt_socket" +
        ",server=y" + ",suspend=y" + ",address=" + address,
        class_name
    });

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);

    final AtomicBoolean success = new AtomicBoolean();
    Process p = ProcessTools.startProcess(
        class_name,
        pb,
        (line) -> {
            // 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.
            success.set(line.contains("Listening for transport dt_socket at address:"));
            return true;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS
    );

    return success.get() ? p : null;
}
 
Example 3
Source File: BadHandshakeTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static Process launch(String address, String class_name) throws Exception {
    String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
        "-agentlib:jdwp=transport=dt_socket" +
        ",server=y" + ",suspend=y" + ",address=" + address,
        class_name
    });

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);

    final AtomicBoolean success = new AtomicBoolean();
    Process p = ProcessTools.startProcess(
        class_name,
        pb,
        (line) -> {
            // 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.
            success.set(line.contains("Listening for transport dt_socket at address:"));
            return true;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS
    );

    return success.get() ? p : null;
}
 
Example 4
Source File: BadHandshakeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static Process launch(String address, String class_name) throws Exception {
    String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
        "-agentlib:jdwp=transport=dt_socket" +
        ",server=y" + ",suspend=y" + ",address=" + address,
        class_name
    });

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);

    final AtomicBoolean success = new AtomicBoolean();
    Process p = ProcessTools.startProcess(
        class_name,
        pb,
        (line) -> {
            // 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.
            success.set(line.contains("Listening for transport dt_socket at address:"));
            return true;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS
    );

    return success.get() ? p : null;
}
 
Example 5
Source File: JMXStartStopTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public synchronized void start() throws InterruptedException, IOException, TimeoutException {
    if (started.compareAndSet(false, true)) {
        try {
            p = ProcessTools.startProcess(
                "JMXStartStopDoSomething",
                pb,
                (line) -> {
                    if (line.toLowerCase().startsWith("pid:")) {
                        pid = Integer.parseInt(line.split("\\:")[1]);
                    }
                    return line.equals("main enter");
                },
                5,
                TimeUnit.SECONDS
            );
        } catch (TimeoutException e) {
            p.destroy();
            p.waitFor();
            throw e;
        }
    }
}
 
Example 6
Source File: JMXStartStopTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run the "jcmd" command
 * @param target The target application name (or PID)
 * @param c {@linkplain Consumer} instance; may be null
 * @param command Command with parameters; space separated string
 * @throws IOException
 * @throws InterruptedException
 */
private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
    dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));

    JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
    l.addToolArg(target);
    for(String cmd : command) {
        l.addToolArg(cmd);
    }
    Process p = ProcessTools.startProcess(
        "jcmd",
        new ProcessBuilder(l.getCommand()),
        c
    );

    p.waitFor();
    dbg_print("[jcmd] --------");
}
 
Example 7
Source File: JMXStartStopTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run the "jcmd" command
 * @param target The target application name (or PID)
 * @param c {@linkplain Consumer} instance; may be null
 * @param command Command with parameters; space separated string
 * @throws IOException
 * @throws InterruptedException
 */
private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
    dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));

    JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
    l.addToolArg(target);
    for(String cmd : command) {
        l.addToolArg(cmd);
    }
    Process p = ProcessTools.startProcess(
        "jcmd",
        new ProcessBuilder(l.getCommand()),
        c
    );

    p.waitFor();
    dbg_print("[jcmd] --------");
}
 
Example 8
Source File: JMXInterfaceBindingTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        process = ProcessTools.startProcess(getName(), pb, predicate, 10, TimeUnit.SECONDS);
        latch.countDown();
        process.waitFor();
    } catch (Exception e) {
        throw new RuntimeException("Test failed", e);
    }
}
 
Example 9
Source File: LocalManagementTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * sanity check arguments to management-agent.jar
 */
private static boolean test4() throws Exception {
    Path agentPath = findAgent();
    if (agentPath != null) {
        ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(
            "-javaagent:" + agentPath.toString() +
            "=com.sun.management.jmxremote.port=7775," +
            "com.sun.management.jmxremote.authenticate=false," +
            "com.sun.management.jmxremote.ssl=false",
            "-cp",
            TEST_CLASSPATH,
            "TestApplication",
            "-exit"
        );

        Process prc = null;
        try {
            prc = ProcessTools.startProcess(
                "TestApplication",
                builder
            );
            int exitCode = prc.waitFor();
            return exitCode == 0;
        } finally {
            if (prc != null) {
                prc.destroy();
                prc.waitFor();
            }
        }
    }
    return false;
}
 
Example 10
Source File: AuthorizationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs AuthorizationTest$ClientSide with the passed options and redirects
 * subprocess standard I/O to the current (parent) process. This provides a
 * trace of what happens in the subprocess while it is runnning (and before
 * it terminates).
 *
 * @param serviceUrlStr string representing the JMX service Url to connect to.
 */
private int runClientSide(String args[], String serviceUrlStr) throws Exception {

    // Building command-line
    List<String> opts = buildCommandLine(args);
    opts.add("-serviceUrl");
    opts.add(serviceUrlStr);

    // Launch separate JVM subprocess
    int exitCode = 0;
    String[] optsArray = opts.toArray(new String[0]);
    ProcessBuilder pb = new ProcessBuilder(optsArray);
    Process p = ProcessTools.startProcess("AuthorizationTest$ClientSide", pb);

    // Handling end of subprocess
    try {
        exitCode = p.waitFor();
        if (exitCode != 0) {
            System.out.println(
                "Subprocess unexpected exit value of [" + exitCode +
                "]. Expected 0.\n");
        }
    } catch (InterruptedException e) {
        System.out.println("Parent process interrupted with exception : \n " + e + " :" );

        // Parent thread unknown state, killing subprocess.
        p.destroyForcibly();

        throw new RuntimeException(
            "Parent process interrupted with exception : \n " + e + " :" );

    } finally {
        if (p.isAlive()) {
            p.destroyForcibly();
        }
        return exitCode;
    }

 }
 
Example 11
Source File: LocalManagementTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * sanity check arguments to management-agent.jar
 */
private static boolean test4() throws Exception {
    Path agentPath = findAgent();
    if (agentPath != null) {
        ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(
            "-javaagent:" + agentPath.toString() +
            "=com.sun.management.jmxremote.port=7775," +
            "com.sun.management.jmxremote.authenticate=false," +
            "com.sun.management.jmxremote.ssl=false",
            "-cp",
            TEST_CLASSPATH,
            "TestApplication",
            "-exit"
        );

        Process prc = null;
        try {
            prc = ProcessTools.startProcess(
                "TestApplication",
                builder
            );
            int exitCode = prc.waitFor();
            return exitCode == 0;
        } finally {
            if (prc != null) {
                prc.destroy();
                prc.waitFor();
            }
        }
    }
    return false;
}
 
Example 12
Source File: BadHandshakeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static LaunchResult launch(String address, String class_name) throws Exception {
    String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
        "-agentlib:jdwp=transport=dt_socket" +
        ",server=y" + ",suspend=y" + ",address=" + address,
        class_name
    });

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);

    final AtomicBoolean success = new AtomicBoolean();
    final AtomicBoolean bindFailed = new AtomicBoolean();
    Process p = ProcessTools.startProcess(
        class_name,
        pb,
        (line) -> {
            // 'Listening for transport dt_socket at address: xxxxx'
            // indicates the debuggee is ready to accept connections
            if (line.contains("Listening for transport dt_socket at address:")) {
                success.set(true);
                return true;
            }
            // 'Address already in use' indicates
            // the debuggee has failed to start due to busy port.
            if (line.contains("Address already in use")) {
                bindFailed.set(true);
                return true;
            }
            return false;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS
    );

    return new LaunchResult(success.get() ? p : null,
            bindFailed.get());
}
 
Example 13
Source File: DefaultAgentFilterTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void start() throws Exception {
    if (started.compareAndSet(false, true)) {
        try {
            AtomicBoolean error = new AtomicBoolean(false);
            AtomicBoolean bindError = new AtomicBoolean(false);
            p = ProcessTools.startProcess(
                    TEST_APP_NAME + "{" + name + "}",
                    pb,
                    (line) -> {
                        if (line.toLowerCase().contains("exception")
                        || line.toLowerCase().contains("error")) {
                            error.set(true);
                        }
                        bindError.set(line.toLowerCase().contains("bindexception"));
                        return true;
                    }, 10, TimeUnit.SECONDS);
            if (bindError.get()) {
                throw new BindException("Process could not be started");
            } else if (error.get()) {
                throw new RuntimeException();
            }
        } catch (Exception ex) {
            if (p != null) {
                p.destroy();
                p.waitFor();
            }
            throw ex;
        }
    }
}
 
Example 14
Source File: MXBeanWeirdParamTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs MXBeanWeirdParamTest$ClientSide with the passed options and redirects
 * subprocess standard I/O to the current (parent) process. This provides a
 * trace of what happens in the subprocess while it is runnning (and before
 * it terminates).
 *
 * @param serviceUrlStr string representing the JMX service Url to connect to.
 */
private int runClientSide(String serviceUrlStr) throws Exception {

    // Building command-line
    List<String> opts = buildCommandLine();
    opts.add(serviceUrlStr);

    // Launch separate JVM subprocess
    int exitCode = 0;
    String[] optsArray = opts.toArray(new String[0]);
    ProcessBuilder pb = new ProcessBuilder(optsArray);
    Process p = ProcessTools.startProcess("MXBeanWeirdParamTest$ClientSide", pb);

    // Handling end of subprocess
    try {
        exitCode = p.waitFor();
        if (exitCode != 0) {
            System.out.println(
                "Subprocess unexpected exit value of [" + exitCode +
                "]. Expected 0.\n");
        }
    } catch (InterruptedException e) {
        System.out.println("Parent process interrupted with exception : \n " + e + " :" );

        // Parent thread unknown state, killing subprocess.
        p.destroyForcibly();

        throw new RuntimeException(
            "Parent process interrupted with exception : \n " + e + " :" );
    } finally {
        return exitCode;
    }

 }
 
Example 15
Source File: ExclusiveBind.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);

    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess(
            "process1",
            process1,
            line -> line.equals("Listening for transport dt_socket at address: " + address),
            Math.round(5000 * Utils.TIMEOUT_FACTOR),
            TimeUnit.MILLISECONDS
    );

    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");

    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();

    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg =
        (Connector.IntegerArgument)conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();

    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}
 
Example 16
Source File: ExclusiveBind.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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);

    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess(
            "process1",
            process1,
            line -> line.equals("Listening for transport dt_socket at address: " + address),
            Math.round(5000 * Utils.TIMEOUT_FACTOR),
            TimeUnit.MILLISECONDS
    );

    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");

    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();

    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg =
        (Connector.IntegerArgument)conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();

    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}
 
Example 17
Source File: SecurityTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs SecurityTest$ClientSide with the passed options and redirects
 * subprocess standard I/O to the current (parent) process. This provides a
 * trace of what happens in the subprocess while it is runnning (and before
 * it terminates).
 *
 * @param serviceUrlStr string representing the JMX service Url to connect to.
 */
private int runClientSide(String args[], String serviceUrlStr) throws Exception {

    System.out.println("SecurityTest::runClientSide: Start") ;

    // Building command-line
    List<String> opts = buildCommandLine(args);
    opts.add("-serviceUrl");
    opts.add(serviceUrlStr);

    // Launch separate JVM subprocess
    int exitCode = 0;
    String[] optsArray = opts.toArray(new String[0]);
    ProcessBuilder pb = new ProcessBuilder(optsArray);
    Process p = ProcessTools.startProcess("SecurityTest$ClientSide", pb);

    // Handling end of subprocess
    try {
        exitCode = p.waitFor();
        if (exitCode != 0) {
            System.out.println(
                "Subprocess unexpected exit value of [" + exitCode +
                "]. Expected 0.\n");
        }
    } catch (InterruptedException e) {
        System.out.println("Parent process interrupted with exception : \n " + e + " :" );

        // Parent thread unknown state, killing subprocess.
        p.destroyForcibly();

        throw new RuntimeException(
            "Parent process interrupted with exception : \n " + e + " :" );

    } finally {
        if (p.isAlive()) {
            p.destroyForcibly();
        }

        System.out.println("SecurityTest::runClientSide: Done") ;

        return exitCode;
    }

 }
 
Example 18
Source File: ExclusiveBind.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);

    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess(
            "process1",
            process1,
            line -> line.equals("Listening for transport dt_socket at address: " + address),
            Utils.adjustTimeout(5000),
            TimeUnit.MILLISECONDS
    );

    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");

    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();

    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg =
        (Connector.IntegerArgument)conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();

    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}
 
Example 19
Source File: ExclusiveBind.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);

    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess(
            "process1",
            process1,
            line -> line.equals("Listening for transport dt_socket at address: " + address),
            Math.round(5000 * Utils.TIMEOUT_FACTOR),
            TimeUnit.MILLISECONDS
    );

    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");

    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();

    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg =
        (Connector.IntegerArgument)conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();

    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}
 
Example 20
Source File: ExclusiveBind.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);

    // launch the first debuggee
    ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
    // start the debuggee and wait for the "ready" message
    Process p = ProcessTools.startProcess(
            "process1",
            process1,
            line -> line.equals("Listening for transport dt_socket at address: " + address),
            Math.round(5000 * Utils.TIMEOUT_FACTOR),
            TimeUnit.MILLISECONDS
    );

    // launch a second debuggee with the same address
    ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");

    // get exit status from second debuggee
    int exitCode = ProcessTools.startProcess("process2", process2).waitFor();

    // clean-up - attach to first debuggee and resume it
    AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
    Map conn_args = conn.defaultArguments();
    Connector.IntegerArgument port_arg =
        (Connector.IntegerArgument)conn_args.get("port");
    port_arg.setValue(port);
    VirtualMachine vm = conn.attach(conn_args);
    vm.resume();

    // if the second debuggee ran to completion then we've got a problem
    if (exitCode == 0) {
        throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
    } else {
        System.out.println("Test passed - second debuggee correctly failed to bind");
    }
}