use of io.pravega.common.io.SerializationException in project pravega by pravega.
the class RevisionDataOutputStreamTests method testNonSeekableOutputLongerLength.
/**
* Tests the NonSeekableRevisionDataOutput class when we provide a shorter length than expected.
*/
@Test
public void testNonSeekableOutputLongerLength() throws Exception {
byte b = 1;
short sn = 2;
int n = 3;
@Cleanup val s = new ByteArrayOutputStream();
// Wrap the stream, but do not auto-close it since we expect close() to fail, which is verified below.
val impl = RevisionDataOutputStream.wrap(s);
int correctLength = Byte.BYTES + Short.BYTES + Integer.BYTES;
// Shorter length.
impl.length(correctLength + 1);
impl.writeByte(b);
impl.writeShort(sn);
impl.writeInt(n);
// Verify close() fails.
AssertExtensions.assertThrows("RevisionDataOutputStream.close() did not throw for byte mismatch.", impl::close, ex -> ex instanceof SerializationException);
// Verify the written data can be read back.
val inputStream = RevisionDataInputStream.wrap(new ByteArrayInputStream(s.toByteArray()));
Assert.assertEquals("Unexpected byte read back.", b, inputStream.read());
Assert.assertEquals("Unexpected short read back.", sn, inputStream.readShort());
Assert.assertEquals("Unexpected int read back.", n, inputStream.readInt());
// And verify we can't read anything else and we'll get an exception upon close indicating we read fewer bytes than expected.
AssertExtensions.assertThrows("Expecting EOF.", () -> inputStream.readFully(new byte[1]), ex -> ex instanceof EOFException);
AssertExtensions.assertThrows("Expecting an exception when reading fewer bytes than declared.", inputStream::close, ex -> ex instanceof SerializationException);
}
use of io.pravega.common.io.SerializationException in project pravega by pravega.
the class DataFrameReaderTests method testReadsWithDeserializationFailure.
/**
* Tests the case when the DataFrameReader reads from a log and it encounters LogItem SerializationExceptions.
*/
@Test
public void testReadsWithDeserializationFailure() throws Exception {
// Fail deserialization every X records (write-wise).
int failDeserializationEvery = 11;
ArrayList<TestLogItem> records = DataFrameTestHelpers.generateLogItems(100, SMALL_RECORD_MIN_SIZE, SMALL_RECORD_MAX_SIZE, 0);
records.addAll(DataFrameTestHelpers.generateLogItems(100, LARGE_RECORD_MIN_SIZE, LARGE_RECORD_MAX_SIZE, records.size()));
try (TestDurableDataLog dataLog = TestDurableDataLog.create(CONTAINER_ID, FRAME_SIZE, executorService())) {
dataLog.initialize(TIMEOUT);
BiConsumer<Throwable, DataFrameBuilder.CommitArgs> errorCallback = (ex, a) -> Assert.fail(String.format("Unexpected error occurred upon commit. %s", ex));
val args = new DataFrameBuilder.Args(Callbacks::doNothing, Callbacks::doNothing, errorCallback, executorService());
try (DataFrameBuilder<TestLogItem> b = new DataFrameBuilder<>(dataLog, SERIALIZER, args)) {
for (TestLogItem r : records) {
b.append(r);
}
}
ErrorInjector<SerializationException> errorInjector = new ErrorInjector<>(count -> count % failDeserializationEvery == 0, () -> new SerializationException("TestLogItem.deserialize intentional"));
TestSerializer logItemFactory = new TestSerializer();
logItemFactory.setDeserializationErrorInjector(errorInjector);
testReadWithException(dataLog, logItemFactory, ex -> ex instanceof DataCorruptionException);
}
}
use of io.pravega.common.io.SerializationException in project pravega by pravega.
the class StreamSegmentAppendOperationTests method testSerializationV0V1.
/**
* Tests the following scenarios:
* - Serialization and deserialization using V1.
* - Serialization using V1 and deserialization using V0.
* - Serialization using V0 and deserialization using V1.
* <p>
* NOTE: {@link #testSerialization()} validates that {@link StreamSegmentAppendOperation.Serializer} works well
* with default settings (for normal scenarios). This method validates backwards compatibility.
*/
@Test
public void testSerializationV0V1() throws IOException {
// Writes V0, reads both V0, V1.
val defaultSerializer = new StreamSegmentAppendOperation.Serializer();
// Writes and reads V0 only.
val serializerV0 = new StreamSegmentAppendOperation.SerializerV0();
// Writes V1, reads both V0, V1.
val serializerV1 = new SerializerV1();
val random = RandomFactory.create();
val operations = Arrays.asList(createOperation(random), new StreamSegmentAppendOperation(1, new ByteArraySegment(new byte[1]), null));
for (val baseOp : operations) {
val baseOpUUIDOnly = new StreamSegmentAppendOperation(baseOp.getStreamSegmentId(), baseOp.getData(), baseOp.getAttributeUpdates() == null ? null : AttributeUpdateCollection.from(baseOp.getAttributeUpdates().getUUIDAttributeUpdates()));
baseOp.setSequenceNumber(MathHelpers.abs(random.nextLong()));
baseOpUUIDOnly.setSequenceNumber(baseOp.getSequenceNumber());
configurePreSerialization(baseOp, random);
baseOpUUIDOnly.setStreamSegmentOffset(baseOp.getStreamSegmentOffset());
// 1. Deserialize the V0 serialization using ALL serializers. This simulates an upgrade scenario from old code.
// This can only do UUIDs.
val v0Serialized = serializerV0.serialize(baseOpUUIDOnly);
val v0v0Deserialized = serializerV0.deserialize(v0Serialized);
OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0v0Deserialized);
val v0v1Deserialized = serializerV1.deserialize(v0Serialized);
OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0v1Deserialized);
val v0vdDeserialized = defaultSerializer.deserialize(v0Serialized);
OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, v0vdDeserialized);
// 2. Deserialize the V1 serializer using V1 and Default serializer. This simulates normal operations.
val v1Serialized = serializerV1.serialize(baseOp);
val v1v1Deserialized = serializerV1.deserialize(v1Serialized);
OperationComparer.DEFAULT.assertEquals(baseOp, v1v1Deserialized);
val v1vdDeserialized = defaultSerializer.deserialize(v1Serialized);
OperationComparer.DEFAULT.assertEquals(baseOp, v1vdDeserialized);
AssertExtensions.assertThrows("V0 should not be able to deserialize V1", () -> serializerV0.deserialize(v1Serialized), ex -> ex instanceof SerializationException);
// 3. Deserialize the Default serializer using ALL serializers. This simulates normal operations and "rollback".
val dSerialized = defaultSerializer.serialize(baseOp);
val vdv0Deserialized = serializerV0.deserialize(dSerialized);
// V0 doesn't know about Variable IDs.
OperationComparer.DEFAULT.assertEquals(baseOpUUIDOnly, vdv0Deserialized);
val vdv1Deserialized = serializerV1.deserialize(dSerialized);
OperationComparer.DEFAULT.assertEquals(baseOp, vdv1Deserialized);
val vdvdDeserialized = defaultSerializer.deserialize(dSerialized);
OperationComparer.DEFAULT.assertEquals(baseOp, vdvdDeserialized);
}
}
use of io.pravega.common.io.SerializationException 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.common.io.SerializationException 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());
}
Aggregations