use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.
the class PulsarDeserializationSchemaTest method createFromFlinkTypeInformation.
@Test
void createFromFlinkTypeInformation() throws Exception {
PulsarDeserializationSchema<String> schema = flinkTypeInfo(Types.STRING, null);
schema.open(new TestingDeserializationContext(), mock(SourceConfiguration.class));
assertDoesNotThrow(() -> InstantiationUtil.clone(schema));
Message<byte[]> message = getMessage("test-content", s -> {
DataOutputSerializer serializer = new DataOutputSerializer(10);
StringValue.writeString(s, serializer);
return serializer.getSharedBuffer();
});
SingleMessageCollector<String> collector = new SingleMessageCollector<>();
schema.deserialize(message, collector);
assertNotNull(collector.result);
assertEquals(collector.result, "test-content");
}
use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.
the class LinkedOptionalMapSerializer method writeFramed.
private static <T> void writeFramed(DataOutputView out, BiConsumerWithException<DataOutputView, T, IOException> writer, T item) throws IOException {
DataOutputSerializer frame = new DataOutputSerializer(64);
writer.accept(frame, item);
final byte[] buffer = frame.getSharedBuffer();
final int bufferSize = frame.length();
out.writeInt(bufferSize);
out.write(buffer, 0, bufferSize);
}
use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.
the class KryoSerializerConcurrencyTest method testConcurrentUseOfSerializer.
@Test
public void testConcurrentUseOfSerializer() throws Exception {
final KryoSerializer<String> serializer = new KryoSerializer<>(String.class, new ExecutionConfig());
final BlockerSync sync = new BlockerSync();
final DataOutputView regularOut = new DataOutputSerializer(32);
final DataOutputView lockingOut = new LockingView(sync);
// this thread serializes and gets stuck there
final CheckedThread thread = new CheckedThread("serializer") {
@Override
public void go() throws Exception {
serializer.serialize("a value", lockingOut);
}
};
thread.start();
sync.awaitBlocker();
// this should fail with an exception
try {
serializer.serialize("value", regularOut);
fail("should have failed with an exception");
} catch (IllegalStateException e) {
// expected
} finally {
// release the thread that serializes
sync.releaseBlocker();
}
// this propagates exceptions from the spawned thread
thread.sync();
}
use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.
the class KryoSerializerSnapshotTest method unLoadableSnapshotBytes.
/**
* This method returns the bytes of a serialized {@link KryoSerializerSnapshot}, that contains a
* Kryo registration of a class that does not exists in the current classpath.
*/
private static byte[] unLoadableSnapshotBytes() throws IOException {
final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
final ClassLoaderUtils.ObjectAndClassLoader<Serializable> outsideClassLoading = ClassLoaderUtils.createSerializableObjectFromNewClassLoader();
try {
Thread.currentThread().setContextClassLoader(outsideClassLoading.getClassLoader());
ExecutionConfig conf = new ExecutionConfig();
conf.registerKryoType(outsideClassLoading.getObject().getClass());
KryoSerializer<Animal> previousSerializer = new KryoSerializer<>(Animal.class, conf);
TypeSerializerSnapshot<Animal> previousSnapshot = previousSerializer.snapshotConfiguration();
DataOutputSerializer out = new DataOutputSerializer(4096);
TypeSerializerSnapshot.writeVersionedSnapshot(out, previousSnapshot);
return out.getCopyOfBuffer();
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.
the class SimpleVersionedSerializationTest method testSerializeEmpty.
@Test
public void testSerializeEmpty() throws IOException {
final String testString = "beeeep!";
SimpleVersionedSerializer<String> emptySerializer = new SimpleVersionedSerializer<String>() {
@Override
public int getVersion() {
return 42;
}
@Override
public byte[] serialize(String obj) throws IOException {
return new byte[0];
}
@Override
public String deserialize(int version, byte[] serialized) throws IOException {
assertEquals(42, version);
assertEquals(0, serialized.length);
return testString;
}
};
final DataOutputSerializer out = new DataOutputSerializer(32);
SimpleVersionedSerialization.writeVersionAndSerialize(emptySerializer, "abc", out);
final byte[] outBytes = out.getCopyOfBuffer();
final byte[] bytes = SimpleVersionedSerialization.writeVersionAndSerialize(emptySerializer, "abc");
assertArrayEquals(bytes, outBytes);
final DataInputDeserializer in = new DataInputDeserializer(bytes);
final String deserialized = SimpleVersionedSerialization.readVersionAndDeSerialize(emptySerializer, in);
final String deserializedFromBytes = SimpleVersionedSerialization.readVersionAndDeSerialize(emptySerializer, outBytes);
assertEquals(testString, deserialized);
assertEquals(testString, deserializedFromBytes);
}
Aggregations