Split the seen_metadata function & assign tuple() as default value

pull/21647/head
Lidi Zheng 5 years ago
parent 181437bbd8
commit f912ddf7d4
  1. 2
      src/python/grpcio/grpc/experimental/aio/_call.py
  2. 3
      src/python/grpcio/grpc/experimental/aio/_channel.py
  3. 3
      src/python/grpcio/grpc/experimental/aio/_interceptor.py
  4. 3
      src/python/grpcio/grpc/experimental/aio/_typing.py
  5. 16
      src/python/grpcio_tests/tests_aio/unit/_common.py
  6. 15
      src/python/grpcio_tests/tests_aio/unit/interceptor_test.py

@ -280,7 +280,7 @@ class UnaryUnaryCall(Call, _base_call.UnaryUnaryCall):
# pylint: disable=too-many-arguments
def __init__(self, request: RequestType, deadline: Optional[float],
metadata: Optional[MetadataType],
metadata: MetadataType,
credentials: Optional[grpc.CallCredentials],
channel: cygrpc.AioChannel, method: bytes,
request_serializer: SerializingFunction,

@ -101,6 +101,9 @@ class UnaryUnaryMultiCallable(_BaseMultiCallable):
if compression:
raise NotImplementedError("TODO: compression not implemented yet")
if metadata is None:
metadata = tuple()
if not self._interceptors:
return UnaryUnaryCall(
request,

@ -106,8 +106,7 @@ class InterceptedUnaryUnaryCall(_base_call.UnaryUnaryCall):
def __init__( # pylint: disable=R0913
self, interceptors: Sequence[UnaryUnaryClientInterceptor],
request: RequestType, timeout: Optional[float],
metadata: Optional[MetadataType],
credentials: Optional[grpc.CallCredentials],
metadata: MetadataType, credentials: Optional[grpc.CallCredentials],
channel: cygrpc.AioChannel, method: bytes,
request_serializer: SerializingFunction,
response_deserializer: DeserializingFunction) -> None:

@ -20,6 +20,7 @@ RequestType = TypeVar('RequestType')
ResponseType = TypeVar('ResponseType')
SerializingFunction = Callable[[Any], bytes]
DeserializingFunction = Callable[[bytes], Any]
MetadataType = Sequence[Tuple[Text, AnyStr]]
MetadatumType = Tuple[Text, AnyStr]
MetadataType = Sequence[MetadatumType]
ChannelArgumentType = Sequence[Tuple[Text, Any]]
EOFType = type(EOF)

@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from grpc.experimental.aio._typing import MetadataType, MetadatumType
def seen_metadata(expected, actual):
def seen_metadata(expected: MetadataType, actual: MetadataType):
return bool(set(expected) - set(actual))
def seen_metadatum(expected: MetadatumType, actual: MetadataType):
metadata_dict = dict(actual)
if type(expected[0]) != tuple:
return metadata_dict.get(expected[0]) == expected[1]
else:
for metadatum in expected:
if metadata_dict.get(metadatum[0]) != metadatum[1]:
return False
return True
return metadata_dict.get(expected[0]) == expected[1]

@ -546,14 +546,11 @@ class TestInterceptedUnaryUnaryCall(AioTestBase):
async def intercept_unary_unary(self, continuation,
client_call_details, request):
if client_call_details.metadata is not None:
new_metadata = client_call_details.metadata + _INITIAL_METADATA_TO_INJECT
else:
new_metadata = _INITIAL_METADATA_TO_INJECT
new_details = aio.ClientCallDetails(
method=client_call_details.method,
timeout=client_call_details.timeout,
metadata=new_metadata,
metadata=client_call_details.metadata +
_INITIAL_METADATA_TO_INJECT,
credentials=client_call_details.credentials,
)
return await continuation(new_details, request)
@ -566,13 +563,13 @@ class TestInterceptedUnaryUnaryCall(AioTestBase):
# Expected to see the echoed initial metadata
self.assertTrue(
_common.seen_metadata(_INITIAL_METADATA_TO_INJECT[0], await
call.initial_metadata()))
_common.seen_metadatum(_INITIAL_METADATA_TO_INJECT[0], await
call.initial_metadata()))
# Expected to see the echoed trailing metadata
self.assertTrue(
_common.seen_metadata(_INITIAL_METADATA_TO_INJECT[1], await
call.trailing_metadata()))
_common.seen_metadatum(_INITIAL_METADATA_TO_INJECT[1], await
call.trailing_metadata()))
self.assertEqual(await call.code(), grpc.StatusCode.OK)

Loading…
Cancel
Save