use of java.util.concurrent.CompletableFuture.completedFuture in project assertj-core by joel-costigliola.
the class CompletableFutureAssert_isCompletedWithValueMatching_Test method should_fail_if_result_does_not_match.
@Test
public void should_fail_if_result_does_not_match() {
// GIVEN
CompletableFuture<String> future = CompletableFuture.completedFuture("done");
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo"), "is foo"));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class).hasMessageContaining("<\"done\">").hasMessageContaining("to match 'is foo' predicate");
}
use of java.util.concurrent.CompletableFuture.completedFuture in project assertj-core by joel-costigliola.
the class CompletableFutureAssert_isCompletedWithValueMatching_Test method should_print_advice_without_description.
@Test
public void should_print_advice_without_description() {
// GIVEN
CompletableFuture<String> future = CompletableFuture.completedFuture("done");
// WHEN
Throwable throwable = catchThrowable(() -> assertThat(future).isCompletedWithValueMatching(result -> result.equals("foo")));
// THEN
assertThat(throwable).isInstanceOf(AssertionError.class).hasMessageContaining("<\"done\">").hasMessageContaining("to match given predicate").hasMessageContaining("a better error message");
}
use of java.util.concurrent.CompletableFuture.completedFuture in project pravega by pravega.
the class FuturesTests method testLoopIterable.
@Test
public void testLoopIterable() {
val list = IntStream.range(1, 10000).boxed().collect(Collectors.toList());
val processedList = Collections.synchronizedList(new ArrayList<Integer>());
Futures.loop(list, item -> {
processedList.add(item);
return CompletableFuture.completedFuture(true);
}, ForkJoinPool.commonPool()).join();
AssertExtensions.assertListEquals("Unexpected result.", list, processedList, Integer::equals);
}
use of java.util.concurrent.CompletableFuture.completedFuture in project pravega by pravega.
the class FuturesTests method testDoWhileLoopWithCondition.
@Test
public void testDoWhileLoopWithCondition() {
final int maxLoops = 10;
final int expectedResult = maxLoops * (maxLoops - 1) / 2;
AtomicInteger loopCounter = new AtomicInteger();
AtomicInteger accumulator = new AtomicInteger();
// 1. Verify this is actually a do-while loop vs a regular while loop.
Futures.doWhileLoop(() -> {
accumulator.incrementAndGet();
return CompletableFuture.completedFuture(0);
}, // Only one iteration.
x -> false, ForkJoinPool.commonPool()).join();
Assert.assertEquals("Unexpected result for loop without a specific accumulator.", 1, accumulator.get());
// 2. Successful execution.
loopCounter.set(0);
accumulator.set(0);
Futures.doWhileLoop(() -> {
int i = loopCounter.get();
accumulator.addAndGet(i);
return CompletableFuture.completedFuture(loopCounter.incrementAndGet());
}, x -> x < maxLoops, ForkJoinPool.commonPool()).join();
Assert.assertEquals("Unexpected result for loop without a specific accumulator.", expectedResult, accumulator.get());
// 3. With exceptions.
loopCounter.set(0);
accumulator.set(0);
CompletableFuture<Void> loopFuture = Futures.doWhileLoop(() -> {
if (loopCounter.incrementAndGet() % 3 == 0) {
throw new IntentionalException();
} else {
accumulator.addAndGet(loopCounter.get());
return CompletableFuture.completedFuture(loopCounter.get());
}
}, x -> x < maxLoops, ForkJoinPool.commonPool());
AssertExtensions.assertThrows("doWhileLoop() did not return a failed Future when one of the loopBody calls returned a failed Future.", loopFuture::join, ex -> ex instanceof IntentionalException);
Assert.assertEquals("Unexpected value accumulated until loop was interrupted.", 3, accumulator.get());
}
Aggregations