Skip to content

Commit

Permalink
yapf-ize grpc_testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ncteisen committed Dec 12, 2017
1 parent 0f6c518 commit e2097f0
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 106 deletions.
16 changes: 8 additions & 8 deletions src/python/grpcio_testing/grpc_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ class Server(six.with_metaclass(abc.ABCMeta)):
"""A server with which to test a system that services RPCs."""

@abc.abstractmethod
def invoke_unary_unary(
self, method_descriptor, invocation_metadata, request, timeout):
def invoke_unary_unary(self, method_descriptor, invocation_metadata,
request, timeout):
"""Invokes an RPC to be serviced by the system under test.
Args:
Expand All @@ -513,8 +513,8 @@ def invoke_unary_unary(
raise NotImplementedError()

@abc.abstractmethod
def invoke_unary_stream(
self, method_descriptor, invocation_metadata, request, timeout):
def invoke_unary_stream(self, method_descriptor, invocation_metadata,
request, timeout):
"""Invokes an RPC to be serviced by the system under test.
Args:
Expand All @@ -531,8 +531,8 @@ def invoke_unary_stream(
raise NotImplementedError()

@abc.abstractmethod
def invoke_stream_unary(
self, method_descriptor, invocation_metadata, timeout):
def invoke_stream_unary(self, method_descriptor, invocation_metadata,
timeout):
"""Invokes an RPC to be serviced by the system under test.
Args:
Expand All @@ -548,8 +548,8 @@ def invoke_stream_unary(
raise NotImplementedError()

@abc.abstractmethod
def invoke_stream_stream(
self, method_descriptor, invocation_metadata, timeout):
def invoke_stream_stream(self, method_descriptor, invocation_metadata,
timeout):
"""Invokes an RPC to be serviced by the system under test.
Args:
Expand Down
2 changes: 2 additions & 0 deletions src/python/grpcio_testing/grpc_testing/_channel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# pylint: disable=unused-argument
def testing_channel(descriptors, time):
return _channel.TestingChannel(time, _channel_state.State())


# pylint: enable=unused-argument
26 changes: 18 additions & 8 deletions src/python/grpcio_testing/grpc_testing/_channel/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ def subscribe(self, callback, try_to_connect=False):
def unsubscribe(self, callback):
raise NotImplementedError()

def unary_unary(
self, method, request_serializer=None, response_deserializer=None):
def unary_unary(self,
method,
request_serializer=None,
response_deserializer=None):
return _multi_callable.UnaryUnary(method, self._state)

def unary_stream(
self, method, request_serializer=None, response_deserializer=None):
def unary_stream(self,
method,
request_serializer=None,
response_deserializer=None):
return _multi_callable.UnaryStream(method, self._state)

def stream_unary(
self, method, request_serializer=None, response_deserializer=None):
def stream_unary(self,
method,
request_serializer=None,
response_deserializer=None):
return _multi_callable.StreamUnary(method, self._state)

def stream_stream(
self, method, request_serializer=None, response_deserializer=None):
def stream_stream(self,
method,
request_serializer=None,
response_deserializer=None):
return _multi_callable.StreamStream(method, self._state)

def take_unary_unary(self, method_descriptor):
Expand All @@ -59,4 +67,6 @@ def take_stream_unary(self, method_descriptor):

def take_stream_stream(self, method_descriptor):
return _channel_rpc.stream_stream(self._state, method_descriptor)


# pylint: enable=unused-argument
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def cancelled(self):
self._rpc_state.cancelled()

def terminate(self, response, trailing_metadata, code, details):
self._rpc_state.terminate_with_response(
response, trailing_metadata, code, details)
self._rpc_state.terminate_with_response(response, trailing_metadata,
code, details)


class _UnaryStream(grpc_testing.UnaryStreamChannelRpc):
Expand Down Expand Up @@ -67,8 +67,8 @@ def cancelled(self):
self._rpc_state.cancelled()

def terminate(self, response, trailing_metadata, code, details):
self._rpc_state.terminate_with_response(
response, trailing_metadata, code, details)
self._rpc_state.terminate_with_response(response, trailing_metadata,
code, details)


class _StreamStream(grpc_testing.StreamStreamChannelRpc):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def __init__(self):
self._condition = threading.Condition()
self._rpc_states = collections.defaultdict(list)

def invoke_rpc(
self, method_full_rpc_name, invocation_metadata, requests,
requests_closed, timeout):
rpc_state = _rpc_state.State(
invocation_metadata, requests, requests_closed)
def invoke_rpc(self, method_full_rpc_name, invocation_metadata, requests,
requests_closed, timeout):
rpc_state = _rpc_state.State(invocation_metadata, requests,
requests_closed)
with self._condition:
self._rpc_states[method_full_rpc_name].append(rpc_state)
self._condition.notify_all()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from grpc_testing import _common
from grpc_testing._channel import _invocation


# All per-call credentials parameters are unused by this test infrastructure.
# pylint: disable=unused-argument
class UnaryUnary(grpc.UnaryUnaryMultiCallable):
Expand All @@ -26,20 +27,20 @@ def __init__(self, method_full_rpc_name, channel_handler):

def __call__(self, request, timeout=None, metadata=None, credentials=None):
rpc_handler = self._channel_handler.invoke_rpc(
self._method_full_rpc_name, _common.fuss_with_metadata(metadata),
[request], True, timeout)
self._method_full_rpc_name,
_common.fuss_with_metadata(metadata), [request], True, timeout)
return _invocation.blocking_unary_response(rpc_handler)

