This is an automated email from the ASF dual-hosted git repository.

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new cf9d2292a RATIS-2057. Add back JavaUtils.attemptUntilTrue(..). (#1066)
cf9d2292a is described below

commit cf9d2292a9f53792a7765da41e3b4b903c0ec1d9
Author: Tsz-Wo Nicholas Sze <[email protected]>
AuthorDate: Mon Apr 15 23:28:53 2024 -0700

    RATIS-2057. Add back JavaUtils.attemptUntilTrue(..). (#1066)
---
 .../src/main/java/org/apache/ratis/util/JavaUtils.java      | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java 
b/ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java
index f689006db..7d1d75309 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java
@@ -41,6 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
 import java.util.function.BiConsumer;
+import java.util.function.BooleanSupplier;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Supplier;
@@ -244,6 +245,18 @@ public interface JavaUtils {
     attemptRepeatedly(CheckedRunnable.asCheckedSupplier(runnable), 
numAttempts, sleepTime, name, log);
   }
 
+  /** Attempt to wait the given condition to return true multiple times. */
+  static void attemptUntilTrue(
+      BooleanSupplier condition, int numAttempts, TimeDuration sleepTime, 
String name, Logger log)
+      throws InterruptedException {
+    Objects.requireNonNull(condition, "condition == null");
+    attempt(() -> {
+      if (!condition.getAsBoolean()) {
+        throw new IllegalStateException("Condition " + name + " is false.");
+      }
+    }, numAttempts, sleepTime, name, log);
+  }
+
   static Timer runRepeatedly(Runnable runnable, long delay, long period, 
TimeUnit unit) {
     final Timer timer = new Timer(true);
     timer.schedule(new TimerTask() {

Reply via email to