HADOOP-14637. GenericTestUtils.waitFor needs to check condition again after max wait time. Contributed by Daniel Templeton
Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/5aa2bf23 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/5aa2bf23 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/5aa2bf23 Branch: refs/heads/HDFS-7240 Commit: 5aa2bf231f40423865f0054ca27426ceb95ab4ba Parents: f5f14a2 Author: Jason Lowe <jl...@yahoo-inc.com> Authored: Tue Jul 18 16:23:41 2017 -0500 Committer: Jason Lowe <jl...@yahoo-inc.com> Committed: Tue Jul 18 16:23:41 2017 -0500 ---------------------------------------------------------------------- .../apache/hadoop/test/GenericTestUtils.java | 39 ++++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/5aa2bf23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java index 82a5e08..38a0c6c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java @@ -335,25 +335,40 @@ public abstract class GenericTestUtils { } } + /** + * Wait for the specified test to return true. The test will be performed + * initially and then every {@code checkEveryMillis} until at least + * {@code waitForMillis} time has expired. If {@code check} is null or + * {@code waitForMillis} is less than {@code checkEveryMillis} this method + * will throw an {@link IllegalArgumentException}. + * + * @param check the test to perform + * @param checkEveryMillis how often to perform the test + * @param waitForMillis the amount of time after which no more tests will be + * performed + * @throws TimeoutException if the test does not return true in the allotted + * time + * @throws InterruptedException if the method is interrupted while waiting + */ public static void waitFor(Supplier<Boolean> check, int checkEveryMillis, int waitForMillis) throws TimeoutException, InterruptedException { Preconditions.checkNotNull(check, ERROR_MISSING_ARGUMENT); - Preconditions.checkArgument(waitForMillis > checkEveryMillis, + Preconditions.checkArgument(waitForMillis >= checkEveryMillis, ERROR_INVALID_ARGUMENT); long st = Time.now(); - do { - boolean result = check.get(); - if (result) { - return; - } - + boolean result = check.get(); + + while (!result && (Time.now() - st < waitForMillis)) { Thread.sleep(checkEveryMillis); - } while (Time.now() - st < waitForMillis); - - throw new TimeoutException("Timed out waiting for condition. " + - "Thread diagnostics:\n" + - TimedOutTestsListener.buildThreadDiagnosticString()); + result = check.get(); + } + + if (!result) { + throw new TimeoutException("Timed out waiting for condition. " + + "Thread diagnostics:\n" + + TimedOutTestsListener.buildThreadDiagnosticString()); + } } /** --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-commits-h...@hadoop.apache.org