def test_write_message_begin(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_message_begin('test', TType.STRING, 1) p.write_message_end() assert_equal("80 01 00 0b 00 00 00 04 74 65 73 74 00 00 00 01", hexlify(b.getvalue()))
def test_write_empty_struct(): b = TMemoryBuffer() item = TItem() p = proto.TCyBinaryProtocol(b) p.write_struct(item) p.write_message_end() assert "00" == hexlify(b.getvalue())
def base64_thrift(thrift_obj): trans = TMemoryBuffer() tbp = TBinaryProtocol(trans) thrift_obj.write(tbp) return b64encode(bytes(trans.getvalue())).strip()
def test_write_bool(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.BOOL, 1) b.flush() assert "01" == hexlify(b.getvalue())
def test_write_message_begin(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_message_begin("test", TType.STRING, 1) p.write_message_end() assert "80 01 00 0b 00 00 00 04 74 65 73 74 00 00 00 01" == \ hexlify(b.getvalue())
def test_write_i8(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.I08, 123) b.flush() assert "7b" == hexlify(b.getvalue())
def test_write_i32(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.I32, 1234567890) b.flush() assert "49 96 02 d2" == hexlify(b.getvalue())
def test_write_i16(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.I16, 12345) b.flush() assert "30 39" == hexlify(b.getvalue())
def test_write_message_begin_no_strict(): b = TMemoryBuffer() trans = TCyBufferedTransport(b) b = proto.TCyBinaryProtocol(trans, strict_write=False) b.write_message_begin("test", TType.STRING, 1) b.write_message_end() assert "00 00 00 04 74 65 73 74 0b 00 00 00 01" == \ hexlify(trans.getvalue())
def test_write_empty_struct(): b = TMemoryBuffer() trans = TCyBufferedTransport(b) b = proto.TCyBinaryProtocol(trans) item = TItem() b.write_struct(item) b.write_message_end() assert "00" == hexlify(trans.getvalue())
def test_read_empty_struct(): b = TMemoryBuffer(b"\x00") b = TCyBufferedTransport(b) b = proto.TCyBinaryProtocol(b) _item = TItem() _item2 = TItem() b.read_struct(_item2) assert _item == _item2
def test_read_struct(): b = TMemoryBuffer(b"\x08\x00\x01\x00\x00\x00{\x0f\x00\x02\x0b\x00\x00\x00" b"\x02\x00\x00\x00\x06123456\x00\x00\x00\x06abcdef\x00") b = TCyBufferedTransport(b) b = proto.TCyBinaryProtocol(b) _item = TItem(id=123, phones=["123456", "abcdef"]) _item2 = TItem() b.read_struct(_item2) assert _item == _item2
def test_write_struct(): b = TMemoryBuffer() item = TItem(id=123, phones=["123456", "abcdef"]) p = proto.TCyBinaryProtocol(b) p.write_struct(item) p.write_message_end() assert ("08 00 01 00 00 00 7b 0f 00 02 0b 00 00 00 02 00 00 00 " "06 31 32 33 34 35 36 00 00 00 06 61 62 63 64 65 66 00") == \ hexlify(b.getvalue())
def thrift_obj_in_bytes(thrift_obj): # pragma: no cover """ Returns TBinaryProtocol encoded Thrift object. :param thrift_obj: thrift object to encode :returns: thrift object in TBinaryProtocol format bytes. """ trans = TMemoryBuffer() thrift_obj.write(TBinaryProtocol(trans)) return bytes(trans.getvalue())
def thrift_obj_in_bytes(thrift_obj): """ Encodes a Thrift object using TBinaryProtocol. :param thrift_obj :returns: TBinaryProtocol bytes represenation of thrift_obj. """ trans = TMemoryBuffer() thrift_obj.write(TBinaryProtocol(trans)) return trans.getvalue(); return bytes(trans.getvalue())
def thrift_obj_in_bytes(thrift_obj): """ Encodes a Thrift object using TBinaryProtocol. :param thrift_obj :returns: TBinaryProtocol bytes represenation of thrift_obj. """ trans = TMemoryBuffer() thrift_obj.write(TBinaryProtocol(trans)) return trans.getvalue() return bytes(trans.getvalue())
def span_to_bytes(thrift_span): """ Returns a TBinaryProtocol encoded Thrift span. :param thrift_span: thrift object to encode. :returns: thrift object in TBinaryProtocol format bytes. """ transport = TMemoryBuffer() protocol = TBinaryProtocol(transport) thrift_span.write(protocol) return bytes(transport.getvalue())
def message_received(self, frame): output_buffer = TMemoryBuffer() yield from self._processor.process( iprot=self._protocol_factory.get_protocol(TMemoryBuffer(frame)), oprot=self._protocol_factory.get_protocol(output_buffer), ) output_frame = output_buffer.getvalue() output_length = struct.pack('!i', len(output_frame)) self._transport.write(output_length + output_frame)
def encode_bytes_list(binary_thrift_obj_list): # pragma: no cover """ Returns a TBinaryProtocol encoded list of Thrift objects. :param binary_thrift_obj_list: list of TBinaryProtocol objects to encode. :returns: bynary object representing the encoded list. """ transport = TMemoryBuffer() write_list_begin(transport, TType.STRUCT, len(binary_thrift_obj_list)) for thrift_bin in binary_thrift_obj_list: transport.write(thrift_bin) return bytes(transport.getvalue())
def test_write_string(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.STRING, "hello world!") b.flush() assert "00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" == \ hexlify(b.getvalue()) b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.STRING, u("你好世界")) b.flush() assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \ hexlify(b.getvalue())
def thrift_objs_in_bytes(thrift_obj_list): # pragma: no cover """ Returns TBinaryProtocol encoded Thrift objects. :param thrift_obj_list: thrift objects list to encode :returns: thrift objects in TBinaryProtocol format bytes. """ transport = TMemoryBuffer() protocol = TBinaryProtocol(transport) write_list_begin(transport, TType.STRUCT, len(thrift_obj_list)) for thrift_obj in thrift_obj_list: thrift_obj.write(protocol) return bytes(transport.getvalue())
def test_pack_string(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.STRING, "hello world!") p.write_message_end() assert "00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" == \ hexlify(b.getvalue()) b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.STRING, u("你好世界")) p.write_message_end() assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \ hexlify(b.getvalue())
def deserialize(base, buf, protocol_factory=TBinaryProtocolFactory()): transport = TMemoryBuffer(buf) protocol = protocol_factory.get_protocol(transport) base.read(protocol) return base
def test_json_proto_api_write(): obj = TItem(id=13, phones=["5234", "12346456"]) trans = TMemoryBuffer() p = TJSONProtocol(trans) p.write_struct(obj) data = trans.getvalue().decode("utf-8") length = data[0:4] import json data = json.loads(data[4:]) assert length == "\x00\x00\x00S" and data == { "metadata": {"version": 1}, "payload": {"phones": ["5234", "12346456"], "id": 13}}
def test_read_struct(): b = TMemoryBuffer(b'\x08\x00\x01\x00\x00\x00{\x0f\x00\x02\x0b\x00\x00\x00' b'\x02\x00\x00\x00\x06123456\x00\x00\x00\x06abcdef\x00') _item = TItem(id=123, phones=['123456', 'abcdef']) _item2 = TItem() proto.TCyBinaryProtocol(b).read_struct(_item2) assert_equal(_item, _item2)
def test_read_wrong_arg_type(): class TWrongTypeItem(TPayload): thrift_spec = { 1: (TType.STRING, "id"), 2: (TType.LIST, "phones", (TType.STRING)), } default_spec = [("id", None), ("phones", None)] trans = TCyBufferedTransport(TMemoryBuffer()) b = proto.TCyBinaryProtocol(trans) item = TItem(id=58, phones=["123456", "abcdef"]) b.write_struct(item) b.write_message_end() item2 = TWrongTypeItem() try: b.read_struct(item2) except Exception: pass item3 = TItem(id=123, phones=["123456", "abcdef"]) b.write_struct(item3) b.write_message_end() item4 = TItem() b.read_struct(item4) assert item3 == item4
def test_skip_string(): b = TCyBufferedTransport(TMemoryBuffer()) proto.write_val(b, TType.STRING, "hello world") proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.STRING) assert 123 == proto.read_val(b, TType.I32)
def test_skip_double(): b = TCyBufferedTransport(TMemoryBuffer()) proto.write_val(b, TType.DOUBLE, 0.123425897) proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.DOUBLE) assert 123 == proto.read_val(b, TType.I32)
def test_skip_list(): b = TCyBufferedTransport(TMemoryBuffer()) proto.write_val(b, TType.LIST, [5, 6, 7, 8, 9], spec=TType.I32) proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.LIST) assert 123 == proto.read_val(b, TType.I32)
def test_skip_bool(): b = TCyBufferedTransport(TMemoryBuffer()) proto.write_val(b, TType.BOOL, 1) proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.BOOL) assert 123 == proto.read_val(b, TType.I32)
def test_buffered_read(): s = TMemoryBuffer() t = TCyBufferedTransport(s) t.write(b"ping") t.flush() assert t.read(4) == b"ping"
def test_write_huge_struct(): b = TMemoryBuffer() b = TCyBufferedTransport(b) b = proto.TCyBinaryProtocol(b) item = TItem(id=12345, phones=["1234567890"] * 100000) b.write_struct(item) b.write_message_end()
def _decode_binary_thrift_objs(obj): spans = [] trans = TMemoryBuffer(obj) _, size = read_list_begin(trans) for _ in range(size): span = zipkin_core.Span() span.read(TBinaryProtocol(trans)) spans.append(span) return spans
def test_skip_map(): b = TCyBufferedTransport(TMemoryBuffer()) proto.write_val(b, TType.MAP, {"hello": 0.3456}, spec=(TType.STRING, TType.DOUBLE)) proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.MAP) assert 123 == proto.read_val(b, TType.I32)
def decode_thrift(encoded_spans): spans = [] trans = TMemoryBuffer(encoded_spans) _, size = read_list_begin(trans) for _ in range(size): span_obj = zipkin_core.Span() span_obj.read(TBinaryProtocol(trans)) spans.append(span_obj) return spans
def test_json_proto_api_read(): obj = TItem(id=13, phones=["5234", "12346456"]) trans = TMemoryBuffer() p = TJSONProtocol(trans) p.write_struct(obj) obj2 = TItem() obj2 = p.read_struct(obj2) assert obj.id == 13 and obj.phones == ["5234", "12346456"]
def read_it(stream): stream_bytes = stream.read() transport_in = TMemoryBuffer(stream_bytes) protocol_in = TBinaryProtocol(transport_in) topology = storm_thrift.StormTopology() topology.read(protocol_in) cls._topology = topology cls.thrift_bolts = topology.bolts cls.thrift_spouts = topology.spouts # Can't reconstruct Python specs from Thrift. cls.specs = []
def test_transport_mismatch(): s = TMemoryBuffer() t1 = TCyBufferedTransport(s) t1.write(b"\x80\x01\x00\x01\x00\x00\x00\x04ping hello world") t1.flush() with pytest.raises(TTransportException) as exc: t2 = TCyFramedTransport(s) t2.read(4) assert "No frame" in str(exc.value)
def test_skip_struct(): b = TCyBufferedTransport(TMemoryBuffer()) p = proto.TCyBinaryProtocol(b) item = TItem(id=123, phones=["123456", "abcdef"]) p.write_struct(item) p.write_message_end() proto.write_val(b, TType.I32, 123) b.flush() proto.skip(b, TType.STRUCT) assert 123 == proto.read_val(b, TType.I32)
def test_pack_i32(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.I32, 1234567890) p.write_message_end() assert "49 96 02 d2" == hexlify(b.getvalue())
def _build_log_message(span): trans = TMemoryBuffer() protocol = TBinaryProtocol(trans=trans) span.write(protocol) return base64.b64encode(trans.getvalue())
def test_pack_i16(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.I16, 12345) p.write_message_end() assert "30 39" == hexlify(b.getvalue())
def test_write_double(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.DOUBLE, 1234567890.1234567890) b.flush() assert "41 d2 65 80 b4 87 e6 b7" == hexlify(b.getvalue())
def serialize(thrift_object, protocol_factory=TBinaryProtocolFactory()): transport = TMemoryBuffer() protocol = protocol_factory.get_protocol(transport) thrift_object.write(protocol) return transport.getvalue()
def test_pack_i8(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.I08, 123) p.write_message_end() assert "7b" == hexlify(b.getvalue())
def test_pack_i64(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.I64, 1234567890123456789) p.write_message_end() assert "11 22 10 f4 7d e9 81 15" == hexlify(b.getvalue())
def write_it(stream): transport_out = TMemoryBuffer() protocol_out = TBinaryProtocol(transport_out) cls.thrift_topology.write(protocol_out) transport_bytes = transport_out.getvalue() stream.write(transport_bytes)
def test_write_i64(): b = TMemoryBuffer() b = TCyBufferedTransport(b) proto.write_val(b, TType.I64, 1234567890123456789) b.flush() assert "11 22 10 f4 7d e9 81 15" == hexlify(b.getvalue())
def test_pack_double(): b = TMemoryBuffer() p = proto.TCyBinaryProtocol(b) p.write_val(TType.DOUBLE, 1234567890.1234567890) p.write_message_end() assert "41 d2 65 80 b4 87 e6 b7" == hexlify(b.getvalue())