Java Code Examples for jdk.testlibrary.Utils#getFreePort()

The following examples show how to use jdk.testlibrary.Utils#getFreePort() . 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: DynamicLauncher.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
protected void run() throws Exception {
    OutputAnalyzer out;
    int retries = 1;
    boolean tryAgain;

    do {
        tryAgain = false;
        jmxPort = Utils.getFreePort();
        out = runVM();
        try {
            out.shouldNotContain("Port already in use");
        } catch (RuntimeException e) {
            if (retries < 3) {
                retries++;
                tryAgain = true;
            }
        }
    } while (tryAgain);
}
 
Example 2
Source File: DynamicLauncher.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
protected void run() throws Exception {
    OutputAnalyzer out;
    int retries = 1;
    boolean tryAgain;

    do {
        tryAgain = false;
        jmxPort = Utils.getFreePort();
        out = runVM();
        try {
            out.shouldNotContain("Port already in use");
        } catch (RuntimeException e) {
            if (retries < 3) {
                retries++;
                tryAgain = true;
            }
        }
    } while (tryAgain);
}
 
Example 3
Source File: DynamicLauncher.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void run() throws Exception {
    OutputAnalyzer out;
    int retries = 1;
    boolean tryAgain;

    do {
        tryAgain = false;
        jmxPort = Utils.getFreePort();
        out = runVM();
        try {
            out.shouldNotContain("Port already in use");
        } catch (RuntimeException e) {
            if (retries < 3) {
                retries++;
                tryAgain = true;
            }
        }
    } while (tryAgain);
}
 
Example 4
Source File: DynamicLauncher.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected void run() throws Exception {
    OutputAnalyzer out;
    int retries = 1;
    boolean tryAgain;

    do {
        tryAgain = false;
        jmxPort = Utils.getFreePort();
        out = runVM();
        try {
            out.shouldNotContain("Port already in use");
        } catch (RuntimeException e) {
            if (retries < 3) {
                retries++;
                tryAgain = true;
            }
        }
    } while (tryAgain);
}
 
Example 5
Source File: DefaultAgentFilterTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void testDefaultAgent(String propertyFile) throws Exception {
    int port = Utils.getFreePort();
    String propFile = System.getProperty("test.src") + File.separator + propertyFile;
    List<String> pbArgs = new ArrayList<>(Arrays.asList(
            "-cp",
            System.getProperty("test.class.path"),
            "-XX:+UsePerfData"
    ));
    String[] args = new String[]{
        "-Dcom.sun.management.jmxremote.port=" + port,
        "-Dcom.sun.management.jmxremote.authenticate=false",
        "-Dcom.sun.management.jmxremote.ssl=false",
        "-Dcom.sun.management.config.file=" + propFile
    };
    pbArgs.addAll(Arrays.asList(args));
    pbArgs.add(TEST_APP_NAME);

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            pbArgs.toArray(new String[pbArgs.size()])
    );

    try (TestAppRun s = new TestAppRun(pb, DefaultAgentFilterTest.class.getSimpleName())) {
        s.start();
        JMXServiceURL url = testConnect(port);
        testMBeanOperations(url);
    }
}
 
Example 6
Source File: LookupTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    String cmd = args[0];
    if (cmd.equals("-getport")) {
        port = Utils.getFreePort();
        System.out.print(port);
    } else if (cmd.equals("-runtest")) {
        port = Integer.parseInt(args[1]);
        SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
        SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
        // name "notAllowedAndNotFound.com" is not in map
        // name "allowedButNotfound.com" is not in map
        try {
            startServer();

            System.setSecurityManager(new SecurityManager());

            test("http://allowedAndFound.com:" + port + "/foo", false, false);

            test("http://notAllowedButFound.com:" + port + "/foo", true, false);

            test("http://allowedButNotfound.com:" + port + "/foo", false, true);

            test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
        } finally {
            serverSocket.close();
        }
    } else {
        throw new RuntimeException("Bad invocation: " + cmd);
    }
}
 
Example 7
Source File: LookupTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    String cmd = args[0];
    if (cmd.equals("-getport")) {
        port = Utils.getFreePort();
        System.out.print(port);
    } else if (cmd.equals("-runtest")) {
        port = Integer.parseInt(args[1]);
        SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
        SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
        // name "notAllowedAndNotFound.com" is not in map
        // name "allowedButNotfound.com" is not in map
        try {
            startServer();

            System.setSecurityManager(new SecurityManager());

            test("http://allowedAndFound.com:" + port + "/foo", false, false);

            test("http://notAllowedButFound.com:" + port + "/foo", true, false);

            test("http://allowedButNotfound.com:" + port + "/foo", false, true);

            test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
        } finally {
            serverSocket.close();
        }
    } else {
        throw new RuntimeException("Bad invocation: " + cmd);
    }
}
 
Example 8
Source File: PortAlreadyInUseTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}
 
Example 9
Source File: LookupTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    String cmd = args[0];
    if (cmd.equals("-getport")) {
        port = Utils.getFreePort();
        System.out.print(port);
    } else if (cmd.equals("-runtest")) {
        port = Integer.parseInt(args[1]);
        SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
        SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
        // name "notAllowedAndNotFound.com" is not in map
        // name "allowedButNotfound.com" is not in map
        try {
            startServer();

            System.setSecurityManager(new SecurityManager());

            test("http://allowedAndFound.com:" + port + "/foo", false, false);

            test("http://notAllowedButFound.com:" + port + "/foo", true, false);

            test("http://allowedButNotfound.com:" + port + "/foo", false, true);

            test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
        } finally {
            serverSocket.close();
        }
    } else {
        throw new RuntimeException("Bad invocation: " + cmd);
    }
}
 
