Repository: hadoop Updated Branches: refs/heads/branch-2 487b0a3d7 -> 9b961db56
HADOOP-14637. GenericTestUtils.waitFor needs to check condition again after max wait time. Contributed by Daniel Templeton (cherry picked from commit 5aa2bf231f40423865f0054ca27426ceb95ab4ba) Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/9b961db5 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/9b961db5 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/9b961db5 Branch: refs/heads/branch-2 Commit: 9b961db569b3be659a83084a22d4c67d14f128ca Parents: 487b0a3 Author: Jason Lowe <[email protected]> Authored: Tue Jul 18 16:23:41 2017 -0500 Committer: Jason Lowe <[email protected]> Committed: Tue Jul 18 16:27:34 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/9b961db5/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: [email protected] For additional commands, e-mail: [email protected]
