def test_behavior_outside_asyncio(self): # Ensures non-AsyncIO object can be initiated channel_creds = grpc.ssl_channel_credentials() # Ensures AsyncIO API not raising outside of AsyncIO. # NOTE(lidiz) This behavior is bound with GAPIC generator, and required # by test frameworks like pytest. In test frameworks, objects shared # across cases need to be created outside of AsyncIO coroutines. aio.insecure_channel('') aio.secure_channel('', channel_creds) aio.server() aio.init_grpc_aio() aio.shutdown_grpc_aio()
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
async def serve(): server = grpc.server() AwsAPI_pb2_grpc.add_S3Servicer_to_server(S3(), server) server.add_insecure_port('[::]:50051') await server.start() print('run server port 50051') await server.wait_for_termination()
async def run_grpc_server(host, port, query_parser): server = aio.server() server.add_insecure_port(f'{host}:{port}') servicer = Search(query_parser) search_pb2_grpc.add_SearchServicer_to_server(servicer, server) await server.start() await server.wait_for_termination()
async def setUp(self): self._server = aio.server() self._server.add_generic_rpc_handlers((_GenericHandler(),)) port = self._server.add_insecure_port('[::]:0') await self._server.start() self._channel = aio.insecure_channel('localhost:%d' % port)
async def run_grpc_server(host, port, handler): server = aio.server() server.add_insecure_port(f'{host}:{port}') servicer = Chromosome(handler) chromosome_pb2_grpc.add_ChromosomeServicer_to_server(servicer, server) await server.start() await server.wait_for_termination()
async def _start_async_server(): server = aio.server() server.add_insecure_port("[::]:50051") webrtc_pusher_pb2_grpc.add_WebRTC_PusherServicer_to_server( WebRTC_Pusher_Service(), server) await server.start() await server.wait_for_termination()
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
async def run_grpc_server(host, port, handler): server = aio.server() server.add_insecure_port(f'{host}:{port}') servicer = GeneSearch(handler) genesearch_pb2_grpc.add_GeneSearchServicer_to_server(servicer, server) await server.start() await server.wait_for_termination()
async def run(host, port): from grpc.experimental.aio import server from concurrent.futures import ThreadPoolExecutor server = server(ThreadPoolExecutor(50)) add_HelloServiceServicer_to_server(HelloService(), server) server.add_insecure_port("{}:{}".format(host, port)) await server.start() kz = KazooClient(hosts="127.0.0.1:2181") kz.start() zk_register = AIOZKRegister(kz_client=kz) await zk_register.register_server(HelloServiceServicer, host, port) async def shutdown(*args, **kwargs): await zk_register.stop() # close kazoo client after zk_register stoped kz.stop() kz.close() await server.stop(0.5) asyncio.get_event_loop().add_signal_handler( signal.SIGTERM, functools.partial(asyncio.ensure_future, shutdown())) await server.wait_for_termination()
async def _start_test_server(): server = aio.server() port = server.add_insecure_port('[::]:0') generic_handler = _GenericHandler() server.add_generic_rpc_handlers((generic_handler,)) await server.start() return 'localhost:%d' % port, server, generic_handler
async def test_secure_no_cert(self): handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = aio.server() server.add_generic_rpc_handlers((handler, )) server_cred = grpc.ssl_server_credentials(_SERVER_CERTS) port = server.add_secure_port('[::]:0', server_cred) await server.start() channel_creds = grpc.ssl_channel_credentials( root_certificates=_TEST_ROOT_CERTIFICATES) channel = aio.secure_channel('localhost:{}'.format(port), channel_creds, options=_PROPERTY_OPTIONS) response = await channel.unary_unary(_UNARY_UNARY)(_REQUEST) await channel.close() await server.stop(None) auth_data = pickle.loads(response) self.assertIsNone(auth_data[_ID]) self.assertIsNone(auth_data[_ID_KEY]) self.assertDictEqual( { 'security_level': [b'TSI_PRIVACY_AND_INTEGRITY'], 'transport_security_type': [b'ssl'], 'ssl_session_reused': [b'false'], }, auth_data[_AUTH_CTX])
def __init__(self, redis_address, dashboard_agent_port, redis_password=None, temp_dir=None, log_dir=None, metrics_export_port=None, node_manager_port=None, object_store_name=None, raylet_name=None): """Initialize the DashboardAgent object.""" # Public attributes are accessible for all agent modules. self.redis_address = dashboard_utils.address_tuple(redis_address) self.redis_password = redis_password self.temp_dir = temp_dir self.log_dir = log_dir self.dashboard_agent_port = dashboard_agent_port self.metrics_export_port = metrics_export_port self.node_manager_port = node_manager_port self.object_store_name = object_store_name self.raylet_name = raylet_name self.node_id = os.environ["RAY_NODE_ID"] assert self.node_id, "Empty node id (RAY_NODE_ID)." self.ip = ray._private.services.get_node_ip_address() self.server = aiogrpc.server(options=(("grpc.so_reuseport", 0), )) self.grpc_port = self.server.add_insecure_port( f"[::]:{self.dashboard_agent_port}") logger.info("Dashboard agent grpc address: %s:%s", self.ip, self.grpc_port) self.aioredis_client = None self.aiogrpc_raylet_channel = aiogrpc.insecure_channel("{}:{}".format( self.ip, self.node_manager_port)) self.http_session = None
async def test_session_resumption(self): # Set up a secure server handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = aio.server() server.add_generic_rpc_handlers((handler, )) server_cred = grpc.ssl_server_credentials(_SERVER_CERTS) port = server.add_secure_port('[::]:0', server_cred) await server.start() # Create a cache for TLS session tickets cache = session_cache.ssl_session_cache_lru(1) channel_creds = grpc.ssl_channel_credentials( root_certificates=_TEST_ROOT_CERTIFICATES) channel_options = _PROPERTY_OPTIONS + ( ('grpc.ssl_session_cache', cache), ) # Initial connection has no session to resume await self._do_one_shot_client_rpc( channel_creds, channel_options, port, expect_ssl_session_reused=[b'false']) # Subsequent connections resume sessions await self._do_one_shot_client_rpc(channel_creds, channel_options, port, expect_ssl_session_reused=[b'true']) await server.stop(None)
async def test_secure_client_cert(self): handler = grpc.method_handlers_generic_handler('test', { 'UnaryUnary': grpc.unary_unary_rpc_method_handler(handle_unary_unary) }) server = aio.server() server.add_generic_rpc_handlers((handler, )) server_cred = grpc.ssl_server_credentials( _SERVER_CERTS, root_certificates=_TEST_ROOT_CERTIFICATES, require_client_auth=True) port = server.add_secure_port('[::]:0', server_cred) await server.start() channel_creds = grpc.ssl_channel_credentials( root_certificates=_TEST_ROOT_CERTIFICATES, private_key=_PRIVATE_KEY, certificate_chain=_CERTIFICATE_CHAIN) channel = aio.secure_channel('localhost:{}'.format(port), channel_creds, options=_PROPERTY_OPTIONS) response = await channel.unary_unary(_UNARY_UNARY)(_REQUEST) await channel.close() await server.stop(None) auth_data = pickle.loads(response) auth_ctx = auth_data[_AUTH_CTX] self.assertCountEqual(_CLIENT_IDS, auth_data[_ID]) self.assertEqual('x509_subject_alternative_name', auth_data[_ID_KEY]) self.assertSequenceEqual([b'ssl'], auth_ctx['transport_security_type']) self.assertSequenceEqual([b'*.test.google.com'], auth_ctx['x509_common_name'])
def __init__(self, redis_address, redis_password=None, temp_dir=None, log_dir=None, node_manager_port=None, object_store_name=None, raylet_name=None): """Initialize the DashboardAgent object.""" self._agent_cls_list = dashboard_utils.get_all_modules( dashboard_utils.DashboardAgentModule) ip, port = redis_address.split(":") # Public attributes are accessible for all agent modules. self.redis_address = (ip, int(port)) self.redis_password = redis_password self.temp_dir = temp_dir self.log_dir = log_dir self.node_manager_port = node_manager_port self.object_store_name = object_store_name self.raylet_name = raylet_name self.ip = ray.services.get_node_ip_address() self.server = aiogrpc.server(options=(("grpc.so_reuseport", 0), )) listen_address = "[::]:0" logger.info("Dashboard agent listen at: %s", listen_address) self.port = self.server.add_insecure_port(listen_address) self.aioredis_client = None self.aiogrpc_raylet_channel = aiogrpc.insecure_channel("{}:{}".format( self.ip, self.node_manager_port)) self.http_session = aiohttp.ClientSession( loop=asyncio.get_event_loop())
async def _start_async_server(self, port): server = aio.server() server.add_insecure_port("[::]:%s" % port) protocol_pb2_grpc.add_ControlServiceActionsServicer_to_server( self, server) await server.start() await server.wait_for_termination()
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
async def run_grpc_server(host, port, handler): server = aio.server() server.add_insecure_port(f'{host}:{port}') servicer = PairwiseMacroSyntenyBlocks(handler) pairwisemacrosyntenyblocks_pb2_grpc\ .add_PairwiseMacroSyntenyBlocksServicer_to_server(servicer, server) await server.start() await server.wait_for_termination()
def test_behavior_outside_asyncio(self): # Ensures non-AsyncIO object can be initiated channel_creds = grpc.ssl_channel_credentials() # Ensures AsyncIO API NOT working outside of AsyncIO with self.assertRaises(RuntimeError): aio.insecure_channel('') with self.assertRaises(RuntimeError): aio.secure_channel('', channel_creds) with self.assertRaises(RuntimeError): aio.server() # Ensures init_grpc_aio fail outside of AsyncIO with self.assertRaises(RuntimeError): aio.init_grpc_aio()
async def setUp(self): self._server = aio.server() reflection.enable_server_reflection(_SERVICE_NAMES, self._server) port = self._server.add_insecure_port('[::]:0') await self._server.start() self._channel = aio.insecure_channel('localhost:%d' % port) self._stub = reflection_pb2_grpc.ServerReflectionStub(self._channel)
async def serve(): server = aio.server() platform_pb2_grpc.add_MpfHardwareServiceServicer_to_server( MpfHardwareService(), server) listen_addr = '[::]:50051' server.add_insecure_port(listen_addr) logging.info("Starting server on %s", listen_addr) await server.start() await server.wait_for_termination()
async def _start_test_server(): server = aio.server() port = server.add_insecure_port('[::]:0') server.add_generic_rpc_handlers(( _TestGenericHandlerForMethods(), _TestGenericHandlerItself(), )) await server.start() return 'localhost:%d' % port, server
async def listen(self, service, port): """Connect to remote host and port.""" server = aio.server() platform_pb2_grpc.add_MpfHardwareServiceServicer_to_server(service, server) listen_addr = "[::]:{}".format(port) server.add_insecure_port(listen_addr) self.log.info("Starting server on %s", listen_addr) await server.start() return server
async def test_unary_unary_body(): server = aio.server() port = server.add_insecure_port('[::]:0') server.add_generic_rpc_handlers((GenericHandler(),)) await server.start() async with aio.insecure_channel('localhost:%d' % port) as channel: unary_call = channel.unary_unary(_TEST_METHOD_PATH) response = await unary_call(_REQUEST) self.assertEqual(response, _RESPONSE)
async def _start_async_server(): server = aio.server() port = server.add_insecure_port(('localhost:%s' % 50051).encode('ASCII')) servicer = BenchmarkServer() benchmark_service_pb2_grpc.add_BenchmarkServiceServicer_to_server( servicer, server) await server.start() await server.wait_for_termination()
async def serve(host='127.0.0.1', port=50051): server = grpc_aio.server() helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port(f'{host}:{port}') await server.start() print(f'Serving on {host}:{port}') try: await server.wait_for_termination() finally: await server.stop(10)
async def start(self): # Server will enable channelz service self.server = aio.server(options=_DISABLE_REUSE_PORT + _ENABLE_CHANNELZ) port = self.server.add_insecure_port('[::]:0') self.address = 'localhost:%d' % port self.server.add_generic_rpc_handlers((_GenericHandler(),)) await self.server.start() # Channel will enable channelz service... self.channel = aio.insecure_channel(self.address, options=_ENABLE_CHANNELZ)
async def _start_async_server(): server = aio.server() port = server.add_insecure_port('localhost:%s' % 50051) servicer = benchmark_servicer.BenchmarkServicer() benchmark_service_pb2_grpc.add_BenchmarkServiceServicer_to_server( servicer, server) await server.start() logging.info('Benchmark server started at :%d' % port) await server.wait_for_termination()
async def run_worker_server(port: int) -> None: server = aio.server() servicer = worker_servicer.WorkerServicer() worker_service_pb2_grpc.add_WorkerServiceServicer_to_server( servicer, server) server.add_insecure_port('[::]:{}'.format(port)) await server.start() await servicer.wait_for_quit() await server.stop(None)