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