Example 10
Source File: StartManagementAgent.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void testRemoteAgent(VirtualMachine vm) throws Exception {
    int port = Utils.getFreePort();

    // try to connect - should fail
    tryConnect(port, false);

    // start agent
    System.out.println("Starting agent on port: " + port);
    Properties mgmtProps = new Properties();
    mgmtProps.put("com.sun.management.jmxremote.port", port);
    mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
    mgmtProps.put("com.sun.management.jmxremote.ssl", "false");

    System.err.println("Starting management agent ...");
    vm.startManagementAgent(mgmtProps);
    System.err.println("Started");

    // try to connect - should work
    tryConnect(port, true);

    // try to start again - should fail
    boolean exception = false;
    try {
        System.err.println("Starting management agent second time ...");
        vm.startManagementAgent(mgmtProps);
        System.err.println("Started");
    } catch(AttachOperationFailedException ex) {
        // expected
        System.err.println("Got expected exception: " + ex.getMessage());
        exception = true;
    }
    if (!exception) {
        throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
    }
}
 
Example 11
Source File: PortAlreadyInUseTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}
 
Example 12
Source File: PortAlreadyInUseTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}
 
Example 13
Source File: PortAlreadyInUseTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}
 
Example 14
Source File: StartManagementAgent.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void testRemoteAgent(VirtualMachine vm) throws Exception {
    int port = Utils.getFreePort();

    // try to connect - should fail
    tryConnect(port, false);

    // start agent
    System.out.println("Starting agent on port: " + port);
    Properties mgmtProps = new Properties();
    mgmtProps.put("com.sun.management.jmxremote.port", port);
    mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
    mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
    vm.startManagementAgent(mgmtProps);

    // try to connect - should work
    tryConnect(port, true);

    // try to start again - should fail
    boolean exception = false;
    try {
        vm.startManagementAgent(mgmtProps);
    } catch(AttachOperationFailedException ex) {
        // expected
        exception = true;
    }
    if (!exception) {
        throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
    }
}
 
Example 15
Source File: StartManagementAgent.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void testRemoteAgent(VirtualMachine vm) throws Exception {
    int port = Utils.getFreePort();

    // try to connect - should fail
    tryConnect(port, false);

    // start agent
    System.out.println("Starting agent on port: " + port);
    Properties mgmtProps = new Properties();
    mgmtProps.put("com.sun.management.jmxremote.port", port);
    mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
    mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
    vm.startManagementAgent(mgmtProps);

    // try to connect - should work
    tryConnect(port, true);

    // try to start again - should fail
    boolean exception = false;
    try {
        vm.startManagementAgent(mgmtProps);
    } catch(AttachOperationFailedException ex) {
        // expected
        exception = true;
    }
    if (!exception) {
        throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
    }
}
 
Example 16
Source File: PortAlreadyInUseTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}
 
Example 17
Source File: StartManagementAgent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void testRemoteAgent(VirtualMachine vm) throws Exception {
    int port = Utils.getFreePort();

    // try to connect - should fail
    tryConnect(port, false);

    // start agent
    System.out.println("Starting agent on port: " + port);
    Properties mgmtProps = new Properties();
    mgmtProps.put("com.sun.management.jmxremote.port", port);
    mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
    mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
    vm.startManagementAgent(mgmtProps);

    // try to connect - should work
    tryConnect(port, true);

    // try to start again - should fail
    boolean exception = false;
    try {
        vm.startManagementAgent(mgmtProps);
    } catch(AttachOperationFailedException ex) {
        // expected
        exception = true;
    }
    if (!exception) {
        throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
    }
}
 
Example 18
Source File: LookupTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    String cmd = args[0];
    if (cmd.equals("-getport")) {
        port = Utils.getFreePort();
        System.out.print(port);
    } else if (cmd.equals("-runtest")) {
        port = Integer.parseInt(args[1]);
        SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
        SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
        // name "notAllowedAndNotFound.com" is not in map
        // name "allowedButNotfound.com" is not in map
        try {
            startServer();

            System.setSecurityManager(new SecurityManager());

            test("http://allowedAndFound.com:" + port + "/foo", false, false);

            test("http://notAllowedButFound.com:" + port + "/foo", true, false);

            test("http://allowedButNotfound.com:" + port + "/foo", false, true);

            test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
        } finally {
            serverSocket.close();
        }
    } else {
        throw new RuntimeException("Bad invocation: " + cmd);
    }
}
 
Example 19
Source File: LookupTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    String cmd = args[0];
    if (cmd.equals("-getport")) {
        port = Utils.getFreePort();
        System.out.print(port);
    } else if (cmd.equals("-runtest")) {
        port = Integer.parseInt(args[1]);
        SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
        SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
        // name "notAllowedAndNotFound.com" is not in map
        // name "allowedButNotfound.com" is not in map
        try {
            startServer();

            System.setSecurityManager(new SecurityManager());

            test("http://allowedAndFound.com:" + port + "/foo", false, false);

            test("http://notAllowedButFound.com:" + port + "/foo", true, false);

            test("http://allowedButNotfound.com:" + port + "/foo", false, true);

            test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
        } finally {
            serverSocket.close();
        }
    } else {
        throw new RuntimeException("Bad invocation: " + cmd);
    }
}
 
Example 20
Source File: PortAlreadyInUseTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void go() throws Exception {
    jmxPort = Utils.getFreePort();
    occupyPort();

    log.info("Attempting to start a VM using the same port.");
    OutputAnalyzer out = this.runVM();
    out.shouldContain("Port already in use");
    log.info("Failed as expected.");

    log.info("Trying again using retries.");
    this.run();
}