Example #1
0
def serve():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--port', type=int, required=True, help='the port on which to serve')
    parser.add_argument(
        '--use_tls',
        default=False,
        type=resources.parse_bool,
        help='require a secure connection')
    args = parser.parse_args()

    server = test_common.test_server()
    test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(),
                                                    server)
    if args.use_tls:
        private_key = resources.private_key()
        certificate_chain = resources.certificate_chain()
        credentials = grpc.ssl_server_credentials(((private_key,
                                                    certificate_chain),))
        server.add_secure_port('[::]:{}'.format(args.port), credentials)
    else:
        server.add_insecure_port('[::]:{}'.format(args.port))

    server.start()
    _LOGGER.info('Server serving.')
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except BaseException as e:
        _LOGGER.info('Caught exception "%s"; stopping server...', e)
        server.stop(None)
        _LOGGER.info('Server stopped; exiting.')
Example #2
0
async def start_test_server(port=0,
                            secure=False,
                            server_credentials=None,
                            interceptors=None):
    server = aio.server(options=(('grpc.so_reuseport', 0), ),
                        interceptors=interceptors)
    servicer = TestServiceServicer()
    test_pb2_grpc.add_TestServiceServicer_to_server(servicer, server)

    server.add_generic_rpc_handlers(
        (_create_extra_generic_handler(servicer), ))

    if secure:
        if server_credentials is None:
            server_credentials = grpc.ssl_server_credentials([
                (resources.private_key(), resources.certificate_chain())
            ])
        port = server.add_secure_port('[::]:%d' % port, server_credentials)
    else:
        port = server.add_insecure_port('[::]:%d' % port)

    await server.start()

    # NOTE(lidizheng) returning the server to prevent it from deallocation
    return 'localhost:%d' % port, server
Example #3
0
def serve():
    parser = argparse.ArgumentParser()
    parser.add_argument('--port',
                        type=int,
                        required=True,
                        help='the port on which to serve')
    parser.add_argument('--use_tls',
                        default=False,
                        type=resources.parse_bool,
                        help='require a secure connection')
    args = parser.parse_args()

    server = test_common.test_server()
    test_pb2_grpc.add_TestServiceServicer_to_server(service.TestService(),
                                                    server)
    if args.use_tls:
        private_key = resources.private_key()
        certificate_chain = resources.certificate_chain()
        credentials = grpc.ssl_server_credentials(
            ((private_key, certificate_chain), ))
        server.add_secure_port('[::]:{}'.format(args.port), credentials)
    else:
        server.add_insecure_port('[::]:{}'.format(args.port))

    server.start()
    _LOGGER.info('Server serving.')
    server.wait_for_termination()
    _LOGGER.info('Server stopped; exiting.')
Example #4
0
async def start_test_server(secure=False):
    server = aio.server(options=(('grpc.so_reuseport', 0),))
    servicer = _TestServiceServicer()
    test_pb2_grpc.add_TestServiceServicer_to_server(servicer, server)

    # Add programatically extra methods not provided by the proto file
    # that are used during the tests
    rpc_method_handlers = {
        'UnaryCallWithSleep':
            grpc.unary_unary_rpc_method_handler(
                servicer.UnaryCallWithSleep,
                request_deserializer=messages_pb2.SimpleRequest.FromString,
                response_serializer=messages_pb2.SimpleResponse.
                SerializeToString)
    }
    extra_handler = grpc.method_handlers_generic_handler(
        'grpc.testing.TestService', rpc_method_handlers)
    server.add_generic_rpc_handlers((extra_handler,))

    if secure:
        server_credentials = grpc.local_server_credentials(
            grpc.LocalConnectionType.LOCAL_TCP)
        port = server.add_secure_port('[::]:0', server_credentials)
    else:
        port = server.add_insecure_port('[::]:0')

    await server.start()
    # NOTE(lidizheng) returning the server to prevent it from deallocation
    return 'localhost:%d' % port, server
 def setUp(self):
     self.server = test_common.test_server()
     test_pb2_grpc.add_TestServiceServicer_to_server(
         service.TestService(), self.server)
     port = self.server.add_insecure_port('[::]:0')
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.insecure_channel('localhost:{}'.format(port)))
Example #6
0
 def setUp(self):
     self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
     test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(),
                                                     self.server)
     port = self.server.add_insecure_port('[::]:0')
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.insecure_channel('localhost:{}'.format(port)))
Example #7
0
 def setUp(self):
     self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
     test_pb2_grpc.add_TestServiceServicer_to_server(
         methods.TestService(), self.server)
     port = self.server.add_insecure_port('[::]:0')
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.insecure_channel('localhost:{}'.format(port)))
 def setUp(self):
     self.server = test_common.test_server()
     test_pb2_grpc.add_TestServiceServicer_to_server(service.TestService(),
                                                     self.server)
     port = self.server.add_insecure_port('[::]:0')
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.insecure_channel('localhost:{}'.format(port)))
Example #9
0
async def start_test_server():
    server = aio.server(options=(('grpc.so_reuseport', 0), ))
    test_pb2_grpc.add_TestServiceServicer_to_server(_TestServiceServicer(),
                                                    server)
    port = server.add_insecure_port('[::]:0')
    await server.start()
    # NOTE(lidizheng) returning the server to prevent it from deallocation
    return 'localhost:%d' % port, server
