Search in sources :

Example 1 with CompletableFuture.completedFuture

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");
}
Also used : AssertionsForClassTypes.catchThrowable(org.assertj.core.api.AssertionsForClassTypes.catchThrowable) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ShouldBeCompleted.shouldBeCompleted(org.assertj.core.error.future.ShouldBeCompleted.shouldBeCompleted) FailureMessages.actualIsNull(org.assertj.core.util.FailureMessages.actualIsNull) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) WARNING(org.assertj.core.error.future.Warning.WARNING) String.format(java.lang.String.format) BaseTest(org.assertj.core.api.BaseTest) AssertionsForClassTypes.catchThrowable(org.assertj.core.api.AssertionsForClassTypes.catchThrowable) Test(org.junit.Test) BaseTest(org.assertj.core.api.BaseTest)

Example 2 with CompletableFuture.completedFuture

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");
}
Also used : AssertionsForClassTypes.catchThrowable(org.assertj.core.api.AssertionsForClassTypes.catchThrowable) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ShouldBeCompleted.shouldBeCompleted(org.assertj.core.error.future.ShouldBeCompleted.shouldBeCompleted) FailureMessages.actualIsNull(org.assertj.core.util.FailureMessages.actualIsNull) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) WARNING(org.assertj.core.error.future.Warning.WARNING) String.format(java.lang.String.format) BaseTest(org.assertj.core.api.BaseTest) AssertionsForClassTypes.catchThrowable(org.assertj.core.api.AssertionsForClassTypes.catchThrowable) Test(org.junit.Test) BaseTest(org.assertj.core.api.BaseTest)

Example 3 with CompletableFuture.completedFuture

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);
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Exceptions(io.pravega.common.Exceptions) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Futures.exceptionallyExpecting(io.pravega.common.concurrent.Futures.exceptionallyExpecting) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Futures.getThrowingException(io.pravega.common.concurrent.Futures.getThrowingException) Assert.fail(org.junit.Assert.fail) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) AssertExtensions.assertThrows(io.pravega.test.common.AssertExtensions.assertThrows) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert.assertFalse(org.junit.Assert.assertFalse) RandomFactory(io.pravega.common.hash.RandomFactory) Assert(org.junit.Assert) Collections(java.util.Collections) Futures.exceptionallyComposeExpecting(io.pravega.common.concurrent.Futures.exceptionallyComposeExpecting) Assert.assertEquals(org.junit.Assert.assertEquals) Test(org.junit.Test)

Example 4 with CompletableFuture.completedFuture

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());
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Exceptions(io.pravega.common.Exceptions) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Futures.exceptionallyExpecting(io.pravega.common.concurrent.Futures.exceptionallyExpecting) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Futures.getThrowingException(io.pravega.common.concurrent.Futures.getThrowingException) Assert.fail(org.junit.Assert.fail) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) AssertExtensions.assertThrows(io.pravega.test.common.AssertExtensions.assertThrows) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert.assertFalse(org.junit.Assert.assertFalse) RandomFactory(io.pravega.common.hash.RandomFactory) Assert(org.junit.Assert) Collections(java.util.Collections) Futures.exceptionallyComposeExpecting(io.pravega.common.concurrent.Futures.exceptionallyComposeExpecting) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)4 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)4 Test (org.junit.Test)4 Exceptions (io.pravega.common.Exceptions)2 Futures.exceptionallyComposeExpecting (io.pravega.common.concurrent.Futures.exceptionallyComposeExpecting)2 Futures.exceptionallyExpecting (io.pravega.common.concurrent.Futures.exceptionallyExpecting)2 Futures.getAndHandleExceptions (io.pravega.common.concurrent.Futures.getAndHandleExceptions)2 Futures.getThrowingException (io.pravega.common.concurrent.Futures.getThrowingException)2 RandomFactory (io.pravega.common.hash.RandomFactory)2 AssertExtensions (io.pravega.test.common.AssertExtensions)2 AssertExtensions.assertThrows (io.pravega.test.common.AssertExtensions.assertThrows)2 IntentionalException (io.pravega.test.common.IntentionalException)2 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)2 String.format (java.lang.String.format)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2