2424
2525package org .jenkinsci .plugins .workflow .steps ;
2626
27+ import static org .awaitility .Awaitility .await ;
2728import static org .hamcrest .MatcherAssert .assertThat ;
2829import static org .hamcrest .Matchers .containsString ;
2930import static org .hamcrest .Matchers .empty ;
30- import static org .junit .Assert .assertTrue ;
31+ import static org .junit .jupiter . api . Assertions .assertTrue ;
3132
3233import hudson .model .Result ;
3334import hudson .model .TaskListener ;
4142import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
4243import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
4344import org .jenkinsci .plugins .workflow .test .steps .SemaphoreStep ;
44- import org .junit .Before ;
45- import org .junit .ClassRule ;
46- import org .junit .Rule ;
47- import org .junit .Test ;
48- import org .jvnet .hudson .test .BuildWatcher ;
45+ import org .junit .jupiter .api .BeforeEach ;
46+ import org .junit .jupiter .api .Test ;
47+ import org .junit .jupiter .api .extension .RegisterExtension ;
4948import org .jvnet .hudson .test .Issue ;
5049import org .jvnet .hudson .test .JenkinsRule ;
51- import org .jvnet .hudson .test .LoggerRule ;
50+ import org .jvnet .hudson .test .LogRecorder ;
5251import org .jvnet .hudson .test .TestExtension ;
52+ import org .jvnet .hudson .test .junit .jupiter .BuildWatcherExtension ;
53+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
5354import org .kohsuke .stapler .DataBoundConstructor ;
5455
55- public class GeneralNonBlockingStepExecutionTest {
56+ @ WithJenkins
57+ class GeneralNonBlockingStepExecutionTest {
5658
57- @ ClassRule public static BuildWatcher buildWatcher = new BuildWatcher ();
59+ @ SuppressWarnings ("unused" )
60+ @ RegisterExtension
61+ private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension ();
5862
59- @ Rule public JenkinsRule r = new JenkinsRule ();
63+ private static Semaphore startEnter , startExit , endEnter , endExit ;
64+
65+ private final LogRecorder logging = new LogRecorder ();
6066
61- @ Rule public LoggerRule logging = new LoggerRule ();
67+ private JenkinsRule r ;
68+
69+ @ BeforeEach
70+ void setUp (JenkinsRule rule ) {
71+ r = rule ;
72+ startEnter = new Semaphore (0 );
73+ startExit = new Semaphore (0 );
74+ endEnter = new Semaphore (0 );
75+ endExit = new Semaphore (0 );
76+ }
6277
63- @ Test public void getStatus () throws Exception {
78+ @ Test
79+ void getStatus () throws Exception {
6480 WorkflowJob p = r .createProject (WorkflowJob .class , "p" );
6581 p .setDefinition (new CpsFlowDefinition ("slowBlock {semaphore 'wait'}" , true ));
6682 WorkflowRun b = p .scheduleBuild2 (0 ).waitForStart ();
@@ -71,9 +87,7 @@ public class GeneralNonBlockingStepExecutionTest {
7187 startExit .release ();
7288 SemaphoreStep .waitForStart ("wait/1" , b );
7389 assertThat (((CpsFlowExecution ) b .getExecution ()).getThreadDump ().toString (), containsString ("at DSL.slowBlock(not currently scheduled, or running blocks)" ));
74- while (b .getExecutor ().getAsynchronousExecution ().blocksRestart ()) {
75- Thread .sleep (100 ); // as above
76- }
90+ await ().until (() -> !b .getExecutor ().getAsynchronousExecution ().blocksRestart ());
7791 SemaphoreStep .success ("wait/1" , null );
7892 endEnter .acquire ();
7993 assertThat (((CpsFlowExecution ) b .getExecution ()).getThreadDump ().toString (), containsString ("at DSL.slowBlock(running in thread: org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution [#" ));
@@ -83,7 +97,8 @@ public class GeneralNonBlockingStepExecutionTest {
8397 r .assertBuildStatusSuccess (r .waitForCompletion (b ));
8498 }
8599
86- @ Test public void stop () throws Exception {
100+ @ Test
101+ void stop () throws Exception {
87102 WorkflowJob p = r .createProject (WorkflowJob .class , "p" );
88103 p .setDefinition (new CpsFlowDefinition ("slowBlock {semaphore 'wait'}" , true ));
89104 logging .record (CpsStepContext .class , Level .WARNING ).capture (100 );
@@ -120,7 +135,8 @@ public class GeneralNonBlockingStepExecutionTest {
120135 }
121136
122137 @ Issue ("JENKINS-58878" )
123- @ Test public void shouldNotHang () throws Exception {
138+ @ Test
139+ void shouldNotHang () throws Exception {
124140 int iterations = 50 ;
125141 startExit .release (iterations ); // Prevents the semaphores from blocking inside of the slowBlock step.
126142 endExit .release (iterations );
@@ -134,61 +150,72 @@ public class GeneralNonBlockingStepExecutionTest {
134150 r .buildAndAssertSuccess (p );
135151 }
136152
137- private static Semaphore startEnter , startExit , endEnter , endExit ;
153+ @ SuppressWarnings ("unused" )
154+ public static final class SlowBlockStep extends Step {
138155
139- @ Before public void semaphores () {
140- startEnter = new Semaphore (0 );
141- startExit = new Semaphore (0 );
142- endEnter = new Semaphore (0 );
143- endExit = new Semaphore (0 );
144- }
156+ @ DataBoundConstructor
157+ public SlowBlockStep () {}
145158
146- public static final class SlowBlockStep extends Step {
147- @ DataBoundConstructor public SlowBlockStep () {}
148- @ Override public StepExecution start (StepContext context ) {
159+ @ Override
160+ public StepExecution start (StepContext context ) {
149161 return new Execution (context , this );
150162 }
163+
151164 private static final class Execution extends GeneralNonBlockingStepExecution {
152165 private final transient SlowBlockStep step ;
166+
153167 Execution (StepContext context , SlowBlockStep step ) {
154168 super (context );
155169 this .step = step ;
156170 }
171+
157172 private void println (String msg ) throws Exception {
158173 getContext ().get (TaskListener .class ).getLogger ().println (msg );
159174 }
160- @ Override public boolean start () throws Exception {
175+
176+ @ Override
177+ public boolean start () throws Exception {
161178 println ("starting step" );
162179 run (this ::doStart );
163180 return false ;
164181 }
182+
165183 private void doStart () throws Exception {
166184 println ("starting background part of step" );
167185 startEnter .release ();
168186 startExit .acquire ();
169187 println ("starting body" );
170188 getContext ().newBodyInvoker ().withCallback (new Callback ()).start ();
171189 }
190+
172191 private final class Callback extends TailCall {
173- @ Override protected void finished (StepContext context ) throws Exception {
192+
193+ @ Override
194+ protected void finished (StepContext context ) throws Exception {
174195 println ("body completed, starting background end part of step" );
175196 endEnter .release ();
176197 endExit .acquire ();
177198 println ("ending step" );
178199 }
179200 }
180201 }
181- @ TestExtension public static final class DescriptorImpl extends StepDescriptor {
182- @ Override public String getFunctionName () {
202+ @ TestExtension
203+ public static final class DescriptorImpl extends StepDescriptor {
204+
205+ @ Override
206+ public String getFunctionName () {
183207 return "slowBlock" ;
184208 }
185- @ Override public Set <? extends Class <?>> getRequiredContext () {
209+
210+ @ Override
211+ public Set <? extends Class <?>> getRequiredContext () {
186212 return Collections .singleton (TaskListener .class );
187213 }
188- @ Override public boolean takesImplicitBlockArgument () {
214+
215+ @ Override
216+ public boolean takesImplicitBlockArgument () {
189217 return true ;
190218 }
191219 }
192220 }
193-
194221}
0 commit comments