MCPcopy Create free account
hub / github.com/apache/fory / test_oob_buffer

Function test_oob_buffer

python/pyfory/tests/test_cross_language.py:663–705  ·  view source on GitHub ↗
(in_band_file_path, out_of_band_file_path)

Source from the content-addressed store, hash-verified

661
662@cross_language_test
663def test_oob_buffer(in_band_file_path, out_of_band_file_path):
664 with open(in_band_file_path, "rb") as f:
665 in_band_bytes = f.read()
666 with open(out_of_band_file_path, "rb") as f:
667 out_of_band_buffer = pyfory.Buffer(f.read())
668 fory = pyfory.Fory(xlang=True, compatible=False, ref=True)
669 n_buffers = out_of_band_buffer.read_int32()
670 buffers = []
671 for i in range(n_buffers):
672 length = out_of_band_buffer.read_int32()
673 reader_index = out_of_band_buffer.get_reader_index()
674 buffers.append(out_of_band_buffer.slice(reader_index, length))
675 out_of_band_buffer.set_reader_index(out_of_band_buffer.get_reader_index() + length)
676 new_obj = fory.deserialize(in_band_bytes, buffers)
677 obj = [bytes(bytearray([0, 1])) for _ in range(10)]
678 assert new_obj == obj, (obj, new_obj)
679
680 buffer_objects = []
681 counter = 0
682
683 def buffer_callback(binary_object):
684 nonlocal counter
685 counter += 1
686 if counter % 2 == 0:
687 buffer_objects.append(binary_object)
688 return False
689 else:
690 return True
691
692 serialized = fory.serialize(obj, buffer_callback=buffer_callback)
693 # in_band_bytes size may be different because it may contain language-specific meta.
694 debug_print(f"{len(serialized), len(in_band_bytes)}")
695 debug_print(f"deserialized from other language {new_obj}")
696 debug_print(f"deserialized from python {fory.deserialize(serialized, [o.getbuffer() for o in buffer_objects])}")
697 fory.deserialize(serialized, [o.getbuffer() for o in buffer_objects])
698 with open(in_band_file_path, "wb+") as f:
699 f.write(serialized)
700 out_of_band_buffer.write_int32(len(buffer_objects))
701 for buffer_object in buffer_objects:
702 out_of_band_buffer.write_int32(buffer_object.total_bytes())
703 buffer_object.write_to(out_of_band_buffer)
704 with open(out_of_band_file_path, "wb+") as f:
705 f.write(out_of_band_buffer.to_bytes(0, out_of_band_buffer.get_writer_index()))
706
707
708@cross_language_test

Callers

nothing calls this directly

Calls 15

read_int32Method · 0.95
deserializeMethod · 0.95
serializeMethod · 0.95
write_int32Method · 0.95
debug_printFunction · 0.70
readMethod · 0.65
writeMethod · 0.65
BufferMethod · 0.45
ForyMethod · 0.45
appendMethod · 0.45
sliceMethod · 0.45
getbufferMethod · 0.45

Tested by

no test coverage detected