Java Code Examples for org.apache.flink.testutils.serialization.types.SerializationTestType#length()

The following examples show how to use org.apache.flink.testutils.serialization.types.SerializationTestType#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpanningRecordSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Iterates over the provided records and tests whether the {@link SpanningRecordSerializer} returns the expected
 * {@link RecordSerializer.SerializationResult} values.
 *
 * <p>Only a single {@link MemorySegment} will be allocated.
 *
 * @param records records to test
 * @param segmentSize size for the {@link MemorySegment}
 */
private void test(Util.MockRecords records, int segmentSize) throws Exception {
	final int serializationOverhead = 4; // length encoding

	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();

	// -------------------------------------------------------------------------------------------------------------

	BufferBuilder bufferBuilder = createBufferBuilder(segmentSize);
	int numBytes = 0;
	for (SerializationTestType record : records) {
		serializer.serializeRecord(record);
		RecordSerializer.SerializationResult result = serializer.copyToBufferBuilder(bufferBuilder);
		numBytes += record.length() + serializationOverhead;

		if (numBytes < segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
		} else if (numBytes == segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD_MEMORY_SEGMENT_FULL, result);
			bufferBuilder = createBufferBuilder(segmentSize);
			numBytes = 0;
		} else {
			Assert.assertEquals(RecordSerializer.SerializationResult.PARTIAL_RECORD_MEMORY_SEGMENT_FULL, result);

			while (result.isFullBuffer()) {
				numBytes -= segmentSize;
				bufferBuilder = createBufferBuilder(segmentSize);
				result = serializer.copyToBufferBuilder(bufferBuilder);
			}

			Assert.assertTrue(result.isFullRecord());
		}
	}
}
 
Example 2
Source File: SpanningRecordSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Iterates over the provided records and tests whether the {@link SpanningRecordSerializer} returns the expected
 * {@link RecordSerializer.SerializationResult} values.
 *
 * <p>Only a single {@link MemorySegment} will be allocated.
 *
 * @param records records to test
 * @param segmentSize size for the {@link MemorySegment}
 */
private void test(Util.MockRecords records, int segmentSize) throws Exception {
	final int serializationOverhead = 4; // length encoding

	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();

	// -------------------------------------------------------------------------------------------------------------

	BufferBuilder bufferBuilder = createBufferBuilder(segmentSize);
	int numBytes = 0;
	for (SerializationTestType record : records) {
		serializer.serializeRecord(record);
		RecordSerializer.SerializationResult result = serializer.copyToBufferBuilder(bufferBuilder);
		numBytes += record.length() + serializationOverhead;

		if (numBytes < segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
		} else if (numBytes == segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD_MEMORY_SEGMENT_FULL, result);
			bufferBuilder = createBufferBuilder(segmentSize);
			numBytes = 0;
		} else {
			Assert.assertEquals(RecordSerializer.SerializationResult.PARTIAL_RECORD_MEMORY_SEGMENT_FULL, result);

			while (result.isFullBuffer()) {
				numBytes -= segmentSize;
				bufferBuilder = createBufferBuilder(segmentSize);
				result = serializer.copyToBufferBuilder(bufferBuilder);
			}

			Assert.assertTrue(result.isFullRecord());
		}
	}
}
 
Example 3
Source File: SpanningRecordSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Iterates over the provided records and tests whether the {@link SpanningRecordSerializer} returns the expected
 * {@link RecordSerializer.SerializationResult} values.
 *
 * <p>Only a single {@link MemorySegment} will be allocated.
 *
 * @param records records to test
 * @param segmentSize size for the {@link MemorySegment}
 */
private void test(Util.MockRecords records, int segmentSize) throws Exception {
	final int serializationOverhead = 4; // length encoding

	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();

	// -------------------------------------------------------------------------------------------------------------

	BufferBuilder bufferBuilder = createBufferBuilder(segmentSize);
	int numBytes = 0;
	for (SerializationTestType record : records) {
		serializer.serializeRecord(record);
		RecordSerializer.SerializationResult result = serializer.copyToBufferBuilder(bufferBuilder);
		numBytes += record.length() + serializationOverhead;

		if (numBytes < segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD, result);
		} else if (numBytes == segmentSize) {
			Assert.assertEquals(RecordSerializer.SerializationResult.FULL_RECORD_MEMORY_SEGMENT_FULL, result);
			bufferBuilder = createBufferBuilder(segmentSize);
			numBytes = 0;
		} else {
			Assert.assertEquals(RecordSerializer.SerializationResult.PARTIAL_RECORD_MEMORY_SEGMENT_FULL, result);

			while (result.isFullBuffer()) {
				numBytes -= segmentSize;
				bufferBuilder = createBufferBuilder(segmentSize);
				result = serializer.copyToBufferBuilder(bufferBuilder);
			}

			Assert.assertTrue(result.isFullRecord());
		}
	}
}
 
Example 4
Source File: DataInputOutputSerializerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testWrapAsByteBuffer() {
	SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);

	DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
	MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(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.");
	}
}
 
Example 5
Source File: DataInputOutputSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testWrapAsByteBuffer() {
	SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);

	DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
	MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(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.");
	}
}
 
Example 6
Source File: DataInputOutputSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testWrapAsByteBuffer() {
	SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);

	DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
	MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(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.");
	}
}