private static boolean doTest(String testId, String arg) throws Exception { List<String> args = new ArrayList<>(); args.add("-XX:+UsePerfData"); args.addAll(Utils.getVmOptions()); args.add("-cp"); args.add(TEST_CLASSPATH); if (arg != null) { args.add(arg); } args.add("TestApplication"); ProcessBuilder server = ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()])); Process serverPrc = null, clientPrc = null; try { final AtomicReference<String> port = new AtomicReference<>(); serverPrc = ProcessTools.startProcess( "TestApplication(" + testId + ")", server, (String line) -> { if (line.startsWith("port:")) { port.set(line.split("\\:")[1]); } else if (line.startsWith("waiting")) { return true; } return false; }); System.out.println("Attaching test manager:"); System.out.println("========================="); System.out.println(" PID : " + serverPrc.getPid()); System.out.println(" shutdown port : " + port.get()); ProcessBuilder client = ProcessTools.createJavaProcessBuilder( "-cp", TEST_CLASSPATH, "--add-exports", "java.management/sun.management=ALL-UNNAMED", "TestManager", String.valueOf(serverPrc.getPid()), port.get(), "true"); clientPrc = ProcessTools.startProcess( "TestManager", client, (String line) -> line.startsWith("Starting TestManager for PID")); int clientExitCode = clientPrc.waitFor(); int serverExitCode = serverPrc.waitFor(); return clientExitCode == 0 && serverExitCode == 0; } finally { if (clientPrc != null) { System.out.println("Stopping process " + clientPrc); clientPrc.destroy(); clientPrc.waitFor(); } if (serverPrc != null) { System.out.println("Stopping process " + serverPrc); serverPrc.destroy(); serverPrc.waitFor(); } } }
@BeforeMethod public void startTestApp() throws Exception { testApp = ProcessTools.startProcess( TEST_APP_NAME, testAppPb, (Predicate<String>) l -> l.trim().equals("main enter")); }