Search in sources :

Example 36 with IntentionalException

use of io.pravega.test.common.IntentionalException in project pravega by pravega.

the class ExecutorServiceHelpersTests method testExecute.

/**
 * Tests the execute() method.
 */
@Test(timeout = 5000)
public void testExecute() {
    AtomicInteger runCount = new AtomicInteger();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    AtomicInteger finallyCount = new AtomicInteger();
    // Normal execution
    ExecutorServiceHelpers.execute(runCount::incrementAndGet, exceptionHolder::set, finallyCount::incrementAndGet, executorService());
    Assert.assertEquals("Unexpected number of runs (normal execution)", 1, runCount.get());
    Assert.assertNull("Unexpected exception set (normal execution)", exceptionHolder.get());
    Assert.assertEquals("Unexpected number of finally runs (normal execution)", 1, finallyCount.get());
    // Run with failure.
    runCount.set(0);
    exceptionHolder.set(null);
    finallyCount.set(0);
    ExecutorServiceHelpers.execute(() -> {
        throw new IntentionalException();
    }, exceptionHolder::set, finallyCount::incrementAndGet, executorService());
    Assert.assertTrue("Unexpected exception set (failed task)", exceptionHolder.get() instanceof IntentionalException);
    Assert.assertEquals("Unexpected number of finally runs (failed task)", 1, finallyCount.get());
    // Scheduling exception
    val closedExecutor = Executors.newSingleThreadExecutor();
    closedExecutor.shutdown();
    runCount.set(0);
    exceptionHolder.set(null);
    finallyCount.set(0);
    AssertExtensions.assertThrows("execute did not throw appropriate exception when executor was closed", () -> ExecutorServiceHelpers.execute(runCount::incrementAndGet, exceptionHolder::set, finallyCount::incrementAndGet, closedExecutor), ex -> ex instanceof RejectedExecutionException);
    Assert.assertEquals("Unexpected number of runs (rejected execution)", 0, runCount.get());
    Assert.assertNull("Unexpected exception set (rejected execution)", exceptionHolder.get());
    Assert.assertEquals("Unexpected number of finally runs (rejected execution)", 1, finallyCount.get());
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) IntentionalException(io.pravega.test.common.IntentionalException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 37 with IntentionalException

use of io.pravega.test.common.IntentionalException in project pravega by pravega.

the class FuturesTests method testCompleteAfter.

@Test
public void testCompleteAfter() {
    // Async exceptions (before call to completeAfter).
    val toFail1 = new CompletableFuture<Integer>();
    Futures.completeAfter(() -> Futures.failedFuture(new IntentionalException()), toFail1);
    Assert.assertTrue("Async exceptions were not propagated properly (before).", toFail1.isCompletedExceptionally());
    AssertExtensions.assertThrows("Unexpected async exception got propagated (before).", toFail1::join, ex -> ex instanceof IntentionalException);
    // Async exceptions (after call to completeAfter).
    val sourceToFail2 = new CompletableFuture<Integer>();
    val toFail2 = new CompletableFuture<Integer>();
    Futures.completeAfter(() -> sourceToFail2, toFail2);
    sourceToFail2.completeExceptionally(new IntentionalException());
    Assert.assertTrue("Async exceptions were not propagated properly (after).", toFail2.isCompletedExceptionally());
    AssertExtensions.assertThrows("Unexpected async exception got propagated (after).", toFail2::join, ex -> ex instanceof IntentionalException);
    // Sync exceptions.
    val toFail3 = new CompletableFuture<Integer>();
    AssertExtensions.assertThrows("Sync exception did not get rethrown.", () -> Futures.completeAfter(() -> {
        throw new IntentionalException();
    }, toFail3), ex -> ex instanceof IntentionalException);
    Assert.assertTrue("Sync exceptions were not propagated properly.", toFail3.isCompletedExceptionally());
    AssertExtensions.assertThrows("Unexpected sync exception got propagated.", toFail3::join, ex -> ex instanceof IntentionalException);
    // Normal completion (before call to completeAfter).
    val toComplete1 = new CompletableFuture<Integer>();
    Futures.completeAfter(() -> CompletableFuture.completedFuture(1), toComplete1);
    Assert.assertTrue("Normal completion did not happen (before).", Futures.isSuccessful(toComplete1));
    Assert.assertEquals("Unexpected value from normal completion (before).", 1, (int) toComplete1.join());
    // Normal completion (after call to completeAfter).
    val sourceToComplete2 = new CompletableFuture<Integer>();
    val toComplete2 = new CompletableFuture<Integer>();
    Futures.completeAfter(() -> sourceToComplete2, toComplete2);
    sourceToComplete2.complete(2);
    Assert.assertTrue("Normal completion did not happen (after).", Futures.isSuccessful(toComplete2));
    Assert.assertEquals("Unexpected value from normal completion (after).", 2, (int) toComplete2.join());
}
Also used : lombok.val(lombok.val) CompletableFuture(java.util.concurrent.CompletableFuture) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 38 with IntentionalException

use of io.pravega.test.common.IntentionalException in project pravega by pravega.

the class FuturesTests method testExceptionallyCompose.

/**
 * Tests the exceptionallyCompose method.
 */
@Test
public void testExceptionallyCompose() {
    // When applied to a CompletableFuture that completes normally.
    val successfulFuture = new CompletableFuture<Integer>();
    val f1 = Futures.<Integer>exceptionallyCompose(successfulFuture, ex -> CompletableFuture.completedFuture(2));
    successfulFuture.complete(1);
    Assert.assertEquals("Unexpected completion value for successful future.", 1, (int) f1.join());
    // When applied to a CompletableFuture that completes exceptionally.
    val failedFuture = new CompletableFuture<Integer>();
    val f2 = Futures.<Integer>exceptionallyCompose(failedFuture, ex -> CompletableFuture.completedFuture(2));
    failedFuture.completeExceptionally(new IntentionalException());
    Assert.assertEquals("Unexpected completion value for failed future that handled the exception.", 2, (int) f2.join());
    // When applied to a CompletableFuture that completes exceptionally and the handler also throws.
    val f3 = Futures.<Integer>exceptionallyCompose(failedFuture, ex -> {
        throw new IntentionalException();
    });
    AssertExtensions.assertThrows("Unexpected completion for failed future whose handler also threw an exception.", () -> f3, ex -> ex instanceof IntentionalException);
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 39 with IntentionalException

use of io.pravega.test.common.IntentionalException in project pravega by pravega.

the class FuturesTests method failRandomFuture.

private void failRandomFuture(List<CompletableFuture<Integer>> futures) {
    int index = new Random().nextInt(futures.size());
    futures.get(index).completeExceptionally(new IntentionalException());
}
Also used : Random(java.util.Random) IntentionalException(io.pravega.test.common.IntentionalException)

Example 40 with IntentionalException

use of io.pravega.test.common.IntentionalException 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 : Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Map(java.util.Map) Assert(org.junit.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Aggregations

IntentionalException (io.pravega.test.common.IntentionalException)40 Test (org.junit.Test)39 lombok.val (lombok.val)26 Cleanup (lombok.Cleanup)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Assert (org.junit.Assert)11 AssertExtensions (io.pravega.test.common.AssertExtensions)10 ArrayList (java.util.ArrayList)10 CompletableFuture (java.util.concurrent.CompletableFuture)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Duration (java.time.Duration)9 Rule (org.junit.Rule)9 Timeout (org.junit.rules.Timeout)9 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)8 StorageOperation (io.pravega.segmentstore.server.logs.operations.StorageOperation)8 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)8 HashSet (java.util.HashSet)8 Random (java.util.Random)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8