|
|
@ -47,338 +47,338 @@ class _MulticallableTestMixin(): |
|
|
|
await self._server.stop(None) |
|
|
|
await self._server.stop(None) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUnaryUnaryCall(_MulticallableTestMixin, AioTestBase): |
|
|
|
# class TestUnaryUnaryCall(_MulticallableTestMixin, AioTestBase): |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_to_string(self): |
|
|
|
# async def test_call_to_string(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(str(call) is not None) |
|
|
|
# self.assertTrue(str(call) is not None) |
|
|
|
self.assertTrue(repr(call) is not None) |
|
|
|
# self.assertTrue(repr(call) is not None) |
|
|
|
|
|
|
|
|
|
|
|
response = await call |
|
|
|
# response = await call |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(str(call) is not None) |
|
|
|
# self.assertTrue(str(call) is not None) |
|
|
|
self.assertTrue(repr(call) is not None) |
|
|
|
# self.assertTrue(repr(call) is not None) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_ok(self): |
|
|
|
# async def test_call_ok(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(call.done()) |
|
|
|
# self.assertFalse(call.done()) |
|
|
|
|
|
|
|
|
|
|
|
response = await call |
|
|
|
# response = await call |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.done()) |
|
|
|
# self.assertTrue(call.done()) |
|
|
|
self.assertIsInstance(response, messages_pb2.SimpleResponse) |
|
|
|
# self.assertIsInstance(response, messages_pb2.SimpleResponse) |
|
|
|
self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
# self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
|
|
|
|
|
|
|
|
# Response is cached at call object level, reentrance |
|
|
|
# # Response is cached at call object level, reentrance |
|
|
|
# returns again the same response |
|
|
|
# # returns again the same response |
|
|
|
response_retry = await call |
|
|
|
# response_retry = await call |
|
|
|
self.assertIs(response, response_retry) |
|
|
|
# self.assertIs(response, response_retry) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_rpc_error(self): |
|
|
|
# async def test_call_rpc_error(self): |
|
|
|
async with aio.insecure_channel(_UNREACHABLE_TARGET) as channel: |
|
|
|
# async with aio.insecure_channel(_UNREACHABLE_TARGET) as channel: |
|
|
|
stub = test_pb2_grpc.TestServiceStub(channel) |
|
|
|
# stub = test_pb2_grpc.TestServiceStub(channel) |
|
|
|
|
|
|
|
|
|
|
|
call = stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
# with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
await call |
|
|
|
# await call |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, |
|
|
|
# self.assertEqual(grpc.StatusCode.UNAVAILABLE, |
|
|
|
exception_context.exception.code()) |
|
|
|
# exception_context.exception.code()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.done()) |
|
|
|
# self.assertTrue(call.done()) |
|
|
|
self.assertEqual(grpc.StatusCode.UNAVAILABLE, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.UNAVAILABLE, await call.code()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_code_awaitable(self): |
|
|
|
# async def test_call_code_awaitable(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
# self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_details_awaitable(self): |
|
|
|
# async def test_call_details_awaitable(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
self.assertEqual('', await call.details()) |
|
|
|
# self.assertEqual('', await call.details()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_initial_metadata_awaitable(self): |
|
|
|
# async def test_call_initial_metadata_awaitable(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
self.assertEqual((), await call.initial_metadata()) |
|
|
|
# self.assertEqual((), await call.initial_metadata()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_trailing_metadata_awaitable(self): |
|
|
|
# async def test_call_trailing_metadata_awaitable(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
self.assertEqual((), await call.trailing_metadata()) |
|
|
|
# self.assertEqual((), await call.trailing_metadata()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_initial_metadata_cancelable(self): |
|
|
|
# async def test_call_initial_metadata_cancelable(self): |
|
|
|
coro_started = asyncio.Event() |
|
|
|
# coro_started = asyncio.Event() |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
async def coro(): |
|
|
|
# async def coro(): |
|
|
|
coro_started.set() |
|
|
|
# coro_started.set() |
|
|
|
await call.initial_metadata() |
|
|
|
# await call.initial_metadata() |
|
|
|
|
|
|
|
|
|
|
|
task = self.loop.create_task(coro()) |
|
|
|
# task = self.loop.create_task(coro()) |
|
|
|
await coro_started.wait() |
|
|
|
# await coro_started.wait() |
|
|
|
task.cancel() |
|
|
|
# task.cancel() |
|
|
|
|
|
|
|
|
|
|
|
# Test that initial metadata can still be asked thought |
|
|
|
# # Test that initial metadata can still be asked thought |
|
|
|
# a cancellation happened with the previous task |
|
|
|
# # a cancellation happened with the previous task |
|
|
|
self.assertEqual((), await call.initial_metadata()) |
|
|
|
# self.assertEqual((), await call.initial_metadata()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_initial_metadata_multiple_waiters(self): |
|
|
|
# async def test_call_initial_metadata_multiple_waiters(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
async def coro(): |
|
|
|
# async def coro(): |
|
|
|
return await call.initial_metadata() |
|
|
|
# return await call.initial_metadata() |
|
|
|
|
|
|
|
|
|
|
|
task1 = self.loop.create_task(coro()) |
|
|
|
# task1 = self.loop.create_task(coro()) |
|
|
|
task2 = self.loop.create_task(coro()) |
|
|
|
# task2 = self.loop.create_task(coro()) |
|
|
|
|
|
|
|
|
|
|
|
await call |
|
|
|
# await call |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual([(), ()], await asyncio.gather(*[task1, task2])) |
|
|
|
# self.assertEqual([(), ()], await asyncio.gather(*[task1, task2])) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_code_cancelable(self): |
|
|
|
# async def test_call_code_cancelable(self): |
|
|
|
coro_started = asyncio.Event() |
|
|
|
# coro_started = asyncio.Event() |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
async def coro(): |
|
|
|
# async def coro(): |
|
|
|
coro_started.set() |
|
|
|
# coro_started.set() |
|
|
|
await call.code() |
|
|
|
# await call.code() |
|
|
|
|
|
|
|
|
|
|
|
task = self.loop.create_task(coro()) |
|
|
|
# task = self.loop.create_task(coro()) |
|
|
|
await coro_started.wait() |
|
|
|
# await coro_started.wait() |
|
|
|
task.cancel() |
|
|
|
# task.cancel() |
|
|
|
|
|
|
|
|
|
|
|
# Test that code can still be asked thought |
|
|
|
# # Test that code can still be asked thought |
|
|
|
# a cancellation happened with the previous task |
|
|
|
# # a cancellation happened with the previous task |
|
|
|
self.assertEqual(grpc.StatusCode.OK, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.OK, await call.code()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_call_code_multiple_waiters(self): |
|
|
|
# async def test_call_code_multiple_waiters(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
async def coro(): |
|
|
|
# async def coro(): |
|
|
|
return await call.code() |
|
|
|
# return await call.code() |
|
|
|
|
|
|
|
|
|
|
|
task1 = self.loop.create_task(coro()) |
|
|
|
# task1 = self.loop.create_task(coro()) |
|
|
|
task2 = self.loop.create_task(coro()) |
|
|
|
# task2 = self.loop.create_task(coro()) |
|
|
|
|
|
|
|
|
|
|
|
await call |
|
|
|
# await call |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual([grpc.StatusCode.OK, grpc.StatusCode.OK], await |
|
|
|
# self.assertEqual([grpc.StatusCode.OK, grpc.StatusCode.OK], await |
|
|
|
asyncio.gather(task1, task2)) |
|
|
|
# asyncio.gather(task1, task2)) |
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel_unary_unary(self): |
|
|
|
# async def test_cancel_unary_unary(self): |
|
|
|
call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.UnaryCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(call.cancelled()) |
|
|
|
# self.assertFalse(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.cancel()) |
|
|
|
# self.assertTrue(call.cancel()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await call |
|
|
|
# await call |
|
|
|
|
|
|
|
|
|
|
|
# The info in the RpcError should match the info in Call object. |
|
|
|
# # The info in the RpcError should match the info in Call object. |
|
|
|
self.assertTrue(call.cancelled()) |
|
|
|
# self.assertTrue(call.cancelled()) |
|
|
|
self.assertEqual(await call.code(), grpc.StatusCode.CANCELLED) |
|
|
|
# self.assertEqual(await call.code(), grpc.StatusCode.CANCELLED) |
|
|
|
self.assertEqual(await call.details(), |
|
|
|
# self.assertEqual(await call.details(), |
|
|
|
'Locally cancelled by application!') |
|
|
|
# 'Locally cancelled by application!') |
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel_unary_unary_in_task(self): |
|
|
|
# async def test_cancel_unary_unary_in_task(self): |
|
|
|
coro_started = asyncio.Event() |
|
|
|
# coro_started = asyncio.Event() |
|
|
|
call = self._stub.EmptyCall(messages_pb2.SimpleRequest()) |
|
|
|
# call = self._stub.EmptyCall(messages_pb2.SimpleRequest()) |
|
|
|
|
|
|
|
|
|
|
|
async def another_coro(): |
|
|
|
# async def another_coro(): |
|
|
|
coro_started.set() |
|
|
|
# coro_started.set() |
|
|
|
await call |
|
|
|
# await call |
|
|
|
|
|
|
|
|
|
|
|
task = self.loop.create_task(another_coro()) |
|
|
|
# task = self.loop.create_task(another_coro()) |
|
|
|
await coro_started.wait() |
|
|
|
# await coro_started.wait() |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(task.done()) |
|
|
|
# self.assertFalse(task.done()) |
|
|
|
task.cancel() |
|
|
|
# task.cancel() |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await task |
|
|
|
# await task |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestUnaryStreamCall(_MulticallableTestMixin, AioTestBase): |
|
|
|
class TestUnaryStreamCall(_MulticallableTestMixin, AioTestBase): |
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel_unary_stream(self): |
|
|
|
# async def test_cancel_unary_stream(self): |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters( |
|
|
|
# messages_pb2.ResponseParameters( |
|
|
|
size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
# size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
# interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
)) |
|
|
|
# )) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
self.assertFalse(call.cancelled()) |
|
|
|
# self.assertFalse(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
response = await call.read() |
|
|
|
# response = await call.read() |
|
|
|
self.assertIs(type(response), messages_pb2.StreamingOutputCallResponse) |
|
|
|
# self.assertIs(type(response), messages_pb2.StreamingOutputCallResponse) |
|
|
|
self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
# self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.cancel()) |
|
|
|
# self.assertTrue(call.cancel()) |
|
|
|
self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
self.assertEqual(_LOCAL_CANCEL_DETAILS_EXPECTATION, await |
|
|
|
# self.assertEqual(_LOCAL_CANCEL_DETAILS_EXPECTATION, await |
|
|
|
call.details()) |
|
|
|
# call.details()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await call.read() |
|
|
|
# await call.read() |
|
|
|
self.assertTrue(call.cancelled()) |
|
|
|
# self.assertTrue(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_multiple_cancel_unary_stream(self): |
|
|
|
# async def test_multiple_cancel_unary_stream(self): |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters( |
|
|
|
# messages_pb2.ResponseParameters( |
|
|
|
size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
# size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
# interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
)) |
|
|
|
# )) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
self.assertFalse(call.cancelled()) |
|
|
|
# self.assertFalse(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
response = await call.read() |
|
|
|
# response = await call.read() |
|
|
|
self.assertIs(type(response), messages_pb2.StreamingOutputCallResponse) |
|
|
|
# self.assertIs(type(response), messages_pb2.StreamingOutputCallResponse) |
|
|
|
self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
# self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.cancel()) |
|
|
|
# self.assertTrue(call.cancel()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await call.read() |
|
|
|
# await call.read() |
|
|
|
|
|
|
|
|
|
|
|
async def test_early_cancel_unary_stream(self): |
|
|
|
# async def test_early_cancel_unary_stream(self): |
|
|
|
"""Test cancellation before receiving messages.""" |
|
|
|
# """Test cancellation before receiving messages.""" |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters( |
|
|
|
# messages_pb2.ResponseParameters( |
|
|
|
size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
# size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
# interval_us=_RESPONSE_INTERVAL_US, |
|
|
|
)) |
|
|
|
# )) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(call.cancelled()) |
|
|
|
# self.assertFalse(call.cancelled()) |
|
|
|
self.assertTrue(call.cancel()) |
|
|
|
# self.assertTrue(call.cancel()) |
|
|
|
self.assertFalse(call.cancel()) |
|
|
|
# self.assertFalse(call.cancel()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await call.read() |
|
|
|
# await call.read() |
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(call.cancelled()) |
|
|
|
# self.assertTrue(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
self.assertEqual(_LOCAL_CANCEL_DETAILS_EXPECTATION, await |
|
|
|
# self.assertEqual(_LOCAL_CANCEL_DETAILS_EXPECTATION, await |
|
|
|
call.details()) |
|
|
|
# call.details()) |
|
|
|
|
|
|
|
|
|
|
|
async def test_late_cancel_unary_stream(self): |
|
|
|
# async def test_late_cancel_unary_stream(self): |
|
|
|
"""Test cancellation after received all messages.""" |
|
|
|
# """Test cancellation after received all messages.""" |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
# messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
|
|
|
|
|
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
response = await call.read() |
|
|
|
# response = await call.read() |
|
|
|
self.assertIs(type(response), |
|
|
|
# self.assertIs(type(response), |
|
|
|
messages_pb2.StreamingOutputCallResponse) |
|
|
|
# messages_pb2.StreamingOutputCallResponse) |
|
|
|
self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
# self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
|
|
|
|
|
|
|
|
# After all messages received, it is possible that the final state |
|
|
|
# # After all messages received, it is possible that the final state |
|
|
|
# is received or on its way. It's basically a data race, so our |
|
|
|
# # is received or on its way. It's basically a data race, so our |
|
|
|
# expectation here is do not crash :) |
|
|
|
# # expectation here is do not crash :) |
|
|
|
call.cancel() |
|
|
|
# call.cancel() |
|
|
|
self.assertIn(await call.code(), |
|
|
|
# self.assertIn(await call.code(), |
|
|
|
[grpc.StatusCode.OK, grpc.StatusCode.CANCELLED]) |
|
|
|
# [grpc.StatusCode.OK, grpc.StatusCode.CANCELLED]) |
|
|
|
|
|
|
|
|
|
|
|
async def test_too_many_reads_unary_stream(self): |
|
|
|
# async def test_too_many_reads_unary_stream(self): |
|
|
|
"""Test calling read after received all messages fails.""" |
|
|
|
# """Test calling read after received all messages fails.""" |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
# messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
|
|
|
|
|
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
response = await call.read() |
|
|
|
# response = await call.read() |
|
|
|
self.assertIs(type(response), |
|
|
|
# self.assertIs(type(response), |
|
|
|
messages_pb2.StreamingOutputCallResponse) |
|
|
|
# messages_pb2.StreamingOutputCallResponse) |
|
|
|
self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
# self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
self.assertIs(await call.read(), aio.EOF) |
|
|
|
# self.assertIs(await call.read(), aio.EOF) |
|
|
|
|
|
|
|
|
|
|
|
# After the RPC is finished, further reads will lead to exception. |
|
|
|
# # After the RPC is finished, further reads will lead to exception. |
|
|
|
self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
# self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
self.assertIs(await call.read(), aio.EOF) |
|
|
|
# self.assertIs(await call.read(), aio.EOF) |
|
|
|
|
|
|
|
|
|
|
|
async def test_unary_stream_async_generator(self): |
|
|
|
# async def test_unary_stream_async_generator(self): |
|
|
|
"""Sunny day test case for unary_stream.""" |
|
|
|
# """Sunny day test case for unary_stream.""" |
|
|
|
# Prepares the request |
|
|
|
# # Prepares the request |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
# for _ in range(_NUM_STREAM_RESPONSES): |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
# messages_pb2.ResponseParameters(size=_RESPONSE_PAYLOAD_SIZE,)) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
self.assertFalse(call.cancelled()) |
|
|
|
# self.assertFalse(call.cancelled()) |
|
|
|
|
|
|
|
|
|
|
|
async for response in call: |
|
|
|
# async for response in call: |
|
|
|
self.assertIs(type(response), |
|
|
|
# self.assertIs(type(response), |
|
|
|
messages_pb2.StreamingOutputCallResponse) |
|
|
|
# messages_pb2.StreamingOutputCallResponse) |
|
|
|
self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
# self.assertEqual(_RESPONSE_PAYLOAD_SIZE, len(response.payload.body)) |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
# self.assertEqual(await call.code(), grpc.StatusCode.OK) |
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel_unary_stream_in_task_using_read(self): |
|
|
|
# async def test_cancel_unary_stream_in_task_using_read(self): |
|
|
|
coro_started = asyncio.Event() |
|
|
|
# coro_started = asyncio.Event() |
|
|
|
|
|
|
|
|
|
|
|
# Configs the server method to block forever |
|
|
|
# # Configs the server method to block forever |
|
|
|
request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
# request = messages_pb2.StreamingOutputCallRequest() |
|
|
|
request.response_parameters.append( |
|
|
|
# request.response_parameters.append( |
|
|
|
messages_pb2.ResponseParameters( |
|
|
|
# messages_pb2.ResponseParameters( |
|
|
|
size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
# size=_RESPONSE_PAYLOAD_SIZE, |
|
|
|
interval_us=_INFINITE_INTERVAL_US, |
|
|
|
# interval_us=_INFINITE_INTERVAL_US, |
|
|
|
)) |
|
|
|
# )) |
|
|
|
|
|
|
|
|
|
|
|
# Invokes the actual RPC |
|
|
|
# # Invokes the actual RPC |
|
|
|
call = self._stub.StreamingOutputCall(request) |
|
|
|
# call = self._stub.StreamingOutputCall(request) |
|
|
|
|
|
|
|
|
|
|
|
async def another_coro(): |
|
|
|
# async def another_coro(): |
|
|
|
coro_started.set() |
|
|
|
# coro_started.set() |
|
|
|
await call.read() |
|
|
|
# await call.read() |
|
|
|
|
|
|
|
|
|
|
|
task = self.loop.create_task(another_coro()) |
|
|
|
# task = self.loop.create_task(another_coro()) |
|
|
|
await coro_started.wait() |
|
|
|
# await coro_started.wait() |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(task.done()) |
|
|
|
# self.assertFalse(task.done()) |
|
|
|
task.cancel() |
|
|
|
# task.cancel() |
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
# self.assertEqual(grpc.StatusCode.CANCELLED, await call.code()) |
|
|
|
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError): |
|
|
|
# with self.assertRaises(asyncio.CancelledError): |
|
|
|
await task |
|
|
|
# await task |
|
|
|
|
|
|
|
|
|
|
|
async def test_cancel_unary_stream_in_task_using_async_for(self): |
|
|
|
async def test_cancel_unary_stream_in_task_using_async_for(self): |
|
|
|
coro_started = asyncio.Event() |
|
|
|
coro_started = asyncio.Event() |
|
|
@ -755,5 +755,5 @@ class TestStreamStreamCall(_MulticallableTestMixin, AioTestBase): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
if __name__ == '__main__': |
|
|
|
logging.basicConfig() |
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
unittest.main(verbosity=2) |
|
|
|
unittest.main(verbosity=2) |
|
|
|