CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-awaitility--awaitility

A Java DSL for synchronizing asynchronous operations

Pending
Overview
Eval results
Files

condition-evaluation.mddocs/

Condition Evaluation

Different ways to specify and evaluate wait conditions, including callable evaluation, predicate matching, assertion-based testing, and atomic variable monitoring.

Capabilities

Callable with Matcher

Wait until a callable's return value matches a Hamcrest matcher.

/**
 * Wait until callable return value matches the given matcher
 * @param supplier callable that provides values to test
 * @param matcher Hamcrest matcher for value evaluation
 * @return the final value returned by supplier when matcher succeeds
 */
<T> T until(Callable<T> supplier, Matcher<? super T> matcher);

Usage Examples:

// Wait until order count is greater than 5
Integer count = await().until(() -> orderService.getOrderCount(), greaterThan(5));

// Wait until customer name contains "Smith"
String name = await().until(() -> customer.getName(), containsString("Smith"));

// Wait until list has specific size
List<Order> orders = await().until(() -> orderService.getAllOrders(), hasSize(3));

// Wait until response status is OK
HttpStatus status = await().atMost(10, SECONDS)
    .until(() -> httpClient.getStatus(), equalTo(HttpStatus.OK));

Callable with Predicate

Wait until a callable's return value satisfies a predicate function.

/**
 * Wait until callable return value satisfies the predicate
 * @param supplier callable that provides values to test
 * @param predicate function that tests the value
 * @return the final value returned by supplier when predicate succeeds
 */
<T> T until(Callable<T> supplier, Predicate<? super T> predicate);

Usage Examples:

// Wait until temperature is above threshold
Double temp = await().until(() -> sensor.getTemperature(), t -> t > 25.0);

// Wait until user is active and verified
User user = await().until(() -> userService.getUser(id), 
    u -> u.isActive() && u.isVerified());

// Wait until collection is not empty
List<Message> messages = await().until(() -> messageQueue.getMessages(),
    msgs -> !msgs.isEmpty());

Boolean Callable

Wait until a callable returns true.

/**
 * Wait until callable returns true
 * @param conditionEvaluator callable that returns boolean
 */
void until(Callable<Boolean> conditionEvaluator);

Usage Examples:

// Wait until service is ready
await().until(() -> healthCheckService.isHealthy());

// Wait until file exists
await().until(() -> Files.exists(Paths.get("/tmp/output.txt")));

// Wait until database connection is available
await().atMost(30, SECONDS).until(() -> {
    try {
        return dataSource.getConnection().isValid(1);
    } catch (SQLException e) {
        return false;
    }
});

Assertion-Based Conditions

Wait until an assertion passes without throwing an exception.

/**
 * Wait until assertion passes (doesn't throw exception)
 * @param assertion runnable that performs assertions
 */
void untilAsserted(ThrowingRunnable assertion);

/**
 * Wait until assertion on supplier value passes
 * @param supplier provides values for assertion
 * @param assertConsumer performs assertions on supplied values
 */
<T> void untilAsserted(Callable<T> supplier, Consumer<? super T> assertConsumer);

Usage Examples:

// Wait until assertion passes
await().untilAsserted(() -> {
    assertThat(orderService.getCompletedOrders()).hasSize(5);
    assertThat(orderService.getPendingOrders()).isEmpty();
});

// Wait until supplier value passes assertion
await().untilAsserted(() -> customerService.getCustomer(id), customer -> {
    assertThat(customer.getStatus()).isEqualTo(CustomerStatus.VERIFIED);
    assertThat(customer.getEmail()).isNotNull();
});

// Using JUnit assertions
await().untilAsserted(() -> {
    assertEquals(ProcessingState.COMPLETED, processor.getState());
    assertTrue(processor.hasResults());
});

Atomic Variable Conditions

Specialized methods for waiting on atomic variable changes.

/**
 * Wait until AtomicInteger matches condition
 * @param atomic the atomic integer to monitor
 * @param matcher condition for the integer value
 * @return final value when condition is met
 */
Integer untilAtomic(AtomicInteger atomic, Matcher<? super Integer> matcher);

/**
 * Wait until AtomicInteger passes consumer assertion
 * @param atomic the atomic integer to monitor  
 * @param matcher consumer that tests the integer value
 */
void untilAtomic(AtomicInteger atomic, Consumer<? super Integer> matcher);

/**
 * Wait until AtomicLong matches condition
 * @param atomic the atomic long to monitor
 * @param matcher condition for the long value
 * @return final value when condition is met
 */
Long untilAtomic(AtomicLong atomic, Matcher<? super Long> matcher);

/**
 * Wait until AtomicLong passes consumer assertion
 * @param atomic the atomic long to monitor
 * @param matcher consumer that tests the long value
 */
void untilAtomic(AtomicLong atomic, Consumer<? super Long> matcher);

