|
| 1 | +package org.jenkinsci.plugins.workflow.cps; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 4 | +import hudson.AbortException; |
| 5 | +import hudson.ExtensionList; |
| 6 | +import hudson.model.Result; |
| 7 | +import org.jenkinsci.plugins.workflow.flow.GraphListener; |
| 8 | +import org.jenkinsci.plugins.workflow.flow.StepListener; |
| 9 | +import org.jenkinsci.plugins.workflow.graph.FlowEndNode; |
| 10 | +import org.jenkinsci.plugins.workflow.graph.FlowNode; |
| 11 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 12 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 13 | +import org.jenkinsci.plugins.workflow.steps.Step; |
| 14 | +import org.jenkinsci.plugins.workflow.steps.StepContext; |
| 15 | +import org.jenkinsci.plugins.workflow.steps.StepDescriptor; |
| 16 | +import org.jenkinsci.plugins.workflow.steps.StepExecution; |
| 17 | +import org.jenkinsci.plugins.workflow.steps.StepExecutions; |
| 18 | +import org.junit.ClassRule; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.jvnet.hudson.test.BuildWatcher; |
| 22 | +import org.jvnet.hudson.test.Issue; |
| 23 | +import org.jvnet.hudson.test.JenkinsRule; |
| 24 | +import org.jvnet.hudson.test.LoggerRule; |
| 25 | +import org.jvnet.hudson.test.TestExtension; |
| 26 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 27 | + |
| 28 | +import java.util.Set; |
| 29 | +import java.util.logging.Level; |
| 30 | + |
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | +import static org.hamcrest.Matchers.containsString; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | +import static org.hamcrest.Matchers.hasItem; |
| 35 | +import static org.hamcrest.Matchers.not; |
| 36 | + |
| 37 | +public class CpsStepContextTest { |
| 38 | + @Rule |
| 39 | + public JenkinsRule r = new JenkinsRule(); |
| 40 | + |
| 41 | + @ClassRule |
| 42 | + public static BuildWatcher buildWatcher = new BuildWatcher(); |
| 43 | + |
| 44 | + @Rule |
| 45 | + public LoggerRule logger = new LoggerRule(); |
| 46 | + |
| 47 | + @Issue("JENKINS-75067") |
| 48 | + @Test |
| 49 | + public void failingStepListenerNotLeakClosures() throws Exception { |
| 50 | + // Even before the fix there's only one warning logged. Asserting zero records is probably over-stepping, |
| 51 | + // but asserting just one record with our target message risks a false negative (some other unrelated message |
| 52 | + // being first, and our being later). |
| 53 | + logger.record(CpsThreadGroup.class, Level.WARNING).capture(10); |
| 54 | + WorkflowJob job = r.createProject(WorkflowJob.class, "p"); |
| 55 | + job.setDefinition(new CpsFlowDefinition("node {}", true)); |
| 56 | + |
| 57 | + WorkflowRun build = r.buildAndAssertStatus(Result.FAILURE, job); |
| 58 | + r.assertLogContains("oops", build); |
| 59 | + assertThat(ClosureCounter.get().closureCount, equalTo(0)); |
| 60 | + assertThat(logger.getMessages(), not(hasItem(containsString("Stale closure")))); |
| 61 | + } |
| 62 | + |
| 63 | + @TestExtension("failingStepListenerNotLeakClosures") |
| 64 | + public static class FailingStepListener implements StepListener { |
| 65 | + |
| 66 | + @Override |
| 67 | + public void notifyOfNewStep(@NonNull Step s, @NonNull StepContext context) { |
| 68 | + context.onFailure(new AbortException("oops")); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Issue("JENKINS-75067") |
| 73 | + @Test |
| 74 | + public void executionStartExceptionNotLeakClosures() throws Exception { |
| 75 | + logger.record(CpsThreadGroup.class, Level.WARNING).capture(10); |
| 76 | + WorkflowJob job = r.createProject(WorkflowJob.class, "p"); |
| 77 | + job.setDefinition(new CpsFlowDefinition("badBlock {}", true)); |
| 78 | + |
| 79 | + WorkflowRun build = r.buildAndAssertStatus(Result.FAILURE, job); |
| 80 | + r.assertLogContains("oops", build); |
| 81 | + assertThat(ClosureCounter.get().closureCount, equalTo(0)); |
| 82 | + assertThat(logger.getMessages(), not(hasItem(containsString("Stale closure")))); |
| 83 | + } |
| 84 | + |
| 85 | + @Issue("JENKINS-75067") |
| 86 | + @Test |
| 87 | + public void executionWithBodyRunningSyncNotLeakClosures() throws Exception { |
| 88 | + logger.record(CpsThreadGroup.class, Level.WARNING).capture(10); |
| 89 | + WorkflowJob job = r.createProject(WorkflowJob.class, "p"); |
| 90 | + job.setDefinition(new CpsFlowDefinition("def r = passthrough {}; echo r", true)); |
| 91 | + |
| 92 | + WorkflowRun build = r.buildAndAssertSuccess(job); |
| 93 | + r.assertLogContains("hooray", build); |
| 94 | + assertThat(ClosureCounter.get().closureCount, equalTo(0)); |
| 95 | + assertThat(logger.getMessages(), not(hasItem(containsString("Stale closure")))); |
| 96 | + } |
| 97 | + |
| 98 | + public static class BadBlockStep extends Step { |
| 99 | + |
| 100 | + @DataBoundConstructor |
| 101 | + public BadBlockStep() {} |
| 102 | + |
| 103 | + @Override |
| 104 | + public StepExecution start(StepContext context) throws Exception { |
| 105 | + return StepExecutions.synchronous(context, ctx -> { |
| 106 | + throw new AbortException("oops"); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + @TestExtension("executionStartExceptionNotLeakClosures") |
| 111 | + public static class DescriptorImpl extends StepDescriptor { |
| 112 | + |
| 113 | + @Override |
| 114 | + public Set<? extends Class<?>> getRequiredContext() { |
| 115 | + return Set.of(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getFunctionName() { |
| 120 | + return "badBlock"; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean takesImplicitBlockArgument() { |
| 125 | + return true; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static class PassthroughStep extends Step { |
| 131 | + |
| 132 | + @DataBoundConstructor |
| 133 | + public PassthroughStep() {} |
| 134 | + |
| 135 | + @Override |
| 136 | + public StepExecution start(StepContext context) throws Exception { |
| 137 | + return StepExecutions.synchronous(context, ctx -> { |
| 138 | + return "hooray"; |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + @TestExtension("executionWithBodyRunningSyncNotLeakClosures") |
| 143 | + public static class DescriptorImpl extends StepDescriptor { |
| 144 | + |
| 145 | + @Override |
| 146 | + public Set<? extends Class<?>> getRequiredContext() { |
| 147 | + return Set.of(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String getFunctionName() { |
| 152 | + return "passthrough"; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean takesImplicitBlockArgument() { |
| 157 | + return true; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @TestExtension |
| 163 | + public static class ClosureCounter implements GraphListener.Synchronous { |
| 164 | + int closureCount = -1; |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onNewHead(FlowNode node) { |
| 168 | + // this only works using a Synchronous listener, otherwise the fall-back closure cleaning |
| 169 | + // will have already executed prior to receiving this event |
| 170 | + if (node instanceof FlowEndNode) { |
| 171 | + try { |
| 172 | + closureCount = ((CpsFlowExecution) node.getExecution()).programPromise.get().closures.size(); |
| 173 | + } |
| 174 | + catch (Exception e) { |
| 175 | + throw new RuntimeException(e); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + static ClosureCounter get() { |
| 181 | + return ExtensionList.lookupSingleton(ClosureCounter.class); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments