Java Code Examples for org.apache.flink.testutils.serialization.types.Util#randomRecord()

The following examples show how to use org.apache.flink.testutils.serialization.types.Util#randomRecord() . 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 6 votes vote down vote up
@Test
public void testHasSerializedData() throws IOException {
	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();
	final SerializationTestType randomIntRecord = Util.randomRecord(SerializationTestTypeFactory.INT);

	Assert.assertFalse(serializer.hasSerializedData());

	serializer.serializeRecord(randomIntRecord);
	Assert.assertTrue(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder1 = createBufferBuilder(16);
	serializer.copyToBufferBuilder(bufferBuilder1);
	Assert.assertFalse(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder2 = createBufferBuilder(8);
	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	Assert.assertFalse(serializer.hasSerializedData());

	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	// Buffer builder full!
	Assert.assertTrue(serializer.hasSerializedData());
}
 
Example 2
Source File: SpanningRecordSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasSerializedData() throws IOException {
	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();
	final SerializationTestType randomIntRecord = Util.randomRecord(SerializationTestTypeFactory.INT);

	Assert.assertFalse(serializer.hasSerializedData());

	serializer.serializeRecord(randomIntRecord);
	Assert.assertTrue(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder1 = createBufferBuilder(16);
	serializer.copyToBufferBuilder(bufferBuilder1);
	Assert.assertFalse(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder2 = createBufferBuilder(8);
	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	Assert.assertFalse(serializer.hasSerializedData());

	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	// Buffer builder full!
	Assert.assertTrue(serializer.hasSerializedData());
}
 
Example 3
Source File: SpanningRecordSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasSerializedData() throws IOException {
	final SpanningRecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<>();
	final SerializationTestType randomIntRecord = Util.randomRecord(SerializationTestTypeFactory.INT);

	Assert.assertFalse(serializer.hasSerializedData());

	serializer.serializeRecord(randomIntRecord);
	Assert.assertTrue(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder1 = createBufferBuilder(16);
	serializer.copyToBufferBuilder(bufferBuilder1);
	Assert.assertFalse(serializer.hasSerializedData());

	final BufferBuilder bufferBuilder2 = createBufferBuilder(8);
	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	Assert.assertFalse(serializer.hasSerializedData());

	serializer.reset();
	serializer.copyToBufferBuilder(bufferBuilder2);
	// Buffer builder full!
	Assert.assertTrue(serializer.hasSerializedData());
}
 
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.");
	}
}