Make handlers optional at server construction

pull/7148/head
Nathaniel Manista 9 years ago
parent 5220b0c89d
commit f5f9e04464
  1. 16
      src/python/grpcio/grpc/__init__.py
  2. 2
      src/python/grpcio/grpc/_server.py
  3. 3
      src/python/grpcio/grpc/beta/_server_adaptations.py
  4. 4
      src/python/grpcio/tests/protoc_plugin/_python_plugin_test.py
  5. 4
      src/python/grpcio/tests/unit/_channel_connectivity_test.py
  6. 2
      src/python/grpcio/tests/unit/_channel_ready_future_test.py
  7. 3
      src/python/grpcio/tests/unit/_empty_message_test.py
  8. 2
      src/python/grpcio/tests/unit/_metadata_code_details_test.py
  9. 4
      src/python/grpcio/tests/unit/_metadata_test.py
  10. 2
      src/python/grpcio/tests/unit/_rpc_test.py

@ -1207,25 +1207,23 @@ def secure_channel(target, credentials, options=None):
return _channel.Channel(target, options, credentials._credentials)
def server(generic_rpc_handlers, thread_pool, options=None):
def server(thread_pool, handlers=None):
"""Creates a Server with which RPCs can be serviced.
The GenericRpcHandlers passed to this function needn't be the only
GenericRpcHandlers that will be used to serve RPCs; others may be added later
by calling add_generic_rpc_handlers any time before the returned server is
started.
Args:
generic_rpc_handlers: Some number of GenericRpcHandlers that will be used
to service RPCs after the returned Server is started.
thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
to service RPCs.
handlers: An optional sequence of GenericRpcHandlers to be used to service
RPCs after the returned Server is started. These andlers need not be the
only handlers the server will use to service RPCs; other handlers may
later be added by calling add_generic_rpc_handlers any time before the
returned Server is started.
Returns:
A Server with which RPCs can be serviced.
"""
from grpc import _server
return _server.Server(generic_rpc_handlers, thread_pool)
return _server.Server(thread_pool, () if handlers is None else handlers)
################################### __all__ #################################

@ -729,7 +729,7 @@ def _start(state):
class Server(grpc.Server):
def __init__(self, generic_handlers, thread_pool):
def __init__(self, thread_pool, generic_handlers):
completion_queue = cygrpc.CompletionQueue()
server = cygrpc.Server()
server.register_completion_queue(completion_queue)

@ -364,4 +364,5 @@ def server(
_DEFAULT_POOL_SIZE if thread_pool_size is None else thread_pool_size)
else:
effective_thread_pool = thread_pool
return _Server(grpc.server((generic_rpc_handler,), effective_thread_pool))
return _Server(
grpc.server(effective_thread_pool, handlers=(generic_rpc_handler,)))

@ -173,7 +173,7 @@ def _CreateService(service_pb2, response_pb2, payload_pb2):
return servicer_methods.HalfDuplexCall(request_iter, context)
server = grpc.server(
(), futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
getattr(service_pb2, ADD_SERVICER_TO_SERVER_IDENTIFIER)(Servicer(), server)
port = server.add_insecure_port('[::]:0')
server.start()
@ -197,7 +197,7 @@ def _CreateIncompleteService(service_pb2):
pass
server = grpc.server(
(), futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
getattr(service_pb2, ADD_SERVICER_TO_SERVER_IDENTIFIER)(Servicer(), server)
port = server.add_insecure_port('[::]:0')
server.start()

@ -104,7 +104,7 @@ class ChannelConnectivityTest(unittest.TestCase):
grpc.ChannelConnectivity.READY, fifth_connectivities)
def test_immediately_connectable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
first_callback = _Callback()
@ -143,7 +143,7 @@ class ChannelConnectivityTest(unittest.TestCase):
grpc.ChannelConnectivity.SHUTDOWN, fourth_connectivities)
def test_reachable_then_unreachable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
callback = _Callback()

@ -78,7 +78,7 @@ class ChannelReadyFutureTest(unittest.TestCase):
self.assertFalse(ready_future.running())
def test_immediately_connectable_channel_connectivity(self):
server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
port = server.add_insecure_port('[::]:0')
server.start()
channel = grpc.insecure_channel('localhost:{}'.format(port))

@ -103,7 +103,8 @@ class EmptyMessageTest(unittest.TestCase):
def setUp(self):
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((_GenericHandler(),), self._server_pool)
self._server = grpc.server(
self._server_pool, handlers=(_GenericHandler(),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()
self._channel = grpc.insecure_channel('localhost:%d' % port)

@ -189,7 +189,7 @@ class MetadataCodeDetailsTest(unittest.TestCase):
self._servicer = _Servicer()
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server(
(_generic_handler(self._servicer),), self._server_pool)
self._server_pool, handlers=(_generic_handler(self._servicer),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()

@ -161,8 +161,8 @@ class MetadataTest(unittest.TestCase):
def setUp(self):
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((_GenericHandler(weakref.proxy(self)),),
self._server_pool)
self._server = grpc.server(
self._server_pool, handlers=(_GenericHandler(weakref.proxy(self)),))
port = self._server.add_insecure_port('[::]:0')
self._server.start()
self._channel = grpc.insecure_channel('localhost:%d' % port,

@ -184,7 +184,7 @@ class RPCTest(unittest.TestCase):
self._handler = _Handler(self._control)
self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
self._server = grpc.server((), self._server_pool)
self._server = grpc.server(self._server_pool)
port = self._server.add_insecure_port(b'[::]:0')
self._server.add_generic_rpc_handlers((_GenericHandler(self._handler),))
self._server.start()

Loading…
Cancel
Save