Skip to content

Commit

Permalink
RATIS-2057. Add back JavaUtils.attemptUntilTrue(..). (apache#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
szetszwo committed Jun 16, 2024
1 parent b9d0075 commit 84dbe1f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ratis-common/src/main/java/org/apache/ratis/util/JavaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -244,6 +245,18 @@ static <THROWABLE extends Throwable> void attempt(
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() {
Expand Down

0 comments on commit 84dbe1f

Please sign in to comment.