8067295: Need to port Utils chagnes from JDK-8066440 into jdk workspace
authordpochepk
Sat, 13 Dec 2014 22:14:34 +0300
changeset 28415 8a8eb9fe7232
parent 28414 19769301f6bc
child 28416 a902022106d6
8067295: Need to port Utils chagnes from JDK-8066440 into jdk workspace Reviewed-by: fzhinkin, iignatyev
jdk/test/lib/testlibrary/jdk/testlibrary/Utils.java
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/Utils.java	Fri Dec 12 21:16:42 2014 +0300
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/Utils.java	Sat Dec 13 22:14:34 2014 +0300
@@ -39,6 +39,7 @@
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import java.util.concurrent.TimeUnit;
+import java.util.function.BooleanSupplier;
 
 /**
  * Common library for various test helper functions.
@@ -279,4 +280,50 @@
     public static long adjustTimeout(long tOut) {
         return Math.round(tOut * Utils.TIMEOUT_FACTOR);
     }
+
+    /**
+     * Wait for condition to be true
+     *
+     * @param condition, a condition to wait for
+     */
+    public static final void waitForCondition(BooleanSupplier condition) {
+        waitForCondition(condition, -1L, 100L);
+    }
+
+    /**
+     * Wait until timeout for condition to be true
+     *
+     * @param condition, a condition to wait for
+     * @param timeout a time in milliseconds to wait for condition to be true
+     * specifying -1 will wait forever
+     * @return condition value, to determine if wait was successfull
+     */
+    public static final boolean waitForCondition(BooleanSupplier condition,
+            long timeout) {
+        return waitForCondition(condition, timeout, 100L);
+    }
+
+    /**
+     * Wait until timeout for condition to be true for specified time
+     *
+     * @param condition, a condition to wait for
+     * @param timeout a time in milliseconds to wait for condition to be true,
+     * specifying -1 will wait forever
+     * @param sleepTime a time to sleep value in milliseconds
+     * @return condition value, to determine if wait was successfull
+     */
+    public static final boolean waitForCondition(BooleanSupplier condition,
+            long timeout, long sleepTime) {
+        long startTime = System.currentTimeMillis();
+        while (!(condition.getAsBoolean() || (timeout != -1L
+                && ((System.currentTimeMillis() - startTime) > timeout)))) {
+            try {
+                Thread.sleep(sleepTime);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                throw new Error(e);
+            }
+        }
+        return condition.getAsBoolean();
+    }
 }