Merge pull request #1031 from nathanielmanistaatgoogle/service_name

Add service name to Python early_adopter
pull/1070/head
Masood Malekghassemi 10 years ago
commit d942720266
  1. 27
      src/compiler/python_generator.cc
  2. 5
      src/python/interop/interop/_insecure_interop_test.py
  3. 6
      src/python/interop/interop/_secure_interop_test.py
  4. 7
      src/python/interop/interop/client.py
  5. 38
      src/python/interop/interop/methods.py
  6. 5
      src/python/interop/interop/server.py
  7. 82
      src/python/src/grpc/early_adopter/_face_utilities.py
  8. 5
      src/python/src/grpc/early_adopter/_reexport.py
  9. 59
      src/python/src/grpc/early_adopter/implementations.py
  10. 8
      src/python/src/grpc/early_adopter/implementations_test.py

@ -229,7 +229,8 @@ bool GetModuleAndMessagePath(const Descriptor* type,
return true; return true;
} }
bool PrintServerFactory(const ServiceDescriptor* service, Printer* out) { bool PrintServerFactory(const std::string& package_qualified_service_name,
const ServiceDescriptor* service, Printer* out) {
out->Print("def early_adopter_create_$Service$_server(servicer, port, " out->Print("def early_adopter_create_$Service$_server(servicer, port, "
"root_certificates, key_chain_pairs):\n", "root_certificates, key_chain_pairs):\n",
"Service", service->name()); "Service", service->name());
@ -293,17 +294,18 @@ bool PrintServerFactory(const ServiceDescriptor* service, Printer* out) {
out->Print("),\n"); out->Print("),\n");
} }
out->Print("}\n"); out->Print("}\n");
// out->Print("return implementations.insecure_server("
// "method_service_descriptions, port)\n");
out->Print( out->Print(
"return implementations.secure_server(" "return implementations.secure_server("
"method_service_descriptions, port, root_certificates," "\"$PackageQualifiedServiceName$\","
" key_chain_pairs)\n"); " method_service_descriptions, port, root_certificates,"
" key_chain_pairs)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
} }
return true; return true;
} }
bool PrintStubFactory(const ServiceDescriptor* service, Printer* out) { bool PrintStubFactory(const std::string& package_qualified_service_name,
const ServiceDescriptor* service, Printer* out) {
map<std::string, std::string> dict = ListToDict({ map<std::string, std::string> dict = ListToDict({
"Service", service->name(), "Service", service->name(),
}); });
@ -369,7 +371,9 @@ bool PrintStubFactory(const ServiceDescriptor* service, Printer* out) {
out->Print("}\n"); out->Print("}\n");
out->Print( out->Print(
"return implementations.insecure_stub(" "return implementations.insecure_stub("
"method_invocation_descriptions, host, port)\n"); "\"$PackageQualifiedServiceName$\","
" method_invocation_descriptions, host, port)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
} }
return true; return true;
} }
@ -392,13 +396,18 @@ pair<bool, std::string> GetServices(const FileDescriptor* file) {
if (!PrintPreamble(file, &out)) { if (!PrintPreamble(file, &out)) {
return make_pair(false, ""); return make_pair(false, "");
} }
auto package = file->package();
if (!package.empty()) {
package = package.append(".");
}
for (int i = 0; i < file->service_count(); ++i) { for (int i = 0; i < file->service_count(); ++i) {
auto service = file->service(i); auto service = file->service(i);
auto package_qualified_service_name = package + service->name();
if (!(PrintServicer(service, &out) && if (!(PrintServicer(service, &out) &&
PrintServer(service, &out) && PrintServer(service, &out) &&
PrintStub(service, &out) && PrintStub(service, &out) &&
PrintServerFactory(service, &out) && PrintServerFactory(package_qualified_service_name, service, &out) &&
PrintStubFactory(service, &out))) { PrintStubFactory(package_qualified_service_name, service, &out))) {
return make_pair(false, ""); return make_pair(false, "");
} }
} }

@ -42,11 +42,12 @@ class InsecureInteropTest(
unittest.TestCase): unittest.TestCase):
def setUp(self): def setUp(self):
self.server = implementations.insecure_server(methods.SERVER_METHODS, 0) self.server = implementations.insecure_server(
methods.SERVICE_NAME, methods.SERVER_METHODS, 0)
self.server.start() self.server.start()
port = self.server.port() port = self.server.port()
self.stub = implementations.insecure_stub( self.stub = implementations.insecure_stub(
methods.CLIENT_METHODS, 'localhost', port) methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port)
def tearDown(self): def tearDown(self):
self.server.stop() self.server.stop()

