Search in sources :

Example 1 with ArtificialCNFExceptionThrowingClassLoader

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());
}
Also used : ArtificialCNFExceptionThrowingClassLoader(org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 2 with ArtificialCNFExceptionThrowingClassLoader

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);
}
Also used : ArtificialCNFExceptionThrowingClassLoader(org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 3 with ArtificialCNFExceptionThrowingClassLoader

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));
}
Also used : ArtificialCNFExceptionThrowingClassLoader(org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) Tuple2(org.apache.flink.api.java.tuple.Tuple2) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)3 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)3 ArtificialCNFExceptionThrowingClassLoader (org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader)3 Test (org.junit.Test)3 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)2 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashSet (java.util.HashSet)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1