def run(server_address, secure): if secure: fallback_creds = grpc.experimental.insecure_channel_credentials() channel_creds = grpc.xds_channel_credentials(fallback_creds) channel = grpc.secure_channel(server_address, channel_creds) else: channel = grpc.insecure_channel(server_address) with channel: stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message)
def test_xds_creds_fallback_insecure(self): # Since there is no xDS server, the fallback credentials will be used. # In this case, insecure. server_fallback_creds = grpc.insecure_server_credentials() with xds_channel_server_without_xds( server_fallback_creds) as server_address: channel_fallback_creds = grpc.experimental.insecure_channel_credentials( ) channel_creds = grpc.xds_channel_credentials( channel_fallback_creds) with grpc.secure_channel(server_address, channel_creds) as channel: request = b"abc" response = channel.unary_unary("/test/method")( request, wait_for_ready=True) self.assertEqual(response, request)
def _run_single_channel(config: _ChannelConfiguration) -> None: global _global_rpc_id # pylint: disable=global-statement with config.condition: server = config.server channel = None if config.secure_mode: fallback_creds = grpc.experimental.insecure_channel_credentials() channel_creds = grpc.xds_channel_credentials(fallback_creds) channel = grpc.secure_channel(server, channel_creds) else: channel = grpc.insecure_channel(server) with channel: stub = test_pb2_grpc.TestServiceStub(channel) futures: Dict[int, Tuple[grpc.Future, str]] = {} while not _stop_event.is_set(): with config.condition: if config.qps == 0: config.condition.wait( timeout=_CONFIG_CHANGE_TIMEOUT.total_seconds()) continue else: duration_per_query = 1.0 / float(config.qps) request_id = None with _global_lock: request_id = _global_rpc_id _global_rpc_id += 1 _global_rpcs_started[config.method] += 1 start = time.time() end = start + duration_per_query with config.condition: _start_rpc(config.method, config.metadata, request_id, stub, float(config.rpc_timeout_sec), futures) with config.condition: _remove_completed_rpcs(futures, config.print_response) logger.debug(f"Currently {len(futures)} in-flight RPCs") now = time.time() while now < end: time.sleep(end - now) now = time.time() _cancel_all_rpcs(futures)
def test_xds_creds_fallback_ssl(self): # Since there is no xDS server, the fallback credentials will be used. # In this case, SSL credentials. server_fallback_creds = grpc.ssl_server_credentials( ((resources.private_key(), resources.certificate_chain()), )) with xds_channel_server_without_xds( server_fallback_creds) as server_address: override_options = (("grpc.ssl_target_name_override", "foo.test.google.fr"), ) channel_fallback_creds = grpc.ssl_channel_credentials( root_certificates=resources.test_root_certificates(), private_key=resources.private_key(), certificate_chain=resources.certificate_chain()) channel_creds = grpc.xds_channel_credentials( channel_fallback_creds) with grpc.secure_channel(server_address, channel_creds, options=override_options) as channel: request = b"abc" response = channel.unary_unary("/test/method")( request, wait_for_ready=True) self.assertEqual(response, request)