@ -46,12 +46,12 @@ class SecureInteropTest(
def setUp(self): def setUp(self):
self.server = implementations.secure_server( self.server = implementations.secure_server(
methods.SERVER_METHODS, 0, resources.private_key(), methods.SERVICE_NAME, methods.SERVER_METHODS, 0,
resources.certificate_chain()) resources.private_key(), resources.certificate_chain())
self.server.start() self.server.start()
port = self.server.port() port = self.server.port()
self.stub = implementations.secure_stub( self.stub = implementations.secure_stub(
methods.CLIENT_METHODS, 'localhost', port, methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port,
resources.test_root_certificates(), None, None, resources.test_root_certificates(), None, None,
server_host_override=_SERVER_HOST_OVERRIDE) server_host_override=_SERVER_HOST_OVERRIDE)

@ -67,12 +67,13 @@ def _stub(args):
root_certificates = resources.prod_root_certificates() root_certificates = resources.prod_root_certificates()
stub = implementations.secure_stub( stub = implementations.secure_stub(
methods.CLIENT_METHODS, args.server_host, args.server_port, methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
root_certificates, None, None, args.server_port, root_certificates, None, None,
server_host_override=args.server_host_override) server_host_override=args.server_host_override)
else: else:
stub = implementations.insecure_stub( stub = implementations.insecure_stub(
methods.CLIENT_METHODS, args.server_host, args.server_port) methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
args.server_port)
return stub return stub

@ -122,31 +122,31 @@ _SERVER_HALF_DUPLEX_CALL = utilities.stream_stream_service_description(
messages_pb2.StreamingOutputCallResponse.SerializeToString) messages_pb2.StreamingOutputCallResponse.SerializeToString)
_SERVICE_NAME = '/grpc.testing.TestService' SERVICE_NAME = 'grpc.testing.TestService'
EMPTY_CALL_METHOD_NAME = _SERVICE_NAME + '/EmptyCall' _EMPTY_CALL_METHOD_NAME = 'EmptyCall'
UNARY_CALL_METHOD_NAME = _SERVICE_NAME + '/UnaryCall' _UNARY_CALL_METHOD_NAME = 'UnaryCall'
STREAMING_OUTPUT_CALL_METHOD_NAME = _SERVICE_NAME + '/StreamingOutputCall' _STREAMING_OUTPUT_CALL_METHOD_NAME = 'StreamingOutputCall'
STREAMING_INPUT_CALL_METHOD_NAME = _SERVICE_NAME + '/StreamingInputCall' _STREAMING_INPUT_CALL_METHOD_NAME = 'StreamingInputCall'
FULL_DUPLEX_CALL_METHOD_NAME = _SERVICE_NAME + '/FullDuplexCall' _FULL_DUPLEX_CALL_METHOD_NAME = 'FullDuplexCall'
HALF_DUPLEX_CALL_METHOD_NAME = _SERVICE_NAME + '/HalfDuplexCall' _HALF_DUPLEX_CALL_METHOD_NAME = 'HalfDuplexCall'
CLIENT_METHODS = { CLIENT_METHODS = {
EMPTY_CALL_METHOD_NAME: _CLIENT_EMPTY_CALL, _EMPTY_CALL_METHOD_NAME: _CLIENT_EMPTY_CALL,
UNARY_CALL_METHOD_NAME: _CLIENT_UNARY_CALL, _UNARY_CALL_METHOD_NAME: _CLIENT_UNARY_CALL,
STREAMING_OUTPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_OUTPUT_CALL, _STREAMING_OUTPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_OUTPUT_CALL,
STREAMING_INPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_INPUT_CALL, _STREAMING_INPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_INPUT_CALL,
FULL_DUPLEX_CALL_METHOD_NAME: _CLIENT_FULL_DUPLEX_CALL, _FULL_DUPLEX_CALL_METHOD_NAME: _CLIENT_FULL_DUPLEX_CALL,
HALF_DUPLEX_CALL_METHOD_NAME: _CLIENT_HALF_DUPLEX_CALL, _HALF_DUPLEX_CALL_METHOD_NAME: _CLIENT_HALF_DUPLEX_CALL,
} }
SERVER_METHODS = { SERVER_METHODS = {
EMPTY_CALL_METHOD_NAME: _SERVER_EMPTY_CALL, _EMPTY_CALL_METHOD_NAME: _SERVER_EMPTY_CALL,
UNARY_CALL_METHOD_NAME: _SERVER_UNARY_CALL, _UNARY_CALL_METHOD_NAME: _SERVER_UNARY_CALL,
STREAMING_OUTPUT_CALL_METHOD_NAME: _SERVER_STREAMING_OUTPUT_CALL, _STREAMING_OUTPUT_CALL_METHOD_NAME: _SERVER_STREAMING_OUTPUT_CALL,
STREAMING_INPUT_CALL_METHOD_NAME: _SERVER_STREAMING_INPUT_CALL, _STREAMING_INPUT_CALL_METHOD_NAME: _SERVER_STREAMING_INPUT_CALL,
FULL_DUPLEX_CALL_METHOD_NAME: _SERVER_FULL_DUPLEX_CALL, _FULL_DUPLEX_CALL_METHOD_NAME: _SERVER_FULL_DUPLEX_CALL,
HALF_DUPLEX_CALL_METHOD_NAME: _SERVER_HALF_DUPLEX_CALL, _HALF_DUPLEX_CALL_METHOD_NAME: _SERVER_HALF_DUPLEX_CALL,
} }