def with_call(self, request, timeout=None, metadata=None, credentials=None):
rpc_handler = self._channel_handler.invoke_rpc(
self._method_full_rpc_name, _common.fuss_with_metadata(metadata),
[request], True, timeout)
self._method_full_rpc_name,
_common.fuss_with_metadata(metadata), [request], True, timeout)
return _invocation.blocking_unary_response_with_call(rpc_handler)

def future(self, request, timeout=None, metadata=None, credentials=None):
rpc_handler = self._channel_handler.invoke_rpc(
self._method_full_rpc_name, _common.fuss_with_metadata(metadata),
[request], True, timeout)
self._method_full_rpc_name,
_common.fuss_with_metadata(metadata), [request], True, timeout)
return _invocation.future_call(rpc_handler)


Expand Down Expand Up @@ -112,4 +113,6 @@ def __call__(self,
_common.fuss_with_metadata(metadata), [], False, timeout)
_invocation.consume_requests(request_iterator, rpc_handler)
return _invocation.ResponseIteratorCall(rpc_handler)


# pylint: enable=unused-argument
21 changes: 10 additions & 11 deletions src/python/grpcio_testing/grpc_testing/_channel/_rpc_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,22 @@ def take_response(self):
if self._code is grpc.StatusCode.OK:
if self._responses:
response = self._responses.pop(0)
return _common.ChannelRpcRead(
response, None, None, None)
return _common.ChannelRpcRead(response, None, None,
None)
else:
return _common.ChannelRpcRead(
None, self._trailing_metadata,
grpc.StatusCode.OK, self._details)
None, self._trailing_metadata, grpc.StatusCode.OK,
self._details)
elif self._code is None:
if self._responses:
response = self._responses.pop(0)
return _common.ChannelRpcRead(
response, None, None, None)
return _common.ChannelRpcRead(response, None, None,
None)
else:
self._condition.wait()
else:
return _common.ChannelRpcRead(
None, self._trailing_metadata, self._code,
self._details)
return _common.ChannelRpcRead(None, self._trailing_metadata,
self._code, self._details)

def termination(self):
with self._condition:
Expand Down Expand Up @@ -150,8 +149,8 @@ def send_response(self, response):
self._responses.append(response)
self._condition.notify_all()

