Exemplo n.º 1
0
    def test_json_format(self):
        class Test(Message):
            name: str

        class TestJsonFormat(Message):
            ref_field: Test
            list_test_field: List[Test]
            list_int32_field: List[int]
            map_field: Dict[str, Test]

        generate_proto_file()

        test = TestJsonFormat(
            ref_field=Test(name="Test"),
            list_test_field=[Test(name="Test")],
            list_int32_field=range(10),
            map_field={"test": Test(name="Test")},
        )
        dict_test = {
            "ref_field": {
                "name": "Test"
            },
            "list_test_field": [{
                "name": "Test"
            }],
            "list_int32_field": list(range(10)),
            "map_field": {
                "test": {
                    "name": "Test"
                }
            },
        }
        self.assertDictEqual(dict_test, test.message_to_dict())
        self.assertDictEqual(dict_test, json.loads(test.message_to_json()))
Exemplo n.º 2
0
    def run(
        cls,
        config: Optional[DefaultConfig] = None,
        host: Optional[str] = None,
        port: Optional[int] = None,
        server_credentials: Optional[grpc.ServerCredentials] = None,
        block: Optional[bool] = None,
    ):
        if config is None:
            config = DefaultConfig()
        host = host or config.GRPC_SERVER_HOST
        port = port or config.GRPC_SERVER_PORT

        socket_bind_test(host, port)

        cls.as_view()
        for bp_cls in cls.get_blueprints():
            bp_cls.as_view()

        generate_proto_file(
            template_path_root=config.PROTO_TEMPLATE_ROOT,
            template_path=config.PROTO_TEMPLATE_PATH,
        )

        if config.GRPC_SERVER_PROCESS_COUNT > 1:
            address_family = select_address_family(host)
            server_address = get_sockaddr(host, port, address_family)
            with socket.socket(address_family, socket.SOCK_STREAM) as sock:
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
                if sock.getsockopt(socket.SOL_SOCKET,
                                   socket.SO_REUSEPORT) == 0:
                    raise RuntimeError("Failed to set SO_REUSEPORT.")
                sock.bind(server_address)
                config.GRPC_SERVER_OPTIONS.append(("grpc.so_reuseport", 1))
                for _ in range(config.GRPC_SERVER_PROCESS_COUNT):
                    # NOTE: It is imperative that the worker subprocesses be forked before
                    # any gRPC servers start up. See
                    # https://github.com/grpc/grpc/issues/16001 for more details.
                    worker = multiprocessing.Process(
                        target=cls._run,
                        kwargs=dict(
                            config=config,
                            target=f"{host}:{port}",
                            server_credentials=server_credentials,
                            block=True,
                        ),
                    )
                    worker.start()
                    cls.workers.append(worker)
                if block:
                    for worker in cls.workers:
                        worker.join()
        else:
            return cls._run(
                config=config,
                target=f"{host}:{port}",
                server_credentials=server_credentials,
                block=block,
            )
Exemplo n.º 3
0
    def test_generate_proto_file(self):
        from grpcalchemy.orm import Message

        class TestNestedPackage(Message):
            test: str

        dir_name = "protos/v1"
        if os.path.exists(dir_name):
            rmtree("protos")
        generate_proto_file(template_path=dir_name)
        self.assertTrue(os.path.exists(dir_name))
        self.assertTrue(os.path.exists(os.path.join(dir_name, "__init__.py")))
        self.assertTrue(os.path.exists(os.path.join(dir_name, "README.md")))
        rmtree("protos")
Exemplo n.º 4
0
    def test_generate_proto_file_automatically(self):
        from grpcalchemy.orm import Message

        class TestNestedPackage(Message):
            __filename__ = "nested"
            test: str

        dir_name = "nested/protos/v1"
        if os.path.exists(dir_name):
            rmtree("nested")
        generate_proto_file(template_path=dir_name)
        self.assertTrue(os.path.exists(dir_name))
        self.assertTrue(os.path.exists(os.path.join(dir_name, "__init__.py")))
        self.assertTrue(os.path.exists(os.path.join(dir_name, "README.md")))
        self.assertTrue(os.path.exists(os.path.join(dir_name, "nested.proto")))
        self.assertTrue(os.path.exists(os.path.join(dir_name,
                                                    "nested_pb2.py")))
        self.assertTrue(
            os.path.exists(os.path.join(dir_name, "nested_pb2_grpc.py")))
        rmtree("nested")
Exemplo n.º 5
0
 def generate_proto_file(cls, config: DefaultConfig):
     generate_proto_file(
         template_path_root=config.PROTO_TEMPLATE_ROOT,
         template_path=config.PROTO_TEMPLATE_PATH,
         auto_generate=config.PROTO_AUTO_GENERATED,
     )
Exemplo n.º 6
0
 def generate_proto_file(cls):
     generate_proto_file(
         template_path_root=cls.config.PROTO_TEMPLATE_ROOT,
         template_path=cls.config.PROTO_TEMPLATE_PATH,
     )