Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73824] Add test for deleting a Pipeline job while one of its builds is running only on a OneOffExecutor #468

Closed
wants to merge 6 commits into from
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jenkinsci.plugins.workflow.job;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -12,10 +14,14 @@
import org.htmlunit.html.HtmlCheckBoxInput;
import org.htmlunit.html.HtmlForm;
import hudson.cli.CLICommandInvoker;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.plugins.git.GitSCM;
import hudson.security.WhoAmI;
import hudson.triggers.SCMTrigger;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jenkins.plugins.git.GitSampleRepoRule;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
Expand All @@ -25,6 +31,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RunLoadCounter;


dwnusbaum marked this conversation as resolved.
Show resolved Hide resolved
public class WorkflowJobTest {

@Rule public JenkinsRule j = new JenkinsRule();
Expand Down Expand Up @@ -157,4 +164,38 @@ public void newBuildsShouldNotLoadOld() throws Throwable {
});
}

@Test
dwnusbaum marked this conversation as resolved.
Show resolved Hide resolved
public void deletionShouldWaitForBuildsToComplete() throws Throwable {
var p = j.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition(
"import java.time.Duration\n" +
"import java.time.Instant\n" +
"@NonCPS def tightLoop() {\n" +
dwnusbaum marked this conversation as resolved.
Show resolved Hide resolved
" echo 'uninterruptible'\n" +
" def start = Instant.now()\n" +
" while (Duration.between(start, Instant.now()).toSeconds() < 3) {\n" +
" try {\n" +
" Thread.sleep(100)\n" +
" } catch (InterruptedException e) { }\n" +
" }\n" +
"}\n" +
"tightLoop()", false));
var b = p.scheduleBuild2(0).waitForStart();
j.waitForMessage("uninterruptible", b);
// The build isn't done and can't be interrupted, so ItemDeletion.cancelBuildsInProgress should have to wait at least 3 seconds for it to complete.
// (but right now it completes immediately)
p.delete();
// Make sure that the job really has been deleted.
assertThat(j.jenkins.getItemByFullName(p.getFullName()), nullValue());
// ItemDeletion.cancelBuildsInProgress should guarantee that the queue is empty at this point.
// (but right now test0 #1 will still be running)
var executables = Stream.of(j.jenkins.getComputers())
.flatMap(c -> c.getAllExecutors().stream())
.map(Executor::getCurrentExecutable)
.filter(Objects::nonNull)
.collect(Collectors.toList());
assertThat(executables, empty());
dwnusbaum marked this conversation as resolved.
Show resolved Hide resolved
// (right now we end up waiting 3 seconds here for test0 #1 to complete while shutting down Jenkins)
}

}