Merge pull request #22026 from lidizheng/defects-test

Improve invocation_defects_tests with assertions and comments
pull/22066/head
Lidi Zheng 5 years ago committed by GitHub
commit c65bad469b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 59
      src/python/grpcio_tests/tests/unit/_invocation_defects_test.py

@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import itertools
import threading
import unittest import unittest
import logging import logging
@ -35,26 +33,6 @@ _STREAM_STREAM = '/test/StreamStream'
_DEFECTIVE_GENERIC_RPC_HANDLER = '/test/DefectiveGenericRpcHandler' _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): class _Handler(object):
def __init__(self, control): def __init__(self, control):
@ -199,6 +177,7 @@ def _defective_handler_multi_callable(channel):
class InvocationDefectsTest(unittest.TestCase): class InvocationDefectsTest(unittest.TestCase):
"""Tests the handling of exception-raising user code on the client-side."""
def setUp(self): def setUp(self):
self._control = test_control.PauseFailControl() self._control = test_control.PauseFailControl()
@ -216,35 +195,44 @@ class InvocationDefectsTest(unittest.TestCase):
self._channel.close() self._channel.close()
def testIterableStreamRequestBlockingUnaryResponse(self): def testIterableStreamRequestBlockingUnaryResponse(self):
requests = [b'\x07\x08' for _ in range(test_constants.STREAM_LENGTH)] requests = object()
multi_callable = _stream_unary_multi_callable(self._channel) multi_callable = _stream_unary_multi_callable(self._channel)
with self.assertRaises(grpc.RpcError): with self.assertRaises(grpc.RpcError) as exception_context:
response = multi_callable( multi_callable(
requests, requests,
metadata=(('test', metadata=(('test',
'IterableStreamRequestBlockingUnaryResponse'),)) 'IterableStreamRequestBlockingUnaryResponse'),))
self.assertIs(grpc.StatusCode.UNKNOWN,
exception_context.exception.code())
def testIterableStreamRequestFutureUnaryResponse(self): def testIterableStreamRequestFutureUnaryResponse(self):
requests = [b'\x07\x08' for _ in range(test_constants.STREAM_LENGTH)] requests = object()
multi_callable = _stream_unary_multi_callable(self._channel) multi_callable = _stream_unary_multi_callable(self._channel)
response_future = multi_callable.future( response_future = multi_callable.future(
requests, requests,
metadata=(('test', 'IterableStreamRequestFutureUnaryResponse'),)) metadata=(('test', 'IterableStreamRequestFutureUnaryResponse'),))
with self.assertRaises(grpc.RpcError): with self.assertRaises(grpc.RpcError) as exception_context:
response = response_future.result() response_future.result()
self.assertIs(grpc.StatusCode.UNKNOWN,
exception_context.exception.code())
def testIterableStreamRequestStreamResponse(self): def testIterableStreamRequestStreamResponse(self):
requests = [b'\x77\x58' for _ in range(test_constants.STREAM_LENGTH)] requests = object()
multi_callable = _stream_stream_multi_callable(self._channel) multi_callable = _stream_stream_multi_callable(self._channel)
response_iterator = multi_callable( response_iterator = multi_callable(
requests, requests,
metadata=(('test', 'IterableStreamRequestStreamResponse'),)) metadata=(('test', 'IterableStreamRequestStreamResponse'),))
with self.assertRaises(grpc.RpcError): with self.assertRaises(grpc.RpcError) as exception_context:
next(response_iterator) next(response_iterator)
self.assertIs(grpc.StatusCode.UNKNOWN,
exception_context.exception.code())
def testIteratorStreamRequestStreamResponse(self): def testIteratorStreamRequestStreamResponse(self):
requests_iterator = FailAfterFewIterationsCounter( requests_iterator = FailAfterFewIterationsCounter(
test_constants.STREAM_LENGTH // 2, b'\x07\x08') test_constants.STREAM_LENGTH // 2, b'\x07\x08')
@ -253,18 +241,21 @@ class InvocationDefectsTest(unittest.TestCase):
requests_iterator, requests_iterator,
metadata=(('test', 'IteratorStreamRequestStreamResponse'),)) 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): for _ in range(test_constants.STREAM_LENGTH // 2 + 1):
next(response_iterator) next(response_iterator)
self.assertIs(grpc.StatusCode.UNKNOWN,
exception_context.exception.code())
def testDefectiveGenericRpcHandlerUnaryResponse(self): def testDefectiveGenericRpcHandlerUnaryResponse(self):
request = b'\x07\x08' request = b'\x07\x08'
multi_callable = _defective_handler_multi_callable(self._channel) multi_callable = _defective_handler_multi_callable(self._channel)
with self.assertRaises(grpc.RpcError) as exception_context: with self.assertRaises(grpc.RpcError) as exception_context:
response = multi_callable( multi_callable(request,
request, metadata=(('test',
metadata=(('test', 'DefectiveGenericRpcHandlerUnary'),)) 'DefectiveGenericRpcHandlerUnary'),))
self.assertIs(grpc.StatusCode.UNKNOWN, self.assertIs(grpc.StatusCode.UNKNOWN,
exception_context.exception.code()) exception_context.exception.code())

Loading…
Cancel
Save