0
# Condition Evaluation
1
2
Different ways to specify and evaluate wait conditions, including callable evaluation, predicate matching, assertion-based testing, and atomic variable monitoring.
3
4
## Capabilities
5
6
### Callable with Matcher
7
8
Wait until a callable's return value matches a Hamcrest matcher.
9
10
```java { .api }
11
/**
12
* Wait until callable return value matches the given matcher
13
* @param supplier callable that provides values to test
14
* @param matcher Hamcrest matcher for value evaluation
15
* @return the final value returned by supplier when matcher succeeds
16
*/
17
<T> T until(Callable<T> supplier, Matcher<? super T> matcher);
18
```
19
20
**Usage Examples:**
21
```java
22
// Wait until order count is greater than 5
23
Integer count = await().until(() -> orderService.getOrderCount(), greaterThan(5));
24
25
// Wait until customer name contains "Smith"
26
String name = await().until(() -> customer.getName(), containsString("Smith"));
27
28
// Wait until list has specific size
29
List<Order> orders = await().until(() -> orderService.getAllOrders(), hasSize(3));
30
31
// Wait until response status is OK
32
HttpStatus status = await().atMost(10, SECONDS)
33
.until(() -> httpClient.getStatus(), equalTo(HttpStatus.OK));
34
```
35
36
### Callable with Predicate
37
38
Wait until a callable's return value satisfies a predicate function.
39
40
```java { .api }
41
/**
42
* Wait until callable return value satisfies the predicate
43
* @param supplier callable that provides values to test
44
* @param predicate function that tests the value
45
* @return the final value returned by supplier when predicate succeeds
46
*/
47
<T> T until(Callable<T> supplier, Predicate<? super T> predicate);
48
```
49
50
**Usage Examples:**
51
```java
52
// Wait until temperature is above threshold
53
Double temp = await().until(() -> sensor.getTemperature(), t -> t > 25.0);
54
55
// Wait until user is active and verified
56
User user = await().until(() -> userService.getUser(id),
57
u -> u.isActive() && u.isVerified());
58
59
// Wait until collection is not empty
60
List<Message> messages = await().until(() -> messageQueue.getMessages(),
61
msgs -> !msgs.isEmpty());
62
```
63
64
### Boolean Callable
65
66
Wait until a callable returns true.
67
68
```java { .api }
69
/**
70
* Wait until callable returns true
71
* @param conditionEvaluator callable that returns boolean
72
*/
73
void until(Callable<Boolean> conditionEvaluator);
74
```
75
76
**Usage Examples:**
77
```java
78
// Wait until service is ready
79
await().until(() -> healthCheckService.isHealthy());
80
81
// Wait until file exists
82
await().until(() -> Files.exists(Paths.get("/tmp/output.txt")));
83
84
// Wait until database connection is available
85
await().atMost(30, SECONDS).until(() -> {
86
try {
87
return dataSource.getConnection().isValid(1);
88
} catch (SQLException e) {
89
return false;
90
}
91
});
92
```
93
94
### Assertion-Based Conditions
95
96
Wait until an assertion passes without throwing an exception.
97
98
```java { .api }
99
/**
100
* Wait until assertion passes (doesn't throw exception)
101
* @param assertion runnable that performs assertions
102
*/
103
void untilAsserted(ThrowingRunnable assertion);
104
105
/**
106
* Wait until assertion on supplier value passes
107
* @param supplier provides values for assertion
108
* @param assertConsumer performs assertions on supplied values
109
*/
110
<T> void untilAsserted(Callable<T> supplier, Consumer<? super T> assertConsumer);
111
```
112
113
**Usage Examples:**
114
```java
115
// Wait until assertion passes
116
await().untilAsserted(() -> {
117
assertThat(orderService.getCompletedOrders()).hasSize(5);
118
assertThat(orderService.getPendingOrders()).isEmpty();
119
});
120
121
// Wait until supplier value passes assertion
122
await().untilAsserted(() -> customerService.getCustomer(id), customer -> {
123
assertThat(customer.getStatus()).isEqualTo(CustomerStatus.VERIFIED);
124
assertThat(customer.getEmail()).isNotNull();
125
});
126
127
// Using JUnit assertions
128
await().untilAsserted(() -> {
129
assertEquals(ProcessingState.COMPLETED, processor.getState());
130
assertTrue(processor.hasResults());
131
});
132
```
133
134
### Atomic Variable Conditions
135
136
Specialized methods for waiting on atomic variable changes.
137
138
```java { .api }
139
/**
140
* Wait until AtomicInteger matches condition
141
* @param atomic the atomic integer to monitor
142
* @param matcher condition for the integer value
143
* @return final value when condition is met
144
*/
145
Integer untilAtomic(AtomicInteger atomic, Matcher<? super Integer> matcher);
146
147
/**
148
* Wait until AtomicInteger passes consumer assertion
149
* @param atomic the atomic integer to monitor
150
* @param matcher consumer that tests the integer value
151
*/
152
void untilAtomic(AtomicInteger atomic, Consumer<? super Integer> matcher);
153
154
/**
155
* Wait until AtomicLong matches condition
156
* @param atomic the atomic long to monitor
157
* @param matcher condition for the long value
158
* @return final value when condition is met
159
*/
160
Long untilAtomic(AtomicLong atomic, Matcher<? super Long> matcher);
161
162
/**
163
* Wait until AtomicLong passes consumer assertion
164
* @param atomic the atomic long to monitor
165
* @param matcher consumer that tests the long value
166
*/
167
void untilAtomic(AtomicLong atomic, Consumer<? super Long> matcher);
168
169
/**
170
* Wait until AtomicBoolean matches condition
171
* @param atomic the atomic boolean to monitor
172
* @param matcher condition for the boolean value
173
*/
174
void untilAtomic(AtomicBoolean atomic, Matcher<? super Boolean> matcher);
175
176
/**
177
* Wait until AtomicBoolean passes consumer assertion
178
* @param atomic the atomic boolean to monitor
179
* @param matcher consumer that tests the boolean value
180
*/
181
void untilAtomic(AtomicBoolean atomic, Consumer<? super Boolean> matcher);
182
183
/**
184
* Wait until AtomicReference matches condition
185
* @param atomic the atomic reference to monitor
186
* @param matcher condition for the reference value
187
* @return final value when condition is met
188
*/
189
<V> V untilAtomic(AtomicReference<V> atomic, Matcher<? super V> matcher);
190
191
/**
192
* Wait until AtomicReference passes consumer assertion
193
* @param atomic the atomic reference to monitor
194
* @param matcher consumer that tests the reference value
195
*/
196
<V> void untilAtomic(AtomicReference<V> atomic, Consumer<? super V> matcher);
197
```
198
199
### Atomic Boolean Convenience Methods
200
201
```java { .api }
202
/**
203
* Wait until AtomicBoolean becomes true
204
* @param atomic the atomic boolean to monitor
205
*/
206
void untilTrue(AtomicBoolean atomic);
207
208
/**
209
* Wait until AtomicBoolean becomes false
210
* @param atomic the atomic boolean to monitor
211
*/
212
void untilFalse(AtomicBoolean atomic);
213
```
214
215
**Atomic Usage Examples:**
216
```java
217
// Wait for counter to reach threshold
218
AtomicInteger counter = new AtomicInteger(0);
219
await().untilAtomic(counter, greaterThanOrEqualTo(10));
220
221
// Wait for flag to be set
222
AtomicBoolean ready = new AtomicBoolean(false);
223
await().untilTrue(ready);
224
225
// Wait for reference to be non-null
226
AtomicReference<String> result = new AtomicReference<>();
227
String value = await().untilAtomic(result, notNullValue());
228
229
// Wait with assertion
230
AtomicLong timestamp = new AtomicLong(0);
231
await().untilAtomic(timestamp, ts -> assertThat(ts).isGreaterThan(System.currentTimeMillis()));
232
```
233
234
### Adder and Accumulator Support
235
236
Support for Java 8+ LongAdder, DoubleAdder, and accumulator classes.
237
238
```java { .api }
239
/**
240
* Wait until LongAdder matches condition
241
* @param adder the long adder to monitor
242
* @param matcher condition for the sum value
243
*/
244
void untilAdder(LongAdder adder, Matcher<? super Long> matcher);
245
246
/**
247
* Wait until LongAdder passes consumer assertion
248
* @param adder the long adder to monitor
249
* @param matcher consumer that tests the sum value
250
*/
251
void untilAdder(LongAdder adder, Consumer<? super Long> matcher);
252
253
/**
254
* Wait until DoubleAdder matches condition
255
* @param adder the double adder to monitor
256
* @param matcher condition for the sum value
257
*/
258
void untilAdder(DoubleAdder adder, Matcher<? super Double> matcher);
259
260
/**
261
* Wait until DoubleAdder passes consumer assertion
262
* @param adder the double adder to monitor
263
* @param matcher consumer that tests the sum value
264
*/
265
void untilAdder(DoubleAdder adder, Consumer<? super Double> matcher);
266
267
/**
268
* Wait until LongAccumulator matches condition
269
* @param accumulator the long accumulator to monitor
270
* @param matcher condition for the accumulated value
271
*/
272
void untilAccumulator(LongAccumulator accumulator, Matcher<? super Long> matcher);
273
274
/**
275
* Wait until LongAccumulator passes consumer assertion
276
* @param accumulator the long accumulator to monitor
277
* @param matcher consumer that tests the accumulated value
278
*/
279
void untilAccumulator(LongAccumulator accumulator, Consumer<? super Long> matcher);
280
281
/**
282
* Wait until DoubleAccumulator matches condition
283
* @param accumulator the double accumulator to monitor
284
* @param matcher condition for the accumulated value
285
*/
286
void untilAccumulator(DoubleAccumulator accumulator, Matcher<? super Double> matcher);
287
288
/**
289
* Wait until DoubleAccumulator passes consumer assertion
290
* @param accumulator the double accumulator to monitor
291
* @param matcher consumer that tests the accumulated value
292
*/
293
void untilAccumulator(DoubleAccumulator accumulator, Consumer<? super Double> matcher);
294
```
295
296
**Adder/Accumulator Usage Examples:**
297
```java
298
// Wait for adder sum to reach threshold
299
LongAdder requestCount = new LongAdder();
300
await().untilAdder(requestCount, greaterThan(1000L));
301
302
// Wait for accumulator maximum
303
LongAccumulator maxValue = new LongAccumulator(Long::max, Long.MIN_VALUE);
304
await().untilAccumulator(maxValue, greaterThan(50L));
305
306
// Wait with assertion
307
DoubleAdder totalAmount = new DoubleAdder();
308
await().untilAdder(totalAmount, amount -> {
309
assertThat(amount).isGreaterThan(10000.0);
310
assertThat(amount).isLessThan(50000.0);
311
});
312
```