Search in sources :

Example 6 with ReadResultMock

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

the class AsyncTableEntryReaderTests method testReadKey.

// region Reading Keys
/**
 * Tests the ability to read a key.
 */
@Test
public void testReadKey() throws Exception {
    val testItems = generateTestItems();
    for (val e : testItems) {
        val keyReader = AsyncTableEntryReader.readKey(1L, SERIALIZER, new TimeoutTimer(TIMEOUT));
        Assert.assertEquals("Unexpected initial suggested read length.", AsyncTableEntryReader.INITIAL_READ_LENGTH, keyReader.getMaxReadAtOnce());
        @Cleanup val rr = new ReadResultMock(e.serialization, e.serialization.length, 1);
        AsyncReadResultProcessor.process(rr, keyReader, executorService());
        AssertExtensions.assertEventuallyEquals(true, () -> {
            int readerMaxReadAtOnce = keyReader.getMaxReadAtOnce();
            return Math.min(rr.getMaxResultLength(), readerMaxReadAtOnce != 0 ? readerMaxReadAtOnce : Integer.MAX_VALUE) == rr.getMaxReadAtOnce();
        }, 30 * 1000);
        // Get the result and compare it with the original key.
        val result = keyReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        val expectedVersion = e.isRemoval ? TableKey.NOT_EXISTS : (e.explicitVersion == TableKey.NO_VERSION) ? 1L : e.explicitVersion;
        Assert.assertEquals("Unexpected version.", expectedVersion, result.getVersion());
        Assert.assertEquals("Unexpected key read back.", new ByteArraySegment(e.key), result.getKey());
        Assert.assertEquals("Unexpected final suggested read length.", 0, keyReader.getMaxReadAtOnce());
    }
}
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 7 with ReadResultMock

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

the class AsyncTableEntryReaderTests method testReadKeyResultTooShort.

/**
 * Tests the ability to handle a case where the key could not be read before the read result was done.
 */
@Test
public void testReadKeyResultTooShort() {
    val testItems = generateTestItems();
    for (val e : testItems) {
        // Start a new reader & processor for this key-serialization pair.
        val keyReader = AsyncTableEntryReader.readKey(1L, SERIALIZER, new TimeoutTimer(TIMEOUT));
        @Cleanup val rr = new ReadResultMock(e.serialization, e.key.length - 1, 1);
        AsyncReadResultProcessor.process(rr, keyReader, executorService());
        AssertExtensions.assertThrows("Unexpected behavior for shorter read result.", () -> keyReader.getResult().get(BASE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), ex -> ex instanceof SerializationException);
    }
}
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)

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