Search in sources :

Example 1 with JavaSysMon

use of com.jezhumble.javasysmon.JavaSysMon in project gocd by gocd.

the class BuildSessionCancelingTest method cancelTaskShouldBeProcessedBeforeKillChildProcess.

@Test
@RunIf(value = EnhancedOSChecker.class, arguments = { DO_NOT_RUN_ON, WINDOWS })
public void cancelTaskShouldBeProcessedBeforeKillChildProcess() throws InterruptedException {
    final BuildSession buildSession = newBuildSession();
    final BuildCommand printSubProcessCount = exec("/bin/bash", "-c", "pgrep -P " + new JavaSysMon().currentPid() + " | wc -l");
    Thread buildingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            buildSession.build(compose(compose(execSleepScript(50), echo("after sleep")).setOnCancel(printSubProcessCount)));
        }
    });
    buildingThread.start();
    waitUntilSubProcessExists(execSleepScriptProcessCommand(), true);
    assertTrue(buildInfo(), buildSession.cancel(30, TimeUnit.SECONDS));
    waitUntilSubProcessExists(execSleepScriptProcessCommand(), false);
    assertThat(Integer.parseInt(console.lastLine().trim()), greaterThan(0));
    buildingThread.join();
}
Also used : JavaSysMon(com.jezhumble.javasysmon.JavaSysMon) BuildCommand(com.thoughtworks.go.domain.BuildCommand) RunIf(com.googlecode.junit.ext.RunIf) Test(org.junit.Test)

Example 2 with JavaSysMon

use of com.jezhumble.javasysmon.JavaSysMon in project gocd by gocd.

the class BuildSessionCancelingTest method cancelShouldProcessOnCancelCommandOfCommandThatIsRunning.

@Test
public void cancelShouldProcessOnCancelCommandOfCommandThatIsRunning() throws InterruptedException {
    final BuildSession buildSession = newBuildSession();
    Thread buildingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            buildSession.build(compose(compose(execSleepScript(50).setOnCancel(echo("exec canceled")), echo("after sleep")).setOnCancel(echo("inner oncancel"))).setOnCancel(echo("outter oncancel")));
        }
    });
    buildingThread.start();
    waitUntilSubProcessExists(execSleepScriptProcessCommand(), true);
    assertTrue(buildInfo(), buildSession.cancel(30, TimeUnit.SECONDS));
    waitUntilSubProcessExists(execSleepScriptProcessCommand(), false);
    JavaSysMon javaSysMon = new JavaSysMon();
    final boolean[] exists = { false };
    javaSysMon.visitProcessTree(javaSysMon.currentPid(), new ProcessVisitor() {

        @Override
        public boolean visit(OsProcess osProcess, int i) {
            String command = osProcess.processInfo().getName();
            if (execSleepScriptProcessCommand().equals(command)) {
                exists[0] = true;
            }
            return false;
        }
    });
    assertThat(exists[0], is(false));
    assertThat(buildInfo(), getLast(statusReporter.results()), is(Cancelled));
    assertThat(buildInfo(), console.output(), not(containsString("after sleep")));
    assertThat(buildInfo(), console.output(), containsString("exec canceled"));
    assertThat(buildInfo(), console.output(), containsString("inner oncancel"));
    assertThat(buildInfo(), console.output(), containsString("outter oncancel"));
    buildingThread.join();
}
Also used : OsProcess(com.jezhumble.javasysmon.OsProcess) JavaSysMon(com.jezhumble.javasysmon.JavaSysMon) ProcessVisitor(com.jezhumble.javasysmon.ProcessVisitor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with JavaSysMon

use of com.jezhumble.javasysmon.JavaSysMon in project gocd by gocd.

the class SubprocessLoggerTest method stubSysMon.

private JavaSysMon stubSysMon() {
    final OsProcess process1 = mock(OsProcess.class);
    when(process1.processInfo()).thenReturn(new ProcessInfo(101, 100, "command-1", "name-1", "owner-1", 100, 200, 400, 800));
    final OsProcess process1a = mock(OsProcess.class);
    when(process1a.processInfo()).thenReturn(new ProcessInfo(103, 100, "command-1a", "name-1a", "owner-1", 160, 260, 460, 860));
    final OsProcess process2 = mock(OsProcess.class);
    when(process2.processInfo()).thenReturn(new ProcessInfo(102, 101, "command-2", "name-2", "owner-1", 150, 250, 450, 850));
    JavaSysMon sysMon = new JavaSysMon() {

        @Override
        public void visitProcessTree(int pid, ProcessVisitor processVisitor) {
            processVisitor.visit(process2, 2);
            processVisitor.visit(process1, 1);
            processVisitor.visit(process1a, 1);
        }

        @Override
        public int currentPid() {
            return 100;
        }
    };
    return sysMon;
}
Also used : OsProcess(com.jezhumble.javasysmon.OsProcess) JavaSysMon(com.jezhumble.javasysmon.JavaSysMon) ProcessVisitor(com.jezhumble.javasysmon.ProcessVisitor) ProcessInfo(com.jezhumble.javasysmon.ProcessInfo)

Example 4 with JavaSysMon

use of com.jezhumble.javasysmon.JavaSysMon in project gocd by gocd.

the class ServerVersion method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    LOG.info(String.format("[Startup] Go Version: %s", version()));
    LOG.info(String.format("[Startup] PID: %s", new JavaSysMon().currentPid()));
    LOG.info(String.format("[Startup] JVM properties: %s", System.getProperties()));
    LOG.info(String.format("[Startup] Environment Variables: %s", System.getenv()));
}
Also used : JavaSysMon(com.jezhumble.javasysmon.JavaSysMon)

Example 5 with JavaSysMon

use of com.jezhumble.javasysmon.JavaSysMon in project gocd by gocd.

the class BuildSessionCancelingTest method subProcessNames.

private List<String> subProcessNames() {
    JavaSysMon javaSysMon = new JavaSysMon();
    final List<String> names = new ArrayList<>();
    final int currentPid = javaSysMon.currentPid();
    javaSysMon.visitProcessTree(currentPid, new ProcessVisitor() {

        @Override
        public boolean visit(OsProcess osProcess, int i) {
            if (osProcess.processInfo().getPid() != currentPid) {
                names.add(osProcess.processInfo().getName());
            }
            return false;
        }
    });
    return names;
}
Also used : OsProcess(com.jezhumble.javasysmon.OsProcess) JavaSysMon(com.jezhumble.javasysmon.JavaSysMon) ProcessVisitor(com.jezhumble.javasysmon.ProcessVisitor) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

JavaSysMon (com.jezhumble.javasysmon.JavaSysMon)6 OsProcess (com.jezhumble.javasysmon.OsProcess)3 ProcessVisitor (com.jezhumble.javasysmon.ProcessVisitor)3 Test (org.junit.Test)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 RunIf (com.googlecode.junit.ext.RunIf)1 ProcessInfo (com.jezhumble.javasysmon.ProcessInfo)1 BuildCommand (com.thoughtworks.go.domain.BuildCommand)1 ArrayList (java.util.ArrayList)1