Package org.apache.flink.runtime.io.network.serialization.types

Examples of org.apache.flink.runtime.io.network.serialization.types.SerializationTestType


public class DataInputOutputSerializerTest {

  @Test
  public void testWrapAsByteBuffer() {
    SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);

    DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
    MemorySegment segment = new MemorySegment(new byte[randomInt.length()]);

    try {
      // empty buffer, read buffer should be empty
      ByteBuffer wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // write to data output, read buffer should still be empty
      randomInt.write(serializer);

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // get updated read buffer, read buffer should contain written data
      wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());

      // clear data output, read buffer should still contain written data
      serializer.clear();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());

      // get updated read buffer, should be empty
      wrapper = serializer.wrapAsByteBuffer();

      Assert.assertEquals(0, wrapper.position());
      Assert.assertEquals(0, wrapper.limit());

      // write to data output and read back to memory
      randomInt.write(serializer);
      wrapper = serializer.wrapAsByteBuffer();

      segment.put(0, wrapper, randomInt.length());

      Assert.assertEquals(randomInt.length(), wrapper.position());
      Assert.assertEquals(randomInt.length(), wrapper.limit());
    } catch (IOException e) {
      e.printStackTrace();
      Assert.fail("Test encountered an unexpected exception.");
    }
  }
View Full Code Here


    DataInputDeserializer deserializer = new DataInputDeserializer(serializer.wrapAsByteBuffer());

    for (SerializationTestType expected : reference) {
      try {
        SerializationTestType actual = expected.getClass().newInstance();
        actual.read(deserializer);

        Assert.assertEquals(expected, actual);
      } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Test encountered an unexpected exception.");
View Full Code Here

    // check the records
    TestInputView inView = new TestInputView(outView.segments);

    for (SerializationTestType reference : elements) {
      SerializationTestType result = reference.getClass().newInstance();
      result.read(inView);
      assertEquals(reference, result);
    }
  }
View Full Code Here

  public void testHasData() {
    final int SEGMENT_SIZE = 16;

    final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
    final Buffer buffer = new Buffer(new MemorySegment(new byte[SEGMENT_SIZE]), SEGMENT_SIZE, null);
    final SerializationTestType randomIntRecord = Util.randomRecord(SerializationTestTypeFactory.INT);

    Assert.assertFalse(serializer.hasData());

    try {
      serializer.addRecord(randomIntRecord);
View Full Code Here

    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      SerializationTestType emptyRecord = new SerializationTestType() {
        @Override
        public SerializationTestType getRandom(Random rnd) {
          throw new UnsupportedOperationException();
        }
View Full Code Here

      if (serializer.addRecord(record).isFullBuffer()) {
        // buffer is full => start deserializing
        deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);

        while (!serializedRecords.isEmpty()) {
          SerializationTestType expected = serializedRecords.poll();
          SerializationTestType actual = expected.getClass().newInstance();

          if (deserializer.getNextRecord(actual).isFullRecord()) {
            Assert.assertEquals(expected, actual);
            numRecords--;
          } else {
            serializedRecords.addFirst(expected);
            break;
          }
        }

        while (serializer.setNextBuffer(buffer).isFullBuffer()) {
          deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);
        }



      }
    }

    // deserialize left over records
    deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), (numBytes % segmentSize));

    serializer.clear();

    while (!serializedRecords.isEmpty()) {
      SerializationTestType expected = serializedRecords.poll();

      SerializationTestType actual = expected.getClass().newInstance();
      DeserializationResult result = deserializer.getNextRecord(actual);

      Assert.assertTrue(result.isFullRecord());
      Assert.assertEquals(expected, actual);
      numRecords--;
View Full Code Here

TOP

Related Classes of org.apache.flink.runtime.io.network.serialization.types.SerializationTestType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.