use of org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader in project flink by apache.
the class TypeSerializerSerializationUtilTest method testSerializerSerializationWithClassNotFound.
/**
* Verifies deserialization failure cases when reading a serializer from bytes, in the case of a
* {@link ClassNotFoundException}.
*/
@Test
public void testSerializerSerializationWithClassNotFound() throws Exception {
TypeSerializer<?> serializer = IntSerializer.INSTANCE;
byte[] serialized;
try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
serialized = out.toByteArray();
}
TypeSerializer<?> deserializedSerializer;
try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader(Thread.currentThread().getContextClassLoader(), Collections.singleton(IntSerializer.class.getName())), true);
}
Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);
Assert.assertArrayEquals(InstantiationUtil.serializeObject(serializer), ((UnloadableDummyTypeSerializer<?>) deserializedSerializer).getActualBytes());
}
use of org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader in project flink by apache.
the class TypeSerializerSerializationUtilTest method testSerializerSerializationWithInvalidClass.
/**
* Verifies deserialization failure cases when reading a serializer from bytes, in the case of a
* {@link InvalidClassException}.
*/
@Test
public void testSerializerSerializationWithInvalidClass() throws Exception {
TypeSerializer<?> serializer = IntSerializer.INSTANCE;
byte[] serialized;
try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
serialized = out.toByteArray();
}
TypeSerializer<?> deserializedSerializer;
try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader(Thread.currentThread().getContextClassLoader(), Collections.singleton(IntSerializer.class.getName())), true);
}
Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);
}
use of org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader in project flink by apache.
the class TypeSerializerSerializationUtilTest method testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures.
/**
* Verifies resilience to serializer deserialization failures when writing and reading
* serializer and config snapshot pairs.
*/
@Test
public void testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures() throws Exception {
TestIntSerializer serializer = new TestIntSerializer();
List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = Arrays.asList(new Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>(serializer, serializer.snapshotConfiguration()));
byte[] serializedSerializersAndConfigs;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience(new DataOutputViewStreamWrapper(out), serializersAndConfigs);
serializedSerializersAndConfigs = out.toByteArray();
}
Set<String> cnfThrowingClassnames = new HashSet<>();
cnfThrowingClassnames.add(TestIntSerializer.class.getName());
List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> restored;
try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSerializersAndConfigs)) {
restored = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader(Thread.currentThread().getContextClassLoader(), cnfThrowingClassnames));
}
Assert.assertEquals(1, restored.size());
Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer);
Assert.assertThat(restored.get(0).f1, Matchers.instanceOf(SimpleTypeSerializerSnapshot.class));
}
Aggregations