Search in sources :

Example 1 with InMemoryTableStore

use of io.pravega.segmentstore.storage.mocks.InMemoryTableStore in project pravega by pravega.

the class TableBasedMetadataStoreMockTests method testExceptionDuringRemoveWithSpy.

@Test
public void testExceptionDuringRemoveWithSpy() throws Exception {
    TableStore mockTableStore = spy(new InMemoryTableStore(executorService()));
    @Cleanup TableBasedMetadataStore tableBasedMetadataStore = new TableBasedMetadataStore("test", mockTableStore, ChunkedSegmentStorageConfig.DEFAULT_CONFIG, executorService());
    // Step 1 - set up keys
    try (val txn = tableBasedMetadataStore.beginTransaction(false, "TEST")) {
        txn.create(new MockStorageMetadata("key1", "A"));
        txn.create(new MockStorageMetadata("key2", "B"));
        txn.create(new MockStorageMetadata("key3", "C"));
        txn.commit().join();
    }
    // Step 2 - Throw random exception
    Exception e = new ArithmeticException();
    val td = BaseMetadataStore.TransactionData.builder().key("foo").version(1L).dbObject(2L).build();
    CompletableFuture<Void> f = new CompletableFuture<>();
    f.completeExceptionally(e);
    when(mockTableStore.remove(anyString(), any(), any())).thenReturn(f);
    // Step 3 - modify some keys and delete a key. This uses mock method that throws exception
    try (val txn = tableBasedMetadataStore.beginTransaction(false, "TEST")) {
        Assert.assertEquals("A", ((MockStorageMetadata) txn.get("key1").get()).getValue());
        Assert.assertEquals("B", ((MockStorageMetadata) txn.get("key2").get()).getValue());
        Assert.assertEquals("C", ((MockStorageMetadata) txn.get("key3").get()).getValue());
        txn.update(new MockStorageMetadata("key1", "a"));
        txn.update(new MockStorageMetadata("key2", "b"));
        txn.delete("key3");
        txn.commit().join();
    }
    // Step 4 - Validate results and then retry delete with no mocking.
    try (val txn = tableBasedMetadataStore.beginTransaction(false, "TEST")) {
        Assert.assertEquals("a", ((MockStorageMetadata) txn.get("key1").get()).getValue());
        Assert.assertEquals("b", ((MockStorageMetadata) txn.get("key2").get()).getValue());
        Assert.assertEquals(null, txn.get("key3").get());
        val direct = tableBasedMetadataStore.read("key3").get();
        Assert.assertNotNull(direct);
        Assert.assertNotEquals(-1L, direct.getDbObject());
        txn.delete("key3");
        // stop mocking
        when(mockTableStore.remove(anyString(), any(), any())).thenCallRealMethod();
        txn.commit().join();
    }
    // Step 5 - Validate results.
    try (val txn = tableBasedMetadataStore.beginTransaction(true, "TEST")) {
        Assert.assertEquals("a", ((MockStorageMetadata) txn.get("key1").get()).getValue());
        Assert.assertEquals("b", ((MockStorageMetadata) txn.get("key2").get()).getValue());
        Assert.assertEquals(null, txn.get("key3").get());
        val direct = tableBasedMetadataStore.read("key3").get();
        Assert.assertNotNull(direct);
        Assert.assertEquals(-1L, direct.getDbObject());
    }
}
Also used : lombok.val(lombok.val) CompletableFuture(java.util.concurrent.CompletableFuture) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) Cleanup(lombok.Cleanup) InMemoryTableStore(io.pravega.segmentstore.storage.mocks.InMemoryTableStore) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) DataLogWriterNotPrimaryException(io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) InMemoryTableStore(io.pravega.segmentstore.storage.mocks.InMemoryTableStore) Test(org.junit.Test)

Example 2 with InMemoryTableStore

use of io.pravega.segmentstore.storage.mocks.InMemoryTableStore in project pravega by pravega.

the class TableBasedMetadataStoreTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    val tableStore = new InMemoryTableStore(executorService());
    metadataStore = new TableBasedMetadataStore("TEST", tableStore, ChunkedSegmentStorageConfig.DEFAULT_CONFIG, executorService());
}
Also used : lombok.val(lombok.val) InMemoryTableStore(io.pravega.segmentstore.storage.mocks.InMemoryTableStore) Before(org.junit.Before)

Aggregations

InMemoryTableStore (io.pravega.segmentstore.storage.mocks.InMemoryTableStore)2 lombok.val (lombok.val)2 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)1 BadKeyVersionException (io.pravega.segmentstore.contracts.tables.BadKeyVersionException)1 TableStore (io.pravega.segmentstore.contracts.tables.TableStore)1 DataLogWriterNotPrimaryException (io.pravega.segmentstore.storage.DataLogWriterNotPrimaryException)1 MockStorageMetadata (io.pravega.segmentstore.storage.mocks.MockStorageMetadata)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 Cleanup (lombok.Cleanup)1 Before (org.junit.Before)1 Test (org.junit.Test)1