def terminate_with_response(
self, response, trailing_metadata, code, details):
def terminate_with_response(self, response, trailing_metadata, code,
details):
with self._condition:
if self._initial_metadata is None:
self._initial_metadata = _common.FUSSED_EMPTY_METADATA
Expand Down
35 changes: 15 additions & 20 deletions src/python/grpcio_testing/grpc_testing/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@

def _fuss(tuplified_metadata):
return tuplified_metadata + (
(
'grpc.metadata_added_by_runtime',
'gRPC is allowed to add metadata in transmission and does so.',
),
)
('grpc.metadata_added_by_runtime',
'gRPC is allowed to add metadata in transmission and does so.',),)


FUSSED_EMPTY_METADATA = _fuss(())

Expand All @@ -41,8 +39,8 @@ def rpc_names(service_descriptors):
rpc_names_to_descriptors = {}
for service_descriptor in service_descriptors:
for method_descriptor in service_descriptor.methods_by_name.values():
rpc_name = '/{}/{}'.format(
service_descriptor.full_name, method_descriptor.name)
rpc_name = '/{}/{}'.format(service_descriptor.full_name,
method_descriptor.name)
rpc_names_to_descriptors[rpc_name] = method_descriptor
return rpc_names_to_descriptors

Expand Down Expand Up @@ -96,9 +94,8 @@ def add_callback(self, callback):
class ChannelHandler(six.with_metaclass(abc.ABCMeta)):

@abc.abstractmethod
def invoke_rpc(
self, method_full_rpc_name, invocation_metadata, requests,
requests_closed, timeout):
def invoke_rpc(self, method_full_rpc_name, invocation_metadata, requests,
requests_closed, timeout):
raise NotImplementedError()


Expand Down Expand Up @@ -138,23 +135,21 @@ def add_termination_callback(self, callback):
class Serverish(six.with_metaclass(abc.ABCMeta)):

@abc.abstractmethod
def invoke_unary_unary(
self, method_descriptor, handler, invocation_metadata, request,
deadline):
def invoke_unary_unary(self, method_descriptor, handler,
invocation_metadata, request, deadline):
raise NotImplementedError()

@abc.abstractmethod
def invoke_unary_stream(
self, method_descriptor, handler, invocation_metadata, request,
deadline):
def invoke_unary_stream(self, method_descriptor, handler,
invocation_metadata, request, deadline):
raise NotImplementedError()

@abc.abstractmethod
def invoke_stream_unary(
self, method_descriptor, handler, invocation_metadata, deadline):
def invoke_stream_unary(self, method_descriptor, handler,
invocation_metadata, deadline):
raise NotImplementedError()

@abc.abstractmethod
def invoke_stream_stream(
self, method_descriptor, handler, invocation_metadata, deadline):
def invoke_stream_stream(self, method_descriptor, handler,
invocation_metadata, deadline):
raise NotImplementedError()
4 changes: 2 additions & 2 deletions src/python/grpcio_testing/grpc_testing/_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@


def server_from_dictionary(descriptors_to_servicers, time):
return _server.server_from_descriptor_to_servicers(
descriptors_to_servicers, time)
return _server.server_from_descriptor_to_servicers(descriptors_to_servicers,
time)
6 changes: 2 additions & 4 deletions src/python/grpcio_testing/grpc_testing/_server/_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ def unary_response_termination(self):
if self._unary_response is None:
if self._responses:
self._unary_response = self._responses.pop(0)
return (
self._unary_response, self._trailing_metadata,
self._code, self._details,)

return (self._unary_response, self._trailing_metadata,
self._code, self._details,)

def stream_response_termination(self):
with self._condition:
Expand Down
5 changes: 2 additions & 3 deletions src/python/grpcio_testing/grpc_testing/_server/_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ def add_rpc_error(self, rpc_error):

def application_cancel(self):
with self._condition:
self._abort(
grpc.StatusCode.CANCELLED,
'Cancelled by server-side application!')
self._abort(grpc.StatusCode.CANCELLED,
'Cancelled by server-side application!')

def application_exception_abort(self, exception):
with self._condition:
Expand Down
Loading

0 comments on commit e2097f0

Please sign in to comment.