@BeforeClass public static void createProcessController() throws IOException, URISyntaxException { if (File.pathSeparatorChar == ':') { processUtil = new UnixProcessUtil(); } else { processUtil = new WindowsProcessUtil(); } String jbossHome = System.getProperty("jboss.home"); if (jbossHome == null) { throw new IllegalStateException("-Djboss.home must be set"); } ClassLoader tccl = Thread.currentThread().getContextClassLoader(); URL url = tccl.getResource("domain-configs/domain-standard.xml"); Assert.assertNotNull(url); File domainXml = new File(url.toURI()); url = tccl.getResource("host-configs/respawn-master.xml"); File hostXml = new File(url.toURI()); Assert.assertTrue(domainXml.exists()); Assert.assertTrue(hostXml.exists()); List<String> args = new ArrayList<String>(); args.add("-jboss-home"); args.add(jbossHome); args.add("-jvm"); args.add(processUtil.getJavaCommand()); args.add("--"); args.add("-Dorg.jboss.boot.log.file=" + jbossHome + "/domain/log/host-controller/boot.log"); args.add( "-Dlogging.configuration=file:" + jbossHome + "/domain/configuration/logging.properties"); args.add( "-Djboss.test.host.master.address=" + System.getProperty("jboss.test.host.master.address", "127.0.0.1")); args.add("-Xms64m"); args.add("-Xmx512m"); args.add("-XX:MaxPermSize=256m"); args.add("-Dorg.jboss.resolver.warning=true"); args.add("-Dsun.rmi.dgc.client.gcInterval=3600000"); args.add("-Dsun.rmi.dgc.server.gcInterval=3600000"); args.add("--"); args.add("-default-jvm"); args.add(processUtil.getJavaCommand()); args.add("--host-config=" + hostXml.getAbsolutePath()); args.add("--domain-config=" + domainXml.getAbsolutePath()); args.add( "-Djboss.test.host.master.address=" + System.getProperty("jboss.test.host.master.address", "127.0.0.1")); processController = Main.start(args.toArray(new String[args.size()])); }
private List<RunningProcess> waitForAllProcesses() throws Exception { final long time = System.currentTimeMillis() + TIMEOUT; List<RunningProcess> runningProcesses; do { runningProcesses = processUtil.getRunningProcesses(); if (processUtil.containsProcesses( runningProcesses, HOST_CONTROLLER, SERVER_ONE, SERVER_TWO)) { return runningProcesses; } Thread.sleep(200); } while (System.currentTimeMillis() < time); Assert.fail("Did not have all running processes " + runningProcesses); return null; }
private static int comparePresentationName(MethodElement e1, MethodElement e2) { Collator collator = Collator.getInstance(); String name1 = e1 instanceof BreakdownElement ? ProcessUtil.getPresentationName((BreakdownElement) e1) : e1.getPresentationName(); if (name1.length() < 1) name1 = e1.getName(); String name2 = e2 instanceof BreakdownElement ? ProcessUtil.getPresentationName((BreakdownElement) e2) : e2.getPresentationName(); if (name2.length() < 1) name2 = e2.getName(); return collator.compare(name1, name2); }
@Test public void testDomainRespawn() throws Exception { // Make sure everything started List<RunningProcess> processes = waitForAllProcesses(); readHostControllerServers(); // Kill the master HC and make sure that it gets restarted RunningProcess originalHc = processUtil.getProcess(processes, HOST_CONTROLLER); Assert.assertNotNull(originalHc); processUtil.killProcess(originalHc); processes = waitForAllProcesses(); RunningProcess respawnedHc = processUtil.getProcess(processes, HOST_CONTROLLER); Assert.assertNotNull(respawnedHc); Assert.assertFalse(originalHc.getProcessId().equals(respawnedHc.getProcessId())); readHostControllerServers(); }
private List<RunningProcess> waitForAllProcesses(Set<String> excludedProcessIds) throws Exception { final long time = System.currentTimeMillis() + TIMEOUT; List<RunningProcess> runningProcesses; do { runningProcesses = processUtil.getRunningProcesses(); for (Iterator<RunningProcess> it = runningProcesses.iterator(); it.hasNext(); ) { RunningProcess proc = it.next(); if (excludedProcessIds.contains(proc.getProcessId())) { it.remove(); } } if (processUtil.containsProcesses( runningProcesses, HOST_CONTROLLER, SERVER_ONE, SERVER_TWO)) { return runningProcesses; } Thread.sleep(200); } while (System.currentTimeMillis() < time); Assert.fail("Did not have all running processes " + runningProcesses); return null; }
void killProcess(RunningProcess process) { try { Runtime.getRuntime().exec(getKillCommand(process)); } catch (IOException e) { throw new RuntimeException(e); } final long time = System.currentTimeMillis() + TIMEOUT; do { List<RunningProcess> runningProcesses = processUtil.getRunningProcesses(); if (processUtil.getProcessById(runningProcesses, process.getProcessId()) == null) { return; } try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } while (System.currentTimeMillis() < time); Assert.fail("Did not kill process " + process + " " + processUtil.getRunningProcesses()); }
public static boolean isGemInstallable() { if (!Platform.OS_WIN32.equals(Platform.getOS())) { // TODO This code is pretty blase about possible nulls/errors/etc. Should probably try and // make it // more bullet-proof. // grab the path to the gem executable dir IPath gemBin = find("gem", true, null); // $NON-NLS-1$ String output = ProcessUtil.outputForCommand(gemBin.toOSString(), null, "environment"); // $NON-NLS-1$ final String searchString = "EXECUTABLE DIRECTORY:"; // $NON-NLS-1$ int index = output.indexOf(searchString); output = output.substring(index + searchString.length()); // find first newline... output = output.split("\r\n|\r|\n")[0].trim(); // $NON-NLS-1$ // Now see if user has rights to write to this dir to determine if we need to run under sudo return new File(output).canWrite(); } return true; }
@BeforeClass public static void createProcessController() throws IOException, URISyntaxException, NoSuchAlgorithmException { final String testName = RespawnTestCase.class.getSimpleName(); final File domains = new File("target" + File.separator + "domains" + File.separator + testName); final File masterDir = new File(domains, "master"); final String masterDirPath = masterDir.getAbsolutePath(); final File domainConfigDir = new File(masterDir, "configuration"); // TODO this should not be necessary domainConfigDir.mkdirs(); if (File.pathSeparatorChar == ':') { processUtil = new UnixProcessUtil(); } else { processUtil = new WindowsProcessUtil(); } String jbossHome = System.getProperty("jboss.home"); if (jbossHome == null) { throw new IllegalStateException("-Djboss.home must be set"); } ClassLoader tccl = Thread.currentThread().getContextClassLoader(); URL url = tccl.getResource("domain-configs/domain-respawn.xml"); Assert.assertNotNull(url); File domainXml = new File(url.toURI()); url = tccl.getResource("host-configs/respawn-master.xml"); File hostXml = new File(url.toURI()); Assert.assertTrue(domainXml.exists()); Assert.assertTrue(hostXml.exists()); copyFile(domainXml, domainConfigDir); copyFile(hostXml, domainConfigDir); // No point backing up the file in a test scenario, just write what we need. File usersFile = new File(domainConfigDir, "mgmt-users.properties"); FileOutputStream fos = new FileOutputStream(usersFile); PrintWriter pw = new PrintWriter(fos); pw.println( "slave=" + new UsernamePasswordHashUtil() .generateHashedHexURP( "slave", "ManagementRealm", "slave_user_password".toCharArray())); pw.close(); fos.close(); final String address = System.getProperty("jboss.test.host.master.address", "127.0.0.1"); List<String> args = new ArrayList<String>(); args.add("-jboss-home"); args.add(jbossHome); args.add("-jvm"); args.add(processUtil.getJavaCommand()); args.add("--"); args.add("-Dorg.jboss.boot.log.file=" + masterDirPath + "/log/host-controller.log"); args.add( "-Dlogging.configuration=file:" + jbossHome + "/domain/configuration/logging.properties"); args.add("-Djboss.test.host.master.address=" + address); TestSuiteEnvironment.getIpv6Args(args); args.add("-Xms64m"); args.add("-Xmx512m"); args.add("-XX:MaxPermSize=256m"); args.add("-Dorg.jboss.resolver.warning=true"); args.add("-Dsun.rmi.dgc.client.gcInterval=3600000"); args.add("-Dsun.rmi.dgc.server.gcInterval=3600000"); args.add("--"); args.add("-default-jvm"); args.add(processUtil.getJavaCommand()); args.add("--host-config=" + hostXml.getName()); args.add("--domain-config=" + domainXml.getName()); args.add("-Djboss.test.host.master.address=" + address); args.add("-Djboss.domain.base.dir=" + masterDir.getAbsolutePath()); args.add("--interprocess-hc-address"); args.add(address); args.add("--pc-address"); args.add(address); processController = Main.start(args.toArray(new String[args.size()])); }
/** A test for scale; launch a large number (14) of subprocesses. */ @Test public static void test5() { ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>(); int factor = 2; JavaChild p1 = null; Instant start = Instant.now(); try { p1 = JavaChild.spawnJavaChild("stdin"); ProcessHandle p1Handle = p1.toHandle(); printf("Spawning %d x %d x %d processes, pid: %d%n", factor, factor, factor, p1.getPid()); // Start the first tier of subprocesses p1.sendAction("spawn", factor, "stdin"); // Start the second tier of subprocesses p1.sendAction("child", "spawn", factor, "stdin"); // Start the third tier of subprocesses p1.sendAction("child", "child", "spawn", factor, "stdin"); int newChildren = factor * (1 + factor * (1 + factor)); CountDownLatch spawnCount = new CountDownLatch(newChildren); // Gather the PIDs from the output of the spawning process p1.forEachOutputLine( (s) -> { String[] split = s.trim().split(" "); if (split.length == 3 && split[1].equals("spawn")) { Long child = Long.valueOf(split[2]); Long parent = Long.valueOf(split[0].split(":")[0]); processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get()); spawnCount.countDown(); } }); // Wait for all the subprocesses to be listed as started Assert.assertTrue( spawnCount.await(Utils.adjustTimeout(30L), TimeUnit.SECONDS), "Timeout waiting for processes to start"); // Debugging; list descendants that are not expected in processes List<ProcessHandle> descendants = ProcessUtil.getDescendants(p1Handle); long count = descendants.stream().filter(ph -> !processes.containsKey(ph)).count(); if (count > 0) { descendants .stream() .filter(ph -> !processes.containsKey(ph)) .forEach(ph1 -> ProcessUtil.printProcess(ph1, "Extra process: ")); ProcessUtil.logTaskList(); Assert.assertEquals(0, count, "Extra processes in descendants"); } Assert.assertEquals(getChildren(p1Handle).size(), factor, "expected direct children"); count = getDescendants(p1Handle).size(); long totalChildren = factor * factor * factor + factor * factor + factor; Assert.assertTrue( count >= totalChildren, "expected at least " + totalChildren + ", actual: " + count); List<ProcessHandle> subprocesses = getDescendants(p1Handle); printf( " descendants: %s%n", subprocesses.stream().map(p -> p.getPid()).collect(Collectors.toList())); p1.getOutputStream().close(); // Close stdin for the controlling p1 p1.waitFor(); } catch (InterruptedException | IOException ex) { Assert.fail("Unexpected Exception", ex); } finally { printf("Duration: %s%n", Duration.between(start, Instant.now())); if (p1 != null) { p1.destroyForcibly(); } processes.forEach( (p, parent) -> { if (p.isAlive()) { ProcessUtil.printProcess(p, "Process Cleanup: "); p.destroyForcibly(); } }); } }
/** * Test destroy of processes. A JavaChild is started and it starts three children. Each one is * then checked to be alive and listed by descendants and forcibly destroyed. After they exit they * should no longer be listed by descendants. */ @Test public static void test3() { ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>(); try { ProcessHandle self = ProcessHandle.current(); JavaChild p1 = JavaChild.spawnJavaChild("stdin"); ProcessHandle p1Handle = p1.toHandle(); printf(" p1: %s%n", p1.getPid()); int newChildren = 3; CountDownLatch spawnCount = new CountDownLatch(newChildren); // Spawn children and have them wait p1.sendAction("spawn", newChildren, "stdin"); // Gather the PIDs from the output of the spawning process p1.forEachOutputLine( (s) -> { String[] split = s.trim().split(" "); if (split.length == 3 && split[1].equals("spawn")) { Long child = Long.valueOf(split[2]); Long parent = Long.valueOf(split[0].split(":")[0]); processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get()); spawnCount.countDown(); } }); // Wait for all the subprocesses to be listed as started Assert.assertTrue( spawnCount.await(Utils.adjustTimeout(30L), TimeUnit.SECONDS), "Timeout waiting for processes to start"); // Debugging; list descendants that are not expected in processes List<ProcessHandle> descendants = ProcessUtil.getDescendants(p1Handle); long count = descendants.stream().filter(ph -> !processes.containsKey(ph)).count(); if (count > 0) { descendants .stream() .filter(ph -> !processes.containsKey(ph)) .forEach(ph1 -> ProcessUtil.printProcess(ph1, "Extra process: ")); ProcessUtil.logTaskList(); Assert.assertEquals(0, count, "Extra processes in descendants"); } // Verify that all spawned children are alive, show up in the descendants list // then destroy them processes.forEach( (p, parent) -> { Assert.assertEquals(p.isAlive(), true, "Child should be alive: " + p); Assert.assertTrue( descendants.contains(p), "Spawned child should be listed in descendants: " + p); p.destroyForcibly(); }); Assert.assertEquals(processes.size(), newChildren, "Wrong number of children"); // Wait for each of the processes to exit processes.forEach( (p, parent) -> { for (long retries = Utils.adjustTimeout(100L); retries > 0; retries--) { if (!p.isAlive()) { return; // not alive, go on to the next } // Wait a bit and retry try { Thread.sleep(100L); } catch (InterruptedException ie) { // try again } } printf( "Timeout waiting for exit of pid %s, parent: %s, info: %s%n", p, parent, p.info()); Assert.fail("Process still alive: " + p); }); p1.destroyForcibly(); p1.waitFor(); // Verify that none of the spawned children are still listed by descendants List<ProcessHandle> remaining = getDescendants(self); Assert.assertFalse(remaining.remove(p1Handle), "Child p1 should have exited"); remaining = remaining.stream().filter(processes::containsKey).collect(Collectors.toList()); Assert.assertEquals(remaining.size(), 0, "Subprocess(es) should have exited: " + remaining); } catch (IOException ioe) { Assert.fail("Spawn of subprocess failed", ioe); } catch (InterruptedException inte) { Assert.fail("InterruptedException", inte); } finally { processes.forEach( (p, parent) -> { if (p.isAlive()) { ProcessUtil.printProcess(p); p.destroyForcibly(); } }); } }