From 3cdd83bdaca12977b77f1fa57f40dfa1b985dbfc Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Fri, 14 Feb 2020 10:56:40 -0800 Subject: [PATCH] Improve invocation_defects_tests with assertions and comments --- .../tests/unit/_invocation_defects_test.py | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/python/grpcio_tests/tests/unit/_invocation_defects_test.py b/src/python/grpcio_tests/tests/unit/_invocation_defects_test.py index 26a1a2d30d3..20d1124b901 100644 --- a/src/python/grpcio_tests/tests/unit/_invocation_defects_test.py +++ b/src/python/grpcio_tests/tests/unit/_invocation_defects_test.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import itertools -import threading import unittest import logging @@ -35,26 +33,6 @@ _STREAM_STREAM = '/test/StreamStream' _DEFECTIVE_GENERIC_RPC_HANDLER = '/test/DefectiveGenericRpcHandler' -class _Callback(object): - - def __init__(self): - self._condition = threading.Condition() - self._value = None - self._called = False - - def __call__(self, value): - with self._condition: - self._value = value - self._called = True - self._condition.notify_all() - - def value(self): - with self._condition: - while not self._called: - self._condition.wait() - return self._value - - class _Handler(object): def __init__(self, control): @@ -199,6 +177,7 @@ def _defective_handler_multi_callable(channel): class InvocationDefectsTest(unittest.TestCase): + """Tests the handling of exception-raising user code.""" def setUp(self): self._control = test_control.PauseFailControl() @@ -219,12 +198,15 @@ class InvocationDefectsTest(unittest.TestCase): requests = [b'\x07\x08' for _ in range(test_constants.STREAM_LENGTH)] multi_callable = _stream_unary_multi_callable(self._channel) - with self.assertRaises(grpc.RpcError): + with self.assertRaises(grpc.RpcError) as exception_context: response = multi_callable( requests, metadata=(('test', 'IterableStreamRequestBlockingUnaryResponse'),)) + self.assertIs(grpc.StatusCode.UNKNOWN, + exception_context.exception.code()) + def testIterableStreamRequestFutureUnaryResponse(self): requests = [b'\x07\x08' for _ in range(test_constants.STREAM_LENGTH)] multi_callable = _stream_unary_multi_callable(self._channel) @@ -232,8 +214,11 @@ class InvocationDefectsTest(unittest.TestCase): requests, metadata=(('test', 'IterableStreamRequestFutureUnaryResponse'),)) - with self.assertRaises(grpc.RpcError): - response = response_future.result() + with self.assertRaises(grpc.RpcError) as exception_context: + response_future.result() + + self.assertIs(grpc.StatusCode.UNKNOWN, + exception_context.exception.code()) def testIterableStreamRequestStreamResponse(self): requests = [b'\x77\x58' for _ in range(test_constants.STREAM_LENGTH)] @@ -242,9 +227,12 @@ class InvocationDefectsTest(unittest.TestCase): requests, metadata=(('test', 'IterableStreamRequestStreamResponse'),)) - with self.assertRaises(grpc.RpcError): + with self.assertRaises(grpc.RpcError) as exception_context: next(response_iterator) + self.assertIs(grpc.StatusCode.UNKNOWN, + exception_context.exception.code()) + def testIteratorStreamRequestStreamResponse(self): requests_iterator = FailAfterFewIterationsCounter( test_constants.STREAM_LENGTH // 2, b'\x07\x08') @@ -253,18 +241,21 @@ class InvocationDefectsTest(unittest.TestCase): requests_iterator, metadata=(('test', 'IteratorStreamRequestStreamResponse'),)) - with self.assertRaises(grpc.RpcError): + with self.assertRaises(grpc.RpcError) as exception_context: for _ in range(test_constants.STREAM_LENGTH // 2 + 1): next(response_iterator) + self.assertIs(grpc.StatusCode.UNKNOWN, + exception_context.exception.code()) + def testDefectiveGenericRpcHandlerUnaryResponse(self): request = b'\x07\x08' multi_callable = _defective_handler_multi_callable(self._channel) with self.assertRaises(grpc.RpcError) as exception_context: - response = multi_callable( - request, - metadata=(('test', 'DefectiveGenericRpcHandlerUnary'),)) + multi_callable(request, + metadata=(('test', + 'DefectiveGenericRpcHandlerUnary'),)) self.assertIs(grpc.StatusCode.UNKNOWN, exception_context.exception.code())