Search in sources :

Example 1 with ReadResultMock

use of io.pravega.segmentstore.server.ReadResultMock in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEmptyKey.

/**
 * Tests the ability to read an empty key (this should result in an exception).
 */
@Test
public void testReadEmptyKey() {
    val testItem = generateTestItem(new byte[0], new byte[0], false, false);
    // Start a new reader & processor for this key-serialization pair.
    val keyReader = AsyncTableEntryReader.readKey(1L, SERIALIZER, new TimeoutTimer(TIMEOUT));
    @Cleanup val rr = new ReadResultMock(testItem.serialization, testItem.serialization.length, 1);
    AsyncReadResultProcessor.process(rr, keyReader, executorService());
    AssertExtensions.assertThrows("Unexpected behavior for empty key.", () -> keyReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    // When the result is done, whether with error or not, this should be set to 0.
    Assert.assertEquals("Unexpected final suggested read length.", 0, keyReader.getMaxReadAtOnce());
}
Also used : lombok.val(lombok.val) SerializationException(io.pravega.common.io.SerializationException) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 2 with ReadResultMock

use of io.pravega.segmentstore.server.ReadResultMock in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEntry.

// endregion
// region Reading Entries
/**
 * Tests the ability to read a Table Entry for a matching key.
 */
@Test
public void testReadEntry() throws Exception {
    long keyVersion = 1L;
    val testItems = generateTestItems();
    for (val item : testItems) {
        // Start a new reader & processor for this key-serialization pair.
        val entryReader = AsyncTableEntryReader.readEntry(new ByteArraySegment(item.key), keyVersion, SERIALIZER, new TimeoutTimer(TIMEOUT));
        Assert.assertEquals("Unexpected initial suggested read length.", AsyncTableEntryReader.INITIAL_READ_LENGTH, entryReader.getMaxReadAtOnce());
        @Cleanup val rr = new ReadResultMock(item.serialization, item.serialization.length, 1);
        AsyncReadResultProcessor.process(rr, entryReader, executorService());
        Assert.assertEquals(Math.min(rr.getMaxResultLength(), entryReader.getMaxReadAtOnce()), rr.getMaxReadAtOnce());
        val result = entryReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        Assert.assertNotNull("Expecting a result.", result);
        Assert.assertEquals("Unexpected suggested read length after reading whole entry.", 0, entryReader.getMaxReadAtOnce());
        // Check key.
        val resultKey = result.getKey().getKey();
        Assert.assertEquals("Unexpected result key length.", item.key.length, resultKey.getLength());
        Assert.assertEquals("Unexpected result key.", new ByteArraySegment(item.key), resultKey);
        if (item.isRemoval) {
            // Verify there is no value and that the key has been properly set.
            Assert.assertEquals("Unexpected key version for non existing key.", TableKey.NOT_EXISTS, result.getKey().getVersion());
            Assert.assertNull("Not expecting a value for a removal.", result.getValue());
        } else {
            // Verify we have a value and that it matches.
            if (item.explicitVersion == TableKey.NO_VERSION) {
                Assert.assertEquals("Unexpected key version for existing key.", keyVersion, result.getKey().getVersion());
            } else {
                Assert.assertEquals("Unexpected (explicit) key version for existing key.", item.explicitVersion, result.getKey().getVersion());
            }
            Assert.assertNotNull("Expecting a value for non removal.", result.getValue());
            val resultValue = result.getValue();
            Assert.assertEquals("Unexpected value length.", item.value.length, resultValue.getLength());
            Assert.assertEquals("Unexpected result value", new ByteArraySegment(item.value), resultValue);
        }
        keyVersion++;
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 3 with ReadResultMock

use of io.pravega.segmentstore.server.ReadResultMock in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEntryResultTooShort.

/**
 * Tests the ability to handle a case where the key could not be read before the read result was done.
 */
@Test
public void testReadEntryResultTooShort() {
    val testItems = generateTestItems();
    for (val e : testItems) {
        // Start a new reader & processor for this key-serialization pair.
        val entryReader = AsyncTableEntryReader.readEntry(new ByteArraySegment(e.key), 0L, SERIALIZER, new TimeoutTimer(TIMEOUT));
        @Cleanup val rr = new ReadResultMock(e.serialization, e.serialization.length - 1, 1);
        AsyncReadResultProcessor.process(rr, entryReader, executorService());
        AssertExtensions.assertThrows("Unexpected behavior for shorter read result..", () -> entryReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) SerializationException(io.pravega.common.io.SerializationException) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 4 with ReadResultMock

use of io.pravega.segmentstore.server.ReadResultMock in project pravega by pravega.

the class AsyncTableEntryReaderTests method testReadEntryNoKeyMatch.

/**
 * Tests the ability to not read a Table Entry if the sought key does not match.
 */
@Test
public void testReadEntryNoKeyMatch() throws Exception {
    val testItems = generateTestItems();
    for (int i = 0; i < testItems.size(); i++) {
        for (int j = 0; j < testItems.size(); j++) {
            if (i == j) {
                // This case is tested in testReadEntry().
                continue;
            }
            val searchKey = testItems.get(i).key;
            val searchData = testItems.get(j).serialization;
            val entryReader = AsyncTableEntryReader.readEntry(new ByteArraySegment(searchKey), 0L, SERIALIZER, new TimeoutTimer(TIMEOUT));
            @Cleanup val rr = new ReadResultMock(searchData, searchData.length, 1);
            AsyncReadResultProcessor.process(rr, entryReader, executorService());
            val result = entryReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
            Assert.assertNull("Not expecting a result.", result);
        }
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer) Test(org.junit.Test)

Example 5 with ReadResultMock

use of io.pravega.segmentstore.server.ReadResultMock in project pravega by pravega.

the class AsyncTableEntryReaderTests method testBufferCompaction.

private <T> void testBufferCompaction(GetEntryReader<T> createReader, Function<T, TableKey> getKey, Function<T, BufferView> getValue) throws Exception {
    // Must be less than AsyncTableEntryReader.INITIAL_READ_LENGTH / 2 (to ease testing).
    val keyLength = 3987;
    // Must be less than AsyncTableEntryReader.INITIAL_READ_LENGTH / 2 (to ease testing)..
    val valueLength = 3123;
    val serializer = new EntrySerializer();
    // Generate a number of entries. We only care about the first one, but we want to ensure that we have enough other
    // data to force the ReadResult to try to read more.
    val testItems = generateTestItems(() -> keyLength, () -> valueLength);
    val entries = testItems.stream().filter(i -> !i.isRemoval).map(i -> TableEntry.unversioned(new ByteArraySegment(i.key), new ByteArraySegment(i.value))).collect(Collectors.toList());
    // Search for the first Key/Entry. This makes it easier as we don't have to guess the versions, offsets, etc.
    val soughtEntry = entries.get(0);
    val segmentData = serializer.serializeUpdate(entries).getCopy();
    @Cleanup val readResultNoCompact = new ReadResultMock(segmentData, keyLength + valueLength + 20, keyLength + 200);
    val readerNoCompact = createReader.apply(soughtEntry.getKey().getKey(), 0L, serializer, new TimeoutTimer(TIMEOUT));
    testBufferCompaction(readerNoCompact, readResultNoCompact, getKey, getValue, false);
    @Cleanup val readResultWithCompact = new ReadResultMock(segmentData, segmentData.length, segmentData.length);
    val readerWithCompact = createReader.apply(soughtEntry.getKey().getKey(), 0L, serializer, new TimeoutTimer(TIMEOUT));
    testBufferCompaction(readerWithCompact, readResultWithCompact, getKey, getValue, true);
}
Also used : lombok.val(lombok.val) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Cleanup(lombok.Cleanup) Random(java.util.Random) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) SerializationException(io.pravega.common.io.SerializationException) AsyncReadResultProcessor(io.pravega.segmentstore.server.reading.AsyncReadResultProcessor) Nullable(javax.annotation.Nullable) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) TimeoutTimer(io.pravega.common.TimeoutTimer) lombok.val(lombok.val) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ReadResultMock(io.pravega.segmentstore.server.ReadResultMock) Cleanup(lombok.Cleanup) TimeoutTimer(io.pravega.common.TimeoutTimer)

Aggregations

TimeoutTimer (io.pravega.common.TimeoutTimer)7 ReadResultMock (io.pravega.segmentstore.server.ReadResultMock)7 Cleanup (lombok.Cleanup)7 lombok.val (lombok.val)7 Test (org.junit.Test)7 ByteArraySegment (io.pravega.common.util.ByteArraySegment)5 SerializationException (io.pravega.common.io.SerializationException)4 BufferView (io.pravega.common.util.BufferView)1 TableEntry (io.pravega.segmentstore.contracts.tables.TableEntry)1 TableKey (io.pravega.segmentstore.contracts.tables.TableKey)1 AsyncReadResultProcessor (io.pravega.segmentstore.server.reading.AsyncReadResultProcessor)1 AssertExtensions (io.pravega.test.common.AssertExtensions)1 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Random (java.util.Random)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1