/**
 * Wait until AtomicBoolean matches condition
 * @param atomic the atomic boolean to monitor
 * @param matcher condition for the boolean value
 */
void untilAtomic(AtomicBoolean atomic, Matcher<? super Boolean> matcher);

/**
 * Wait until AtomicBoolean passes consumer assertion
 * @param atomic the atomic boolean to monitor
 * @param matcher consumer that tests the boolean value
 */
void untilAtomic(AtomicBoolean atomic, Consumer<? super Boolean> matcher);

/**
 * Wait until AtomicReference matches condition
 * @param atomic the atomic reference to monitor
 * @param matcher condition for the reference value
 * @return final value when condition is met
 */
<V> V untilAtomic(AtomicReference<V> atomic, Matcher<? super V> matcher);

/**
 * Wait until AtomicReference passes consumer assertion
 * @param atomic the atomic reference to monitor
 * @param matcher consumer that tests the reference value
 */
<V> void untilAtomic(AtomicReference<V> atomic, Consumer<? super V> matcher);

Atomic Boolean Convenience Methods

/**
 * Wait until AtomicBoolean becomes true
 * @param atomic the atomic boolean to monitor
 */
void untilTrue(AtomicBoolean atomic);

/**
 * Wait until AtomicBoolean becomes false
 * @param atomic the atomic boolean to monitor
 */
void untilFalse(AtomicBoolean atomic);

Atomic Usage Examples:

// Wait for counter to reach threshold
AtomicInteger counter = new AtomicInteger(0);
await().untilAtomic(counter, greaterThanOrEqualTo(10));

// Wait for flag to be set
AtomicBoolean ready = new AtomicBoolean(false);
await().untilTrue(ready);

// Wait for reference to be non-null
AtomicReference<String> result = new AtomicReference<>();
String value = await().untilAtomic(result, notNullValue());

// Wait with assertion
AtomicLong timestamp = new AtomicLong(0);
await().untilAtomic(timestamp, ts -> assertThat(ts).isGreaterThan(System.currentTimeMillis()));

Adder and Accumulator Support

Support for Java 8+ LongAdder, DoubleAdder, and accumulator classes.

/**
 * Wait until LongAdder matches condition
 * @param adder the long adder to monitor
 * @param matcher condition for the sum value
 */
void untilAdder(LongAdder adder, Matcher<? super Long> matcher);

/**
 * Wait until LongAdder passes consumer assertion  
 * @param adder the long adder to monitor
 * @param matcher consumer that tests the sum value
 */
void untilAdder(LongAdder adder, Consumer<? super Long> matcher);

/**
 * Wait until DoubleAdder matches condition
 * @param adder the double adder to monitor
 * @param matcher condition for the sum value
 */
void untilAdder(DoubleAdder adder, Matcher<? super Double> matcher);

/**
 * Wait until DoubleAdder passes consumer assertion
 * @param adder the double adder to monitor
 * @param matcher consumer that tests the sum value
 */
void untilAdder(DoubleAdder adder, Consumer<? super Double> matcher);

/**
 * Wait until LongAccumulator matches condition
 * @param accumulator the long accumulator to monitor
 * @param matcher condition for the accumulated value
 */
void untilAccumulator(LongAccumulator accumulator, Matcher<? super Long> matcher);

/**
 * Wait until LongAccumulator passes consumer assertion
 * @param accumulator the long accumulator to monitor
 * @param matcher consumer that tests the accumulated value
 */
void untilAccumulator(LongAccumulator accumulator, Consumer<? super Long> matcher);

/**
 * Wait until DoubleAccumulator matches condition
 * @param accumulator the double accumulator to monitor
 * @param matcher condition for the accumulated value
 */
void untilAccumulator(DoubleAccumulator accumulator, Matcher<? super Double> matcher);

/**
 * Wait until DoubleAccumulator passes consumer assertion
 * @param accumulator the double accumulator to monitor
 * @param matcher consumer that tests the accumulated value
 */
void untilAccumulator(DoubleAccumulator accumulator, Consumer<? super Double> matcher);

Adder/Accumulator Usage Examples:

// Wait for adder sum to reach threshold
LongAdder requestCount = new LongAdder();
await().untilAdder(requestCount, greaterThan(1000L));

// Wait for accumulator maximum
LongAccumulator maxValue = new LongAccumulator(Long::max, Long.MIN_VALUE);
await().untilAccumulator(maxValue, greaterThan(50L));

// Wait with assertion
DoubleAdder totalAmount = new DoubleAdder();
await().untilAdder(totalAmount, amount -> {
    assertThat(amount).isGreaterThan(10000.0);
    assertThat(amount).isLessThan(50000.0);
});

Install with Tessl CLI

npx tessl i tessl/maven-org-awaitility--awaitility

docs

advanced-config.md

atomic-variables.md

condition-evaluation.md

core-await.md

exception-handling.md

field-reflection.md

index.md

timeout-polling.md

tile.json