@ -54,10 +54,11 @@ def serve():
private_key = resources.private_key() private_key = resources.private_key()
certificate_chain = resources.certificate_chain() certificate_chain = resources.certificate_chain()
server = implementations.secure_server( server = implementations.secure_server(
methods.SERVER_METHODS, args.port, private_key, certificate_chain) methods.SERVICE_NAME, methods.SERVER_METHODS, args.port, private_key,
certificate_chain)
else: else:
server = implementations.insecure_server( server = implementations.insecure_server(
methods.SERVER_METHODS, args.port) methods.SERVICE_NAME, methods.SERVER_METHODS, args.port)
server.start() server.start()
logging.info('Server serving.') logging.info('Server serving.')

@ -38,16 +38,28 @@ from grpc.early_adopter import _reexport
from grpc.early_adopter import interfaces from grpc.early_adopter import interfaces
def _qualified_name(service_name, method_name):
return '/%s/%s' % (service_name, method_name)
# TODO(nathaniel): This structure is getting bloated; it could be shrunk if
# implementations._Stub used a generic rather than a dynamic underlying
# face-layer stub.
class InvocationBreakdown(object): class InvocationBreakdown(object):
"""An intermediate representation of invocation-side views of RPC methods. """An intermediate representation of invocation-side views of RPC methods.
Attributes: Attributes:
cardinalities: A dictionary from RPC method name to interfaces.Cardinality cardinalities: A dictionary from RPC method name to interfaces.Cardinality
value. value.
request_serializers: A dictionary from RPC method name to callable qualified_names: A dictionary from unqualified RPC method name to
behavior to be used serializing request values for the RPC. service-qualified RPC method name.
response_deserializers: A dictionary from RPC method name to callable face_cardinalities: A dictionary from service-qualified RPC method name to
behavior to be used deserializing response values for the RPC. to cardinality.Cardinality value.
request_serializers: A dictionary from service-qualified RPC method name to
callable behavior to be used serializing request values for the RPC.
response_deserializers: A dictionary from service-qualified RPC method name
to callable behavior to be used deserializing response values for the
RPC.
""" """
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@ -56,7 +68,8 @@ class _EasyInvocationBreakdown(
InvocationBreakdown, InvocationBreakdown,
collections.namedtuple( collections.namedtuple(
'_EasyInvocationBreakdown', '_EasyInvocationBreakdown',
('cardinalities', 'request_serializers', 'response_deserializers'))): ('cardinalities', 'qualified_names', 'face_cardinalities',
'request_serializers', 'response_deserializers'))):
pass pass
@ -64,12 +77,12 @@ class ServiceBreakdown(object):
"""An intermediate representation of service-side views of RPC methods. """An intermediate representation of service-side views of RPC methods.
Attributes: Attributes:
implementations: A dictionary from RPC method name to implementations: A dictionary from service-qualified RPC method name to
face_interfaces.MethodImplementation implementing the RPC method. face_interfaces.MethodImplementation implementing the RPC method.
request_deserializers: A dictionary from RPC method name to callable request_deserializers: A dictionary from service-qualified RPC method name
behavior to be used deserializing request values for the RPC. to callable behavior to be used deserializing request values for the RPC.
response_serializers: A dictionary from RPC method name to callable response_serializers: A dictionary from service-qualified RPC method name
behavior to be used serializing response values for the RPC. to callable behavior to be used serializing response values for the RPC.
""" """
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@ -82,10 +95,11 @@ class _EasyServiceBreakdown(
pass pass
def break_down_invocation(method_descriptions): def break_down_invocation(service_name, method_descriptions):
"""Derives an InvocationBreakdown from several RPC method descriptions. """Derives an InvocationBreakdown from several RPC method descriptions.
Args: Args:
service_name: The package-qualified full name of the service.
method_descriptions: A dictionary from RPC method name to method_descriptions: A dictionary from RPC method name to
interfaces.RpcMethodInvocationDescription describing the RPCs. interfaces.RpcMethodInvocationDescription describing the RPCs.
@ -93,17 +107,26 @@ def break_down_invocation(method_descriptions):
An InvocationBreakdown corresponding to the given method descriptions. An InvocationBreakdown corresponding to the given method descriptions.
""" """
cardinalities = {} cardinalities = {}
qualified_names = {}
face_cardinalities = {}
request_serializers = {} request_serializers = {}
response_deserializers = {} response_deserializers = {}
for name, method_description in method_descriptions.iteritems(): for name, method_description in method_descriptions.iteritems():
qualified_name = _qualified_name(service_name, name)
method_cardinality = method_description.cardinality()
cardinalities[name] = method_description.cardinality() cardinalities[name] = method_description.cardinality()
request_serializers[name] = method_description.serialize_request qualified_names[name] = qualified_name
response_deserializers[name] = method_description.deserialize_response face_cardinalities[qualified_name] = _reexport.common_cardinality(
method_cardinality)
request_serializers[qualified_name] = method_description.serialize_request
response_deserializers[qualified_name] = (
method_description.deserialize_response)
return _EasyInvocationBreakdown( return _EasyInvocationBreakdown(
cardinalities, request_serializers, response_deserializers) cardinalities, qualified_names, face_cardinalities, request_serializers,
response_deserializers)
def break_down_service(method_descriptions): def break_down_service(service_name, method_descriptions):
"""Derives a ServiceBreakdown from several RPC method descriptions. """Derives a ServiceBreakdown from several RPC method descriptions.
Args: Args:
@ -117,37 +140,44 @@ def break_down_service(method_descriptions):
request_deserializers = {} request_deserializers = {}
response_serializers = {} response_serializers = {}
for name, method_description in method_descriptions.iteritems(): for name, method_description in method_descriptions.iteritems():
cardinality = method_description.cardinality() qualified_name = _qualified_name(service_name, name)
if cardinality is interfaces.Cardinality.UNARY_UNARY: method_cardinality = method_description.cardinality()
if method_cardinality is interfaces.Cardinality.UNARY_UNARY:
def service( def service(
request, face_rpc_context, request, face_rpc_context,
service_behavior=method_description.service_unary_unary): service_behavior=method_description.service_unary_unary):
return service_behavior( return service_behavior(
request, _reexport.rpc_context(face_rpc_context)) request, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.unary_unary_inline(service) implementations[qualified_name] = face_utilities.unary_unary_inline(
elif cardinality is interfaces.Cardinality.UNARY_STREAM: service)
elif method_cardinality is interfaces.Cardinality.UNARY_STREAM:
def service( def service(
request, face_rpc_context, request, face_rpc_context,
service_behavior=method_description.service_unary_stream): service_behavior=method_description.service_unary_stream):
return service_behavior( return service_behavior(
request, _reexport.rpc_context(face_rpc_context)) request, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.unary_stream_inline(service) implementations[qualified_name] = face_utilities.unary_stream_inline(
elif cardinality is interfaces.Cardinality.STREAM_UNARY: service)
elif method_cardinality is interfaces.Cardinality.STREAM_UNARY:
def service( def service(
request_iterator, face_rpc_context, request_iterator, face_rpc_context,
service_behavior=method_description.service_stream_unary): service_behavior=method_description.service_stream_unary):
return service_behavior( return service_behavior(
request_iterator, _reexport.rpc_context(face_rpc_context)) request_iterator, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.stream_unary_inline(service) implementations[qualified_name] = face_utilities.stream_unary_inline(
elif cardinality is interfaces.Cardinality.STREAM_STREAM: service)
elif method_cardinality is interfaces.Cardinality.STREAM_STREAM:
def service( def service(
request_iterator, face_rpc_context, request_iterator, face_rpc_context,
service_behavior=method_description.service_stream_stream): service_behavior=method_description.service_stream_stream):
return service_behavior( return service_behavior(
request_iterator, _reexport.rpc_context(face_rpc_context)) request_iterator, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.stream_stream_inline(service) implementations[qualified_name] = face_utilities.stream_stream_inline(
request_deserializers[name] = method_description.deserialize_request service)
response_serializers[name] = method_description.serialize_response request_deserializers[qualified_name] = (
method_description.deserialize_request)
response_serializers[qualified_name] = (
method_description.serialize_response)
return _EasyServiceBreakdown( return _EasyServiceBreakdown(
implementations, request_deserializers, response_serializers) implementations, request_deserializers, response_serializers)

@ -174,6 +174,11 @@ class _StreamUnarySyncAsync(interfaces.StreamUnarySyncAsync):
return _ReexportedFuture(self._underlying.future(request_iterator, timeout)) return _ReexportedFuture(self._underlying.future(request_iterator, timeout))
def common_cardinality(early_adopter_cardinality):
return _EARLY_ADOPTER_CARDINALITY_TO_COMMON_CARDINALITY[
early_adopter_cardinality]
def common_cardinalities(early_adopter_cardinalities): def common_cardinalities(early_adopter_cardinalities):
common_cardinalities = {} common_cardinalities = {}
for name, early_adopter_cardinality in early_adopter_cardinalities.iteritems(): for name, early_adopter_cardinality in early_adopter_cardinalities.iteritems():

@ -146,8 +146,7 @@ class _Stub(interfaces.Stub):
self._rear_link.join_fore_link(self._front) self._rear_link.join_fore_link(self._front)
self._rear_link.start() self._rear_link.start()
self._understub = _face_implementations.dynamic_stub( self._understub = _face_implementations.dynamic_stub(
_reexport.common_cardinalities(self._breakdown.cardinalities), self._breakdown.face_cardinalities, self._front, self._pool, '')
self._front, self._pool, '')
else: else:
raise ValueError('Tried to __enter__ already-__enter__ed Stub!') raise ValueError('Tried to __enter__ already-__enter__ed Stub!')
return self return self
@ -171,17 +170,9 @@ class _Stub(interfaces.Stub):
if self._pool is None: if self._pool is None:
raise ValueError('Tried to __getattr__ non-__enter__ed Stub!') raise ValueError('Tried to __getattr__ non-__enter__ed Stub!')
else: else:
underlying_attr = getattr(self._understub, attr, None)
method_cardinality = self._breakdown.cardinalities.get(attr) method_cardinality = self._breakdown.cardinalities.get(attr)
# TODO(nathaniel): Eliminate this trick. underlying_attr = getattr(
if underlying_attr is None: self._understub, self._breakdown.qualified_names.get(attr), None)
for method_name, method_cardinality in self._breakdown.cardinalities.iteritems():
last_slash_index = method_name.rfind('/')
if 0 <= last_slash_index and method_name[last_slash_index + 1:] == attr:
underlying_attr = getattr(self._understub, method_name)
break
else:
raise AttributeError(attr)
if method_cardinality is interfaces.Cardinality.UNARY_UNARY: if method_cardinality is interfaces.Cardinality.UNARY_UNARY:
return _reexport.unary_unary_sync_async(underlying_attr) return _reexport.unary_unary_sync_async(underlying_attr)
elif method_cardinality is interfaces.Cardinality.UNARY_STREAM: elif method_cardinality is interfaces.Cardinality.UNARY_STREAM:
@ -198,44 +189,49 @@ class _Stub(interfaces.Stub):
def _build_stub( def _build_stub(
methods, host, port, secure, root_certificates, private_key, service_name, methods, host, port, secure, root_certificates, private_key,
certificate_chain, server_host_override=None): certificate_chain, server_host_override=None):
breakdown = _face_utilities.break_down_invocation(methods) breakdown = _face_utilities.break_down_invocation(service_name, methods)
return _Stub( return _Stub(
breakdown, host, port, secure, root_certificates, private_key, breakdown, host, port, secure, root_certificates, private_key,
certificate_chain, server_host_override=server_host_override) certificate_chain, server_host_override=server_host_override)
def _build_server(methods, port, private_key, certificate_chain): def _build_server(service_name, methods, port, private_key, certificate_chain):
breakdown = _face_utilities.break_down_service(methods) breakdown = _face_utilities.break_down_service(service_name, methods)
return _Server(breakdown, port, private_key, certificate_chain) return _Server(breakdown, port, private_key, certificate_chain)
def insecure_stub(methods, host, port): def insecure_stub(service_name, methods, host, port):
"""Constructs an insecure interfaces.Stub. """Constructs an insecure interfaces.Stub.
Args: Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to methods: A dictionary from RPC method name to
interfaces.RpcMethodInvocationDescription describing the RPCs to be interfaces.RpcMethodInvocationDescription describing the RPCs to be
supported by the created stub. supported by the created stub. The RPC method names in the dictionary are
not qualified by the service name or decorated in any other way.
host: The host to which to connect for RPC service. host: The host to which to connect for RPC service.
port: The port to which to connect for RPC service. port: The port to which to connect for RPC service.
Returns: Returns:
An interfaces.Stub affording RPC invocation. An interfaces.Stub affording RPC invocation.
""" """
return _build_stub(methods, host, port, False, None, None, None) return _build_stub(
service_name, methods, host, port, False, None, None, None)
def secure_stub( def secure_stub(
methods, host, port, root_certificates, private_key, certificate_chain, service_name, methods, host, port, root_certificates, private_key,
server_host_override=None): certificate_chain, server_host_override=None):
"""Constructs an insecure interfaces.Stub. """Constructs an insecure interfaces.Stub.
Args: Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to methods: A dictionary from RPC method name to
interfaces.RpcMethodInvocationDescription describing the RPCs to be interfaces.RpcMethodInvocationDescription describing the RPCs to be
supported by the created stub. supported by the created stub. The RPC method names in the dictionary are
not qualified by the service name or decorated in any other way.
host: The host to which to connect for RPC service. host: The host to which to connect for RPC service.
port: The port to which to connect for RPC service. port: The port to which to connect for RPC service.
root_certificates: The PEM-encoded root certificates or None to ask for root_certificates: The PEM-encoded root certificates or None to ask for
@ -251,17 +247,19 @@ def secure_stub(
An interfaces.Stub affording RPC invocation. An interfaces.Stub affording RPC invocation.
""" """
return _build_stub( return _build_stub(
methods, host, port, True, root_certificates, private_key, service_name, methods, host, port, True, root_certificates, private_key,
certificate_chain, server_host_override=server_host_override) certificate_chain, server_host_override=server_host_override)
def insecure_server(methods, port): def insecure_server(service_name, methods, port):
"""Constructs an insecure interfaces.Server. """Constructs an insecure interfaces.Server.
Args: Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to methods: A dictionary from RPC method name to
interfaces.RpcMethodServiceDescription describing the RPCs to interfaces.RpcMethodServiceDescription describing the RPCs to
be serviced by the created server. be serviced by the created server. The RPC method names in the dictionary
are not qualified by the service name or decorated in any other way.
port: The desired port on which to serve or zero to ask for a port to port: The desired port on which to serve or zero to ask for a port to
be automatically selected. be automatically selected.
@ -269,16 +267,18 @@ def insecure_server(methods, port):
An interfaces.Server that will run with no security and An interfaces.Server that will run with no security and
service unsecured raw requests. service unsecured raw requests.
""" """
return _build_server(methods, port, None, None) return _build_server(service_name, methods, port, None, None)
def secure_server(methods, port, private_key, certificate_chain): def secure_server(service_name, methods, port, private_key, certificate_chain):
"""Constructs a secure interfaces.Server. """Constructs a secure interfaces.Server.
Args: Args:
service_name: The package-qualified full name of the service.
methods: A dictionary from RPC method name to methods: A dictionary from RPC method name to
interfaces.RpcMethodServiceDescription describing the RPCs to interfaces.RpcMethodServiceDescription describing the RPCs to
be serviced by the created server. be serviced by the created server. The RPC method names in the dictionary
are not qualified by the service name or decorated in any other way.
port: The port on which to serve or zero to ask for a port to be port: The port on which to serve or zero to ask for a port to be
automatically selected. automatically selected.
private_key: A pem-encoded private key. private_key: A pem-encoded private key.
@ -287,4 +287,5 @@ def secure_server(methods, port, private_key, certificate_chain):
Returns: Returns:
An interfaces.Server that will serve secure traffic. An interfaces.Server that will serve secure traffic.
""" """
return _build_server(methods, port, private_key, certificate_chain) return _build_server(
service_name, methods, port, private_key, certificate_chain)

@ -37,6 +37,8 @@ from grpc.early_adopter import implementations
from grpc.early_adopter import utilities from grpc.early_adopter import utilities
from grpc._junkdrawer import math_pb2 from grpc._junkdrawer import math_pb2
SERVICE_NAME = 'math.Math'
DIV = 'Div' DIV = 'Div'
DIV_MANY = 'DivMany' DIV_MANY = 'DivMany'
FIB = 'Fib' FIB = 'Fib'
@ -104,10 +106,12 @@ _TIMEOUT = 3
class EarlyAdopterImplementationsTest(unittest.TestCase): class EarlyAdopterImplementationsTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.server = implementations.insecure_server(_SERVICE_DESCRIPTIONS, 0) self.server = implementations.insecure_server(
SERVICE_NAME, _SERVICE_DESCRIPTIONS, 0)
self.server.start() self.server.start()
port = self.server.port() port = self.server.port()
self.stub = implementations.insecure_stub(_INVOCATION_DESCRIPTIONS, 'localhost', port) self.stub = implementations.insecure_stub(
SERVICE_NAME, _INVOCATION_DESCRIPTIONS, 'localhost', port)
def tearDown(self): def tearDown(self):
self.server.stop() self.server.stop()

Loading…
Cancel
Save