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