From 5a7a33fd2b1690a262d17810a872d90cfec6d90d Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Sun, 14 Feb 2021 13:02:37 +0000 Subject: [PATCH] Add test to demonstrate zombie executions --- .../steps/BodyExecutionCallbackITest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/java/org/jenkinsci/plugins/workflow/steps/BodyExecutionCallbackITest.java diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/BodyExecutionCallbackITest.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/BodyExecutionCallbackITest.java new file mode 100644 index 00000000..a227b09a --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/workflow/steps/BodyExecutionCallbackITest.java @@ -0,0 +1,90 @@ +package org.jenkinsci.plugins.workflow.steps; + +import hudson.model.Result; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.TestExtension; +import org.kohsuke.stapler.DataBoundConstructor; + +import java.util.Collections; +import java.util.Set; + +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +public class BodyExecutionCallbackITest { + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void unhandledAssertionsShouldNotCreateZombieExecutions() throws Exception { + j.jenkins.setNumExecutors(1); + WorkflowJob job = j.createProject(WorkflowJob.class); + job.setDefinition(new CpsFlowDefinition("node('master') { withStartFailure { echo 'oh dear' } }")); + j.buildAndAssertStatus(Result.FAILURE, job); + assertThat(j.jenkins.getComputers()[0].getExecutors().get(0).getCurrentWorkUnit(), nullValue()); + } + + public static class WithStartFailureStep extends Step { + + @DataBoundConstructor + public WithStartFailureStep() {} + + @TestExtension("unhandledAssertionsShouldNotCreateZombieExecutions") + public static class DescriptorImpl extends StepDescriptor { + @Override + public String getFunctionName() { + return "withStartFailure"; + } + + @Override + public Set> getRequiredContext() { + return Collections.emptySet(); + } + + @Override + public boolean takesImplicitBlockArgument() { + return true; + } + } + + @Override + public StepExecution start(StepContext context) throws Exception { + return new WithStartFailureStepExecution(context); + } + + static class WithStartFailureStepExecution extends AbstractStepExecutionImpl { + + WithStartFailureStepExecution(final StepContext context) { + super(context); + } + + @Override + public boolean start() throws Exception { + getContext().newBodyInvoker().withCallback(new WithStartFailureStepCallback()).start(); + return false; + } + + static class WithStartFailureStepCallback extends BodyExecutionCallback { + @Override + public void onStart(StepContext context) { + throw new RuntimeException("onStart broken"); + } + + @Override + public void onSuccess(StepContext context, Object result) { + context.onSuccess(result); + } + + @Override + public void onFailure(StepContext context, Throwable t) { + context.onFailure(t); + } + } + } + } + +}