package hudson.util;

import hudson.Functions;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.Maven;
import hudson.tasks.Shell;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;

public class ProcessTreeKillerTest extends HudsonTestCase {

	public void testManualAbortProcess() throws Exception {
		ProcessTree.enabled = true;
		FreeStyleProject project = createFreeStyleProject();
		
		// this contains a maven project with a single test that sleeps 5s.
		project.setScm(new ExtractResourceSCM(getClass().getResource(
				"ProcessTreeKiller-test-project.jar")));
		project.getBuildersList().add(new Maven("install", "maven"));

		// build the project, wait until tests are running, then cancel.
		project.scheduleBuild2(0).waitForStart();

        FreeStyleBuild b = project.getLastBuild();
        b.doStop();

		Thread.sleep(1000);
		
		// will fail (at least on windows) if test process is still running
		b.getWorkspace().deleteRecursive();

	}

    @Bug(22641)
    public void testProcessProperlyKilledUnix() throws Exception {
        ProcessTree.enabled = true;
        if (Functions.isWindows()) return; // This test does not involve windows.

        FreeStyleProject sleepProject = createFreeStyleProject();
        FreeStyleProject processJob = createFreeStyleProject();

        sleepProject.getBuildersList().add(new Shell("nohup sleep 100000 &"));

        assertBuildStatusSuccess(sleepProject.scheduleBuild2(0).get());

        processJob.getBuildersList().add(new Shell("ps -ef | grep sleep"));

        String log = processJob.scheduleBuild2(0).get().getLog();

        System.err.println("LOGS: " + log);

        assertFalse("Did not properly kill task", log.contains("sleep 100000"));
    }

}

@@ -1644,189 +1644,78 @@
     
-String log = processJob.scheduleBuild2(0).get().getLog();%0A%0A        System.err.println(%22LOGS: %22 + log);%0A%0A        assertFalse(%22Did not properly kill task%22, log.contains(%22sleep 100000%22
+assertLogNotContains(%22sleep 100000%22, processJob.scheduleBuild2(0).get(
 ));%0A

Use assertLogNotContains to verify that logs do not contain sleep process.