Esempio n. 1
0
 def testMakeSameProtoClassTwice(self):
     """Test that the DescriptorPool is used."""
     pool = descriptor_pool.DescriptorPool()
     proto_cls1 = proto_builder.MakeSimpleProtoClass(
         self._fields,
         full_name='net.proto2.python.public.proto_builder_test.Test',
         pool=pool)
     proto_cls2 = proto_builder.MakeSimpleProtoClass(
         self._fields,
         full_name='net.proto2.python.public.proto_builder_test.Test',
         pool=pool)
     self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
Esempio n. 2
0
 def testMakeSimpleProtoClass(self):
     """Test that we can create a proto class."""
     proto_cls = proto_builder.MakeSimpleProtoClass(
         self._fields,
         full_name='net.proto2.python.public.proto_builder_test.Test')
     proto = proto_cls()
     proto.foo = 12345
     proto.bar = 'asdf'
     self.assertMultiLineEqual('bar: "asdf"\nfoo: 12345\n',
                               text_format.MessageToString(proto))
Esempio n. 3
0
 def testOrderedFields(self):
     """Test that the field order is maintained when given an OrderedDict."""
     proto_cls = proto_builder.MakeSimpleProtoClass(
         self.ordered_fields,
         full_name='net.proto2.python.public.proto_builder_test.OrderedTest'
     )
     proto = proto_cls()
     proto.foo = 12345
     proto.bar = 'asdf'
     self.assertMultiLineEqual('foo: 12345\nbar: "asdf"\n',
                               text_format.MessageToString(proto))
Esempio n. 4
0
    def testMakeLargeProtoClass(self):
        """Test that large created protos don't use reserved field numbers."""
        num_fields = 123456
        fields = {
            'foo%d' % i: descriptor_pb2.FieldDescriptorProto.TYPE_INT64
            for i in range(num_fields)
        }
        proto_cls = proto_builder.MakeSimpleProtoClass(
            fields,
            full_name=
            'net.proto2.python.public.proto_builder_test.LargeProtoTest')

        reserved_field_numbers = set(
            range(descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER,
                  descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER + 1))
        proto_field_numbers = set(proto_cls.DESCRIPTOR.fields_by_number)
        self.assertFalse(
            reserved_field_numbers.intersection(proto_field_numbers))
Esempio n. 5
0
        mock_tracer.return_value = mock_tracer
        rpc = self._intercepted_channel(NoopTracer()).stream_stream(
            method='',
            request_serializer=EmptyMessage.SerializeToString,
            response_deserializer=EmptyMessage.FromString)(iter(
                [EmptyMessage()]))

        for resp in rpc:
            pass

        self.assertEqual(mock_tracer.end_span.call_count, 1)


EmptyMessage = proto_builder.MakeSimpleProtoClass(
    collections.OrderedDict([]),
    full_name='tests.test_client_interceptor.EmptyMessage')


def _start_server():
    """Starts an insecure grpc server."""
    return grpc.server(logging_pool.pool(max_workers=1),
                       options=(('grpc.so_reuseport', 0), ))


class StreamStreamMethodHandler(grpc.RpcMethodHandler):
    def __init__(self, stream_handler_func):
        self.request_streaming = True
        self.response_streaming = True
        self.request_deserializer = None
        self.response_serializer = EmptyMessage.SerializeToString
Esempio n. 6
0
    @property
    def val(self):
        return self['val']


AVRO_SCHEMA = '''{
"type": "record",
"name": "payload",
  "fields": [
    {"name": "val", "type": "int"}
  ]
}
'''

ProtobufPayloadClass = proto_builder.MakeSimpleProtoClass(
    OrderedDict([('val', FieldDescriptorProto.TYPE_INT64)]),
    full_name="example.Payload")


def make_protobuf_payload(val: int):
    p = ProtobufPayloadClass()
    p.val = val
    return p


class SchemaType(Enum):
    AVRO = 1
    PROTOBUF = 2


class SerdeClient: