Search in sources :

Example 11 with IntentionalException

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

the class FuturesTests method testExceptionListener.

/**
 * Tests the exceptionListener() method.
 */
@Test
public void testExceptionListener() {
    AtomicReference<Throwable> thrownException = new AtomicReference<>();
    CompletableFuture<Void> cf = new CompletableFuture<>();
    Futures.exceptionListener(cf, thrownException::set);
    cf.complete(null);
    Assert.assertNull("exceptionListener invoked the callback when the future was completed normally.", thrownException.get());
    thrownException.set(null);
    cf = new CompletableFuture<>();
    Exception ex = new IntentionalException();
    Futures.exceptionListener(cf, thrownException::set);
    cf.completeExceptionally(ex);
    Assert.assertNotNull("exceptionListener did not invoke the callback when the future was completed exceptionally.", thrownException.get());
    Assert.assertEquals("Unexpected exception was passed to the callback from exceptionListener when the future was completed exceptionally.", ex, thrownException.get());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) IntentionalException(io.pravega.test.common.IntentionalException) ExecutionException(java.util.concurrent.ExecutionException) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 12 with IntentionalException

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

the class FuturesTests method testLoop.

@Test
public void testLoop() {
    final int maxLoops = 10;
    final int expectedResult = maxLoops * (maxLoops - 1) / 2;
    AtomicInteger loopCounter = new AtomicInteger();
    AtomicInteger accumulator = new AtomicInteger();
    // 1. With no specific accumulator.
    Futures.loop(() -> loopCounter.incrementAndGet() < maxLoops, () -> {
        accumulator.addAndGet(loopCounter.get());
        return CompletableFuture.completedFuture(null);
    }, ForkJoinPool.commonPool()).join();
    Assert.assertEquals("Unexpected result for loop without a specific accumulator.", expectedResult, accumulator.get());
    // 2. With specific accumulator.
    loopCounter.set(0);
    accumulator.set(0);
    Futures.loop(() -> loopCounter.incrementAndGet() < maxLoops, () -> CompletableFuture.completedFuture(loopCounter.get()), accumulator::addAndGet, ForkJoinPool.commonPool()).join();
    Assert.assertEquals("Unexpected result for loop with a specific accumulator.", expectedResult, accumulator.get());
    // 3. With exceptions.
    loopCounter.set(0);
    accumulator.set(0);
    CompletableFuture<Void> loopFuture = Futures.loop(() -> loopCounter.incrementAndGet() < maxLoops, () -> {
        if (loopCounter.get() % 3 == 0) {
            throw new IntentionalException();
        } else {
            accumulator.addAndGet(loopCounter.get());
            return CompletableFuture.completedFuture(null);
        }
    }, ForkJoinPool.commonPool());
    AssertExtensions.assertThrows("loop() 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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 13 with IntentionalException

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

the class FuturesTests method testFilterException.

/**
 * Test method for Futures.filter when the FuturePredicate completes exceptionally in future.
 */
@Test
public void testFilterException() {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    Function<Integer, CompletableFuture<Boolean>> futureEvenFilter = x -> Futures.failedFuture(new IntentionalException("intentional"));
    AssertExtensions.assertThrows("Unexpected behavior when filter threw an exception.", () -> Futures.filter(list, futureEvenFilter), ex -> ex instanceof IntentionalException);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) CompletableFuture(java.util.concurrent.CompletableFuture) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 14 with IntentionalException

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

the class SequentialAsyncProcessorTests method testRunAsyncErrors.

/**
 * Tests the runAsync() method with execution errors.
 */
@Test(timeout = TIMEOUT_MILLIS)
public void testRunAsyncErrors() throws Exception {
    final int expectedCount = 2;
    val count = new AtomicInteger();
    val finished = new CompletableFuture<Void>();
    val retry = Retry.withExpBackoff(1, 2, expectedCount).retryWhen(t -> {
        if (count.get() >= expectedCount) {
            finished.complete(null);
        }
        return Exceptions.unwrap(t) instanceof IntentionalException;
    }).throwingOn(Exception.class);
    val error = new CompletableFuture<Throwable>();
    val p = new SequentialAsyncProcessor(() -> {
        count.incrementAndGet();
        throw new IntentionalException();
    }, retry, error::complete, executorService());
    // Invoke it once.
    p.runAsync();
    finished.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    val finalException = Exceptions.unwrap(error.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
    Assert.assertEquals("Unexpected number of final invocations.", expectedCount, count.get());
    Assert.assertTrue("Unexpected final error callback.", finalException instanceof RetriesExhaustedException && Exceptions.unwrap(finalException.getCause()) instanceof IntentionalException);
}
Also used : lombok.val(lombok.val) Retry(io.pravega.common.util.Retry) Semaphore(java.util.concurrent.Semaphore) Exceptions(io.pravega.common.Exceptions) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timeout(org.junit.rules.Timeout) Assert(org.junit.Assert) CompletableFuture(java.util.concurrent.CompletableFuture) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 15 with IntentionalException

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

the class AsyncReadResultProcessorTests method testReadFailures.

/**
 * Tests the AsyncReadResultProcessor when it encounters read failures.
 */
@Test
public void testReadFailures() throws Exception {
    // Pre-generate some entries.
    final int totalLength = 1000;
    final Semaphore barrier = new Semaphore(0);
    // Setup an entry provider supplier that returns Future Reads, which will eventually fail.
    StreamSegmentReadResult.NextEntrySupplier supplier = (offset, length) -> {
        Supplier<ReadResultEntryContents> entryContentsSupplier = () -> {
            barrier.acquireUninterruptibly();
            throw new IntentionalException("Intentional");
        };
        return new TestFutureReadResultEntry(offset, length, entryContentsSupplier, executorService());
    };
    // Start an AsyncReadResultProcessor.
    @Cleanup StreamSegmentReadResult rr = new StreamSegmentReadResult(0, totalLength, supplier, "");
    TestReadResultHandler testReadResultHandler = new TestReadResultHandler(new ArrayList<>());
    try (AsyncReadResultProcessor rp = AsyncReadResultProcessor.process(rr, testReadResultHandler, executorService())) {
        barrier.release();
        // Wait for it to complete, and then verify that no errors have been recorded via the callbacks.
        AssertExtensions.assertThrows("Processor did not complete with the expected failure.", () -> testReadResultHandler.completed.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS), ex -> Exceptions.unwrap(ex) instanceof IntentionalException);
        Assert.assertEquals("Unexpected number of reads processed.", 0, testReadResultHandler.readCount.get());
        Assert.assertNotNull("No read failure encountered.", testReadResultHandler.error.get());
        Assert.assertTrue("Unexpected type of exception was raised: " + testReadResultHandler.error.get(), testReadResultHandler.error.get() instanceof IntentionalException);
    }
    Assert.assertTrue("ReadResult was not closed when the AsyncReadResultProcessor was closed.", rr.isClosed());
}
Also used : AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) Duration(java.time.Duration) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) Timeout(org.junit.rules.Timeout) StreamHelpers(io.pravega.common.io.StreamHelpers) CancellationException(java.util.concurrent.CancellationException) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert(org.junit.Assert) Futures(io.pravega.common.concurrent.Futures) Supplier(java.util.function.Supplier) Semaphore(java.util.concurrent.Semaphore) Cleanup(lombok.Cleanup) 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