|
| 1 | +package org.jenkinsci.plugins.workflow.test.steps; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | + |
| 5 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 6 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 7 | +import org.jenkinsci.plugins.workflow.steps.Step; |
| 8 | +import org.jenkinsci.plugins.workflow.steps.StepContext; |
| 9 | +import org.jenkinsci.plugins.workflow.steps.StepDescriptor; |
| 10 | +import org.jenkinsci.plugins.workflow.steps.StepExecution; |
| 11 | +import org.jenkinsci.plugins.workflow.steps.StepExecutions; |
| 12 | +import org.junit.ClassRule; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.jvnet.hudson.test.BuildWatcher; |
| 16 | +import org.jvnet.hudson.test.JenkinsSessionRule; |
| 17 | +import org.jvnet.hudson.test.TestExtension; |
| 18 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 19 | + |
| 20 | +import hudson.model.TaskListener; |
| 21 | + |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.allOf; |
| 24 | +import static org.hamcrest.Matchers.containsString; |
| 25 | + |
| 26 | +public class SynchronousResumeNotSupportedExceptionTest { |
| 27 | + |
| 28 | + @ClassRule |
| 29 | + public static BuildWatcher bw = new BuildWatcher(); |
| 30 | + |
| 31 | + @Rule |
| 32 | + public JenkinsSessionRule rjr = new JenkinsSessionRule(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void test_SynchronousResumeNotSupportedException_ShouldShowFailedStep_AndSuggestRetry() throws Throwable { |
| 36 | + rjr.then(j -> { |
| 37 | + WorkflowJob p = j.createProject(WorkflowJob.class, "p"); |
| 38 | + p.setDefinition(new CpsFlowDefinition("simulatedSleep 20", true)); |
| 39 | + var run = p.scheduleBuild2(0).waitForStart(); |
| 40 | + j.waitForMessage("Going to sleep for", run); |
| 41 | + }); |
| 42 | + rjr.then(j -> { |
| 43 | + var b = j.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1); |
| 44 | + j.waitForCompletion(b); |
| 45 | + assertThat(b.getLog(), allOf( |
| 46 | + containsString("nonresumable"), |
| 47 | + containsString("retry"), |
| 48 | + containsString("simulatedSleep"), |
| 49 | + containsString("retries"))); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unused") |
| 54 | + public static class SimulatedSleepStep extends Step { |
| 55 | + |
| 56 | + private final int sleepSeconds; |
| 57 | + |
| 58 | + @DataBoundConstructor |
| 59 | + public SimulatedSleepStep(int sleepSeconds) { |
| 60 | + this.sleepSeconds = sleepSeconds; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public StepExecution start(StepContext context) throws Exception { |
| 65 | + return StepExecutions.synchronousNonBlockingVoid(context, c -> { |
| 66 | + var buildLogger = context.get(TaskListener.class).getLogger(); |
| 67 | + buildLogger.println("Simulated sleep step started"); |
| 68 | + buildLogger.println("Going to sleep for " + sleepSeconds + " seconds"); |
| 69 | + Thread.sleep(sleepSeconds * 1000L); |
| 70 | + buildLogger.println("Simulated sleep step completed"); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @TestExtension |
| 75 | + public static final class DescriptorImpl extends StepDescriptor { |
| 76 | + |
| 77 | + @Override |
| 78 | + public Set<? extends Class<?>> getRequiredContext() { |
| 79 | + return Set.of(TaskListener.class); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getFunctionName() { |
| 84 | + return "simulatedSleep"; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments