Search in sources :

Example 31 with IntentionalException

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

the class RollingStorageTests method testDeleteFailure.

/**
 * Tests the case when Delete worked partially (only some SegmentChunks were deleted, or all SegmentChunks were deleted
 * but the Header still exists).
 */
@Test
public void testDeleteFailure() throws Exception {
    final int failAtIndex = 1;
    @Cleanup val baseStorage = new TestStorage();
    @Cleanup val s = new RollingStorage(baseStorage, DEFAULT_ROLLING_POLICY);
    s.initialize(1);
    s.create(SEGMENT_NAME);
    val writeHandle = (RollingSegmentHandle) s.openWrite(SEGMENT_NAME);
    populate(s, writeHandle, null);
    // Simulate a deletion failure that is not a StreamSegmentNotExistsException.
    String failOnDelete = writeHandle.chunks().get(failAtIndex).getName();
    baseStorage.deleteFailure = sn -> sn.equals(failOnDelete) ? new IntentionalException() : null;
    AssertExtensions.assertThrows("delete() did not propagate proper exception on failure.", () -> s.delete(writeHandle), ex -> ex instanceof IntentionalException);
    Assert.assertTrue("Not expecting segment to be deleted yet.", s.exists(SEGMENT_NAME));
    Assert.assertFalse("Expected first SegmentChunk to be marked as deleted.", writeHandle.chunks().get(failAtIndex - 1).exists());
    Assert.assertTrue("Expected failed-to-delete SegmentChunk to not be marked as deleted.", writeHandle.chunks().get(failAtIndex).exists());
    Assert.assertTrue("Expected subsequent SegmentChunk to not be marked as deleted.", writeHandle.chunks().get(failAtIndex + 1).exists());
    // Clear the intentional failure, but do delete the SegmentChunk, to verify it properly handles missing SegmentChunks.
    baseStorage.deleteFailure = null;
    baseStorage.delete(baseStorage.openRead(failOnDelete));
    s.delete(writeHandle);
    Assert.assertFalse("Expecting the segment to be deleted.", s.exists(SEGMENT_NAME));
    Assert.assertTrue("Expected the handle to be marked as deleted.", writeHandle.isDeleted());
    Assert.assertFalse("Expected all SegmentChunks to be marked as deleted.", writeHandle.chunks().stream().anyMatch(SegmentChunk::exists));
}
Also used : lombok.val(lombok.val) Cleanup(lombok.Cleanup) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 32 with IntentionalException

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

the class OrderedItemProcessorTests method testFailures.

@Test
public void testFailures() {
    final int itemCount = 2 * CAPACITY;
    val processedItems = Collections.synchronizedCollection(new HashSet<Integer>());
    val processFutures = Collections.synchronizedList(new ArrayList<CompletableFuture<Integer>>());
    Function<Integer, CompletableFuture<Integer>> itemProcessor = i -> {
        if (!processedItems.add(i)) {
            Assert.fail("Duplicate item detected: " + i);
        }
        CompletableFuture<Integer> result = new CompletableFuture<>();
        processFutures.add(result);
        return result;
    };
    val resultFutures = new ArrayList<CompletableFuture<Integer>>();
    @Cleanup val p = new TestProcessor(CAPACITY, itemProcessor, executorService());
    // Fill up to capacity, and beyond.
    for (int i = 0; i < itemCount; i++) {
        resultFutures.add(p.process(i));
    }
    // Fail an item.
    val failedIndex = CAPACITY / 2;
    processFutures.get(failedIndex).completeExceptionally(new IntentionalException());
    AssertExtensions.assertThrows("Failed item did not have its result failed as well.", resultFutures.get(failedIndex)::join, ex -> ex instanceof IntentionalException);
    // Verify all queued-up items have been failed, but none of the initial ones (that have already begun processing)
    for (int i = CAPACITY; i < itemCount; i++) {
        AssertExtensions.assertThrows("Queued-up item did not fail when a previous item failed.", resultFutures.get(i)::join, ex -> ex instanceof OrderedItemProcessor.ProcessingException && ex.getCause() instanceof IntentionalException);
    }
    for (int i = 0; i < CAPACITY; i++) {
        if (i != failedIndex) {
            Assert.assertFalse("Already-processing future was completed as well.", resultFutures.get(i).isDone());
        }
    }
    // Verify we can't add anything else ...
    AssertExtensions.assertThrows("failure did not cause OrderedItemProcessor to close.", () -> p.process(Integer.MAX_VALUE), ex -> ex instanceof ObjectClosedException);
}
Also used : lombok.val(lombok.val) ObjectClosedException(io.pravega.common.ObjectClosedException) Setter(lombok.Setter) Getter(lombok.Getter) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert(org.junit.Assert) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) IntentionalException(io.pravega.test.common.IntentionalException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) ObjectClosedException(io.pravega.common.ObjectClosedException) Test(org.junit.Test)

Example 33 with IntentionalException

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

the class AbstractThreadPoolServiceTests method testCancellationException.

/**
 * Tests the case when the Service stops due to a CancellationException.
 */
@Test
public void testCancellationException() {
    val s = newService();
    // Stop it and verify it hasn't shut down - it should still be waiting on the runFuture.
    val stopException = new IntentionalException("stop");
    s.errorHandler(stopException);
    s.runFuture.completeExceptionally(new CancellationException());
    // Complete the future and await normal termination.
    s.awaitTerminated();
    Assert.assertEquals("Unexpected state.", Service.State.TERMINATED, s.state());
}
Also used : lombok.val(lombok.val) CancellationException(java.util.concurrent.CancellationException) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 34 with IntentionalException

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

the class AbstractThreadPoolServiceTests method testAutoShutdown.

/**
 * Tests the behavior of AbstractThreadPoolService when the runFuture completes (normally or not).
 */
@Test
public void testAutoShutdown() {
    // When completed normally.
    @Cleanup val s1 = newService();
    s1.runFuture.complete(null);
    s1.awaitTerminated();
    Assert.assertEquals("Unexpected state upon auto-shutdown (normal completion).", Service.State.TERMINATED, s1.state());
    // When completed with failure.
    @Cleanup val s2 = newService();
    s2.runFuture.completeExceptionally(new IntentionalException());
    AssertExtensions.assertThrows("Service did not fail when runFuture failed.", () -> s2.awaitTerminated(), ex -> ex instanceof IllegalStateException);
    Assert.assertEquals("Unexpected state upon auto-shutdown (failure).", Service.State.FAILED, s2.state());
    Assert.assertTrue("Unexpected failure cause.", s2.failureCause() instanceof IntentionalException);
}
Also used : lombok.val(lombok.val) Cleanup(lombok.Cleanup) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test)

Example 35 with IntentionalException

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

the class AbstractThreadPoolServiceTests method testShutdownStopException.

/**
 * Tests the behavior of AbstractThreadPoolService during shutdown when only a StopException is present.
 */
@Test
public void testShutdownStopException() {
    @Cleanup val s = newService();
    // Stop it and verify it hasn't shut down - it should still be waiting on the runFuture.
    val correctEx = new IntentionalException();
    val wrongEx = new IntentionalException();
    s.errorHandler(correctEx);
    s.errorHandler(wrongEx);
    s.stopAsync();
    s.runFuture.complete(null);
    AssertExtensions.assertThrows("Service did not fail when a StopException has been recorded.", () -> s.awaitTerminated(), ex -> ex instanceof IllegalStateException);
    Assert.assertEquals("Unexpected state upon failed shutdown.", Service.State.FAILED, s.state());
    Assert.assertEquals("Unexpected failure cause.", correctEx, s.failureCause());
}
Also used : lombok.val(lombok.val) 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