package hudson.util;

import hudson.model.FreeStyleProject;
import hudson.model.FreeStyleBuild;
import hudson.tasks.Maven;

import org.easymock.EasyMock;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

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();

	}

}

@@ -33,30 +33,17 @@
 son.
-model.FreeStyleProject
+Functions
 ;%0Aim
@@ -94,50 +94,118 @@
 son.
-tasks.Maven;%0A%0Aimport org.easymock.EasyMock
+model.FreeStyleProject;%0Aimport hudson.tasks.Maven;%0Aimport hudson.tasks.Shell;%0Aimport org.jvnet.hudson.test.Bug
 ;%0Aim
@@ -299,95 +299,8 @@
 ase;
-%0Aimport org.kohsuke.stapler.StaplerRequest;%0Aimport org.kohsuke.stapler.StaplerResponse;
 %0A%0Apu
@@ -1069,10 +1069,773 @@
 );%0A%0A%09%7D%0A%0A
+    @Bug(22641)%0A    public void testProcessProperlyKilledUnix() throws Exception %7B%0A        ProcessTree.enabled = true;%0A        if (Functions.isWindows()) return; // This test does not involve windows.%0A%0A        FreeStyleProject sleepProject = createFreeStyleProject();%0A        FreeStyleProject processJob = createFreeStyleProject();%0A%0A        sleepProject.getBuildersList().add(new Shell(%22nohup sleep 100000 &%22));%0A%0A        assertBuildStatusSuccess(sleepProject.scheduleBuild2(0).get());%0A%0A        processJob.getBuildersList().add(new Shell(%22ps -ef %7C grep sleep%22));%0A%0A        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));%0A    %7D%0A%0A
 %7D%0A

Add test unit for JENKINS-22641