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());
}
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());
}
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);
}
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);
}
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());
}
Aggregations