8066440: Various changes in testlibrary for JDK-8059613
authoriignatyev
Sat, 13 Dec 2014 00:54:22 +0300
changeset 28383 aa83cdd8d7f1
parent 28220 6be786c17d01
child 28384 918bbbe51796
8066440: Various changes in testlibrary for JDK-8059613 Reviewed-by: thartmann, twisti Contributed-by: dmitrij.pochepko@oracle.com
hotspot/test/testlibrary/com/oracle/java/testlibrary/Utils.java
--- a/hotspot/test/testlibrary/com/oracle/java/testlibrary/Utils.java	Thu Dec 18 16:53:13 2014 +0100
+++ b/hotspot/test/testlibrary/com/oracle/java/testlibrary/Utils.java	Sat Dec 13 00:54:22 2014 +0300
@@ -40,6 +40,7 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.Random;
+import java.util.function.BooleanSupplier;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -396,6 +397,52 @@
     }
 
     /**
+     * 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();
+    }
+
+    /**
      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
      * @param tOut the timeout value to be adjusted
      * @return The timeout value adjusted for the value of "test.timeout.factor"