Example #10
0
def start_test_server(port: int = 0) -> Tuple[str, Any]:
    server = grpc.server(futures.ThreadPoolExecutor())
    servicer = TestServiceServicer()
    test_pb2_grpc.add_TestServiceServicer_to_server(TestServiceServicer(),
                                                    server)

    server.add_generic_rpc_handlers(
        (_create_extra_generic_handler(servicer), ))
    port = server.add_insecure_port('[::]:%d' % port)
    server.start()
    return 'localhost:%d' % port, server
Example #11
0
def _configure_test_server(server: grpc.Server, port: int, secure_mode: bool,
                           server_id: str) -> None:
    test_pb2_grpc.add_TestServiceServicer_to_server(
        TestService(server_id, socket.gethostname()), server)
    listen_address = f"{_LISTEN_HOST}:{port}"
    if not secure_mode:
        server.add_insecure_port(listen_address)
    else:
        logger.info("Running with xDS Server credentials")
        server_fallback_creds = grpc.insecure_server_credentials()
        server_creds = grpc.xds_server_credentials(server_fallback_creds)
        server.add_secure_port(listen_address, server_creds)
Example #12
0
 def setUp(self):
     self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
     test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(),
                                                     self.server)
     port = self.server.add_secure_port(
         '[::]:0',
         grpc.ssl_server_credentials(
             [(resources.private_key(), resources.certificate_chain())]))
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.secure_channel('localhost:{}'.format(port),
                             grpc.ssl_channel_credentials(
                                 resources.test_root_certificates()), (
                                     ('grpc.ssl_target_name_override',
                                      _SERVER_HOST_OVERRIDE,),)))
Example #13
0
 def setUp(self):
     self.server = test_common.test_server()
     test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(),
                                                     self.server)
     port = self.server.add_secure_port(
         '[::]:0',
         grpc.ssl_server_credentials([(resources.private_key(),
                                       resources.certificate_chain())]))
     self.server.start()
     self.stub = test_pb2_grpc.TestServiceStub(
         grpc.secure_channel('localhost:{}'.format(port),
                             grpc.ssl_channel_credentials(
                                 resources.test_root_certificates()), ((
                                     'grpc.ssl_target_name_override',
                                     _SERVER_HOST_OVERRIDE,
                                 ),)))
Example #14
0
def serve():
    args = parse_interop_server_arguments()

    server = test_common.test_server()
    test_pb2_grpc.add_TestServiceServicer_to_server(service.TestService(),
                                                    server)
    if args.use_tls:
        credentials = get_server_credentials()
        server.add_secure_port('[::]:{}'.format(args.port), credentials)
    else:
        server.add_insecure_port('[::]:{}'.format(args.port))

    server.start()
    _LOGGER.info('Server serving.')
    server.wait_for_termination()
    _LOGGER.info('Server stopped; exiting.')
Example #15
0
async def start_test_server(port=0, secure=False, server_credentials=None):
    server = aio.server(options=(('grpc.so_reuseport', 0), ))
    servicer = _TestServiceServicer()
    test_pb2_grpc.add_TestServiceServicer_to_server(servicer, server)

    server.add_generic_rpc_handlers(
        (_create_extra_generic_handler(servicer), ))

    if secure:
        if server_credentials is None:
            server_credentials = grpc.local_server_credentials(
                grpc.LocalConnectionType.LOCAL_TCP)
        port = server.add_secure_port('[::]:%d' % port, server_credentials)
    else:
        port = server.add_insecure_port('[::]:%d' % port)

    await server.start()

    # NOTE(lidizheng) returning the server to prevent it from deallocation
    return 'localhost:%d' % port, server
Example #16
0
    async def setUp(self):
        self._async_server = aio.server(
            options=(('grpc.so_reuseport', 0), ),
            migration_thread_pool=ThreadPoolExecutor())

        test_pb2_grpc.add_TestServiceServicer_to_server(
            TestServiceServicer(), self._async_server)
        self._adhoc_handlers = _AdhocGenericHandler()
        self._async_server.add_generic_rpc_handlers((self._adhoc_handlers, ))

        port = self._async_server.add_insecure_port('[::]:0')
        address = 'localhost:%d' % port
        await self._async_server.start()

        # Create async stub
        self._async_channel = aio.insecure_channel(address,
                                                   options=_unique_options())
        self._async_stub = test_pb2_grpc.TestServiceStub(self._async_channel)

        # Create sync stub
        self._sync_channel = grpc.insecure_channel(address,
                                                   options=_unique_options())
        self._sync_stub = test_pb2_grpc.TestServiceStub(self._sync_channel)
Example #17
0
from tests.unit.framework.common import test_constants


# TODO (https://github.com/grpc/grpc/issues/19762)
# Change for an asynchronous server version once it's implemented.
class TestServiceServicer(test_pb2_grpc.TestServiceServicer):
    def UnaryCall(self, request, context):
        return messages_pb2.SimpleResponse()

    def EmptyCall(self, request, context):
        while True:
            sleep(test_constants.LONG_TIMEOUT)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Synchronous gRPC server.')
    parser.add_argument('--host_and_port',
                        required=True,
                        type=str,
                        nargs=1,
                        help='the host and port to listen.')
    args = parser.parse_args()

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=1),
                         options=(('grpc.so_reuseport', 1), ))
    test_pb2_grpc.add_TestServiceServicer_to_server(TestServiceServicer(),
                                                    server)
    server.add_insecure_port(args.host_and_port[0])
    server.start()
    server.wait_for_termination()