diff --git a/test/pom.xml b/test/pom.xml index 50b119e317c74e8c66ef468c9d15d019cf729da5..f4e16bb6d8213bb8164c648776ca3218e836cb4e 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -198,6 +198,12 @@ THE SOFTWARE. 4.0 test + + org.awaitility + awaitility + 3.0.0 + test + diff --git a/test/src/test/java/hudson/cli/QuietDownCommandTest.java b/test/src/test/java/hudson/cli/QuietDownCommandTest.java index 840031b5dae0a8b72001f79fcd056dadfc15a49e..723f3ee6602d8fc4323ed60e9da65cdddfdc3b97 100644 --- a/test/src/test/java/hudson/cli/QuietDownCommandTest.java +++ b/test/src/test/java/hudson/cli/QuietDownCommandTest.java @@ -50,6 +50,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static hudson.cli.CLICommandInvoker.Matcher.failedWith; import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput; import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently; +import static org.awaitility.Awaitility.await; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -213,7 +214,6 @@ public class QuietDownCommandTest { try { threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); exec_task.get(10, TimeUnit.SECONDS); } catch (TimeoutException e) { @@ -259,7 +259,6 @@ public class QuietDownCommandTest { try { threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); exec_task.get(10, TimeUnit.SECONDS); } catch (TimeoutException e) { @@ -307,7 +306,6 @@ public class QuietDownCommandTest { }); threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); try { exec_task.get(2*TIMEOUT, TimeUnit.MILLISECONDS); @@ -351,7 +349,6 @@ public class QuietDownCommandTest { }); threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); final boolean timeout_occured = false; try { @@ -400,7 +397,6 @@ public class QuietDownCommandTest { }); threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); finish.signal(); @@ -427,7 +423,6 @@ public class QuietDownCommandTest { final Future build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish); assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1)); - boolean timeoutOccurred = false; final FutureTask exec_task = new FutureTask(new Callable() { public Object call() { assertJenkinsNotInQuietMode(); @@ -445,7 +440,6 @@ public class QuietDownCommandTest { }); threadPool.submit(exec_task); beforeCli.block(); - Thread.sleep(1000); // Left a room for calling CLI assertJenkinsInQuietMode(); finish.signal(); @@ -457,19 +451,39 @@ public class QuietDownCommandTest { exec_task.get(1, TimeUnit.SECONDS); } + /** + * Asserts if Jenkins is in quiet mode. + * Will retry for some time before actually failing. + */ private final void assertJenkinsInQuietMode() { assertJenkinsInQuietMode(j); } + /** + * Asserts if Jenkins is not in quiet mode. + * Will retry for some time before actually failing. + */ private final void assertJenkinsNotInQuietMode() { assertJenkinsNotInQuietMode(j); } + /** + * Asserts if Jenkins is in quiet mode, retrying for some time before failing. + * @throws TimeoutException + */ public static final void assertJenkinsInQuietMode(final JenkinsRule j) { - assertThat(j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task), equalTo(true)); + await().pollInterval(250, TimeUnit.MILLISECONDS) + .atMost(10, TimeUnit.SECONDS) + .until(() -> j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task)); } + /** + * Asserts if Jenkins is not in quiet mode, retrying for some time before failing. + * @throws TimeoutException + */ public static final void assertJenkinsNotInQuietMode(final JenkinsRule j) { - assertThat(j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task), equalTo(false)); + await().pollInterval(250, TimeUnit.MILLISECONDS) + .atMost(10, TimeUnit.SECONDS) + .until(() -> !j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task)); } }