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

keith-turner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a19f4a075 adds Wait.waitUntil (#6690)
8a19f4a075 is described below

commit 8a19f4a075f7afc9fc4d08f1a18dae448a6ec3cb
Author: Keith Turner <[email protected]>
AuthorDate: Fri Dec 4 17:28:51 2026 -0500

    adds Wait.waitUntil (#6690)
    
    When writing Accumulo tests and using the Wait class its not uncommon to
    need something returned from the wait.  Added a new method that can
    return something from waiting.
---
 .../compaction/ExternalCompactionTestUtils.java    | 13 ++--
 .../java/org/apache/accumulo/test/util/Wait.java   | 91 ++++++++++++++++++++--
 2 files changed, 90 insertions(+), 14 deletions(-)

diff --git 
a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionTestUtils.java
 
b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionTestUtils.java
index 3d169ccb76..48803327a8 100644
--- 
a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionTestUtils.java
+++ 
b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionTestUtils.java
@@ -272,17 +272,14 @@ public class ExternalCompactionTestUtils {
 
   public static Set<ExternalCompactionId> 
waitForCompactionStartAndReturnEcids(ServerContext ctx,
       TableId tid) {
-    Set<ExternalCompactionId> ecids = new HashSet<>();
-    do {
+    return Wait.waitUntil(() -> {
       try (TabletsMetadata tm =
           
ctx.getAmple().readTablets().forTable(tid).fetch(ColumnType.ECOMP).build()) {
-        tm.stream().flatMap(t -> 
t.getExternalCompactions().keySet().stream()).forEach(ecids::add);
+        var ecids = tm.stream().flatMap(t -> 
t.getExternalCompactions().keySet().stream())
+            .collect(Collectors.toUnmodifiableSet());
+        return ecids.isEmpty() ? null : ecids;
       }
-      if (ecids.isEmpty()) {
-        UtilWaitThread.sleep(50);
-      }
-    } while (ecids.isEmpty());
-    return ecids;
+    }, 600_000, 50);
   }
 
   public static long countTablets(ServerContext ctx, String tableName,
diff --git a/test/src/main/java/org/apache/accumulo/test/util/Wait.java 
b/test/src/main/java/org/apache/accumulo/test/util/Wait.java
index 264e7d1a3a..a5017387c5 100644
--- a/test/src/main/java/org/apache/accumulo/test/util/Wait.java
+++ b/test/src/main/java/org/apache/accumulo/test/util/Wait.java
@@ -21,10 +21,13 @@ package org.apache.accumulo.test.util;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static java.util.concurrent.TimeUnit.SECONDS;
 
+import java.util.concurrent.Callable;
 import java.util.function.ToIntFunction;
 
 import org.apache.accumulo.core.util.CountDownTimer;
 
+import com.google.common.base.Preconditions;
+
 public class Wait {
 
   public static final long MAX_WAIT_MILLIS = SECONDS.toMillis(30);
@@ -68,6 +71,18 @@ public class Wait {
     waitFor(condition, MAX_WAIT_MILLIS);
   }
 
+  /**
+   * Wait for the provided callable to return non-null - will throw an 
IllegalStateException if the
+   * wait exceeds the default wait period of {@link #MAX_WAIT_MILLIS} with a 
retry period of
+   * {@link #SLEEP_MILLIS}. Both are scaled by the timeout factor.
+   *
+   * @param callable when condition evaluates to non-null, return from wait
+   * @return the first non-null value returned from the callable
+   */
+  public static <T> T waitUntil(final Callable<T> callable) {
+    return waitUntil(callable, MAX_WAIT_MILLIS);
+  }
+
   /**
    * Wait for the provided condition - will throw an IllegalStateException if 
the wait exceeds the
    * default wait period of {@link #MAX_WAIT_MILLIS} with a retry period of 
{@link #SLEEP_MILLIS}.
@@ -81,6 +96,20 @@ public class Wait {
     waitFor(condition, MAX_WAIT_MILLIS, SLEEP_MILLIS, failMessage);
   }
 
+  /**
+   * Wait for the provided callable to return non-null - will throw an 
IllegalStateException if the
+   * wait exceeds the default wait period of {@link #MAX_WAIT_MILLIS} with a 
retry period of
+   * {@link #SLEEP_MILLIS}. Both are scaled by the timeout factor.
+   *
+   * @param callable when condition evaluates to non-null, return from wait
+   * @param failMessage optional message to include in IllegalStateException 
if condition not met
+   *        before expiration.
+   * @return the first non-null value returned from the callable
+   */
+  public static <T> T waitUntil(final Callable<T> callable, final String 
failMessage) {
+    return waitUntil(callable, MAX_WAIT_MILLIS, SLEEP_MILLIS, failMessage);
+  }
+
   /**
    * Wait for the provided condition - will throw an IllegalStateException if 
the wait exceeds the
    * given wait duration with a default retry period of {@link #SLEEP_MILLIS}. 
Both are scaled by
@@ -93,6 +122,19 @@ public class Wait {
     waitFor(condition, duration, SLEEP_MILLIS);
   }
 
+  /**
+   * Wait for the provided callable to return non-null - will throw an 
IllegalStateException if the
+   * wait exceeds the given wait duration with a default retry period of 
{@link #SLEEP_MILLIS}. Both
+   * are scaled by the timeout factor.
+   *
+   * @param callable when condition evaluates to non-null, return from wait
+   * @param duration maximum total time to wait (milliseconds)
+   * @return the first non-null value returned from the callable
+   */
+  public static <T> T waitUntil(final Callable<T> callable, final long 
duration) {
+    return waitUntil(callable, duration, SLEEP_MILLIS);
+  }
+
   /**
    * Wait for the provided condition - will throw an IllegalStateException if 
the wait exceeds the
    * given wait duration.
@@ -106,6 +148,20 @@ public class Wait {
     waitFor(condition, duration, sleepMillis, "");
   }
 
+  /**
+   * Wait for the provided callable to return non-null - will throw an 
IllegalStateException if the
+   * wait exceeds the given wait duration.
+   *
+   * @param callable when condition evaluates to non-null, return from wait
+   * @param duration maximum total time to wait (milliseconds)
+   * @param sleepMillis time to sleep between condition checks
+   * @return the first non-null value returned from the callable
+   */
+  public static <T> T waitUntil(final Callable<T> callable, final long 
duration,
+      final long sleepMillis) {
+    return waitUntil(callable, duration, sleepMillis, "");
+  }
+
   /**
    * Wait for the provided condition - will throw an IllegalStateException if 
the wait exceeds the
    * given wait duration.
@@ -118,6 +174,23 @@ public class Wait {
    */
   public static void waitFor(final Condition condition, final long duration, 
final long sleepMillis,
       final String failMessage) {
+    Callable<String> callable = () -> condition.isSatisfied() ? "" : null;
+    Preconditions.checkState(waitUntil(callable, duration, sleepMillis, 
failMessage).isEmpty());
+  }
+
+  /**
+   * Wait for the provided callable to return non-null - will throw an 
IllegalStateException if the
+   * wait exceeds the given wait duration.
+   *
+   * @param callable when condition evaluates to non null, return from wait
+   * @param duration maximum total time to wait (milliseconds)
+   * @param sleepMillis time to sleep between condition checks
+   * @param failMessage optional message to include in IllegalStateException 
if condition not met
+   *        before expiration.
+   * @return the first non-null value returned from the callable
+   */
+  public static <T> T waitUntil(final Callable<T> callable, final long 
duration,
+      final long sleepMillis, final String failMessage) {
 
     final int timeoutFactor = getTimeoutFactor(e -> 1); // default to factor 
of 1
 
@@ -125,23 +198,29 @@ public class Wait {
     final long scaledSleepMillis = multiplyClampToRange(sleepMillis, 
timeoutFactor);
 
     final CountDownTimer maxWaitTimer = 
CountDownTimer.startNew(scaledWaitMillis, MILLISECONDS);
-    boolean success;
+    T value = null;
     try {
-      success = condition.isSatisfied();
-      while (!success && !maxWaitTimer.isExpired()) {
+      value = callable.call();
+      while (value == null && !maxWaitTimer.isExpired()) {
         MILLISECONDS.sleep(scaledSleepMillis);
-        success = condition.isSatisfied();
+        value = callable.call();
       }
     } catch (InterruptedException ex) {
       Thread.currentThread().interrupt();
-      throw new IllegalStateException("Interrupted during wait");
+      throw new IllegalStateException(failMessage + " Interrupted during 
wait", ex);
     } catch (Exception ex) {
       throw new IllegalStateException(failMessage + ". Failed because of 
exception in condition",
           ex);
     }
-    if (!success) {
+
+    Preconditions.checkArgument(!(value instanceof Boolean),
+        "Use waitFor() instead of waitUntil() for booleans.");
+
+    if (value == null) {
       throw new IllegalStateException(failMessage + ". Timeout exceeded");
     }
+
+    return value;
   }
 
   /**

Reply via email to