|
|
|
@ -20,7 +20,6 @@ from grpc.experimental import aio |
|
|
|
|
from tests_aio.unit._test_base import AioTestBase |
|
|
|
|
from tests.unit.framework.common import test_constants |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_SIMPLE_UNARY_UNARY = '/test/SimpleUnaryUnary' |
|
|
|
|
_BLOCK_FOREVER = '/test/BlockForever' |
|
|
|
|
_BLOCK_SHORTLY = '/test/BlockShortly' |
|
|
|
@ -30,6 +29,7 @@ _RESPONSE = b'\x01\x01\x01' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _GenericHandler(grpc.GenericRpcHandler): |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
self._called = asyncio.get_event_loop().create_future() |
|
|
|
|
|
|
|
|
@ -40,9 +40,8 @@ class _GenericHandler(grpc.GenericRpcHandler): |
|
|
|
|
async def _block_forever(self, unused_request, unused_context): |
|
|
|
|
await asyncio.get_event_loop().create_future() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _block_shortly(self, unused_request, unused_context): |
|
|
|
|
await asyncio.sleep(test_constants.SHORT_TIMEOUT/2) |
|
|
|
|
await asyncio.sleep(test_constants.SHORT_TIMEOUT / 2) |
|
|
|
|
return _RESPONSE |
|
|
|
|
|
|
|
|
|
def service(self, handler_details): |
|
|
|
@ -70,6 +69,7 @@ async def _start_test_server(): |
|
|
|
|
class TestServer(AioTestBase): |
|
|
|
|
|
|
|
|
|
def test_unary_unary(self): |
|
|
|
|
|
|
|
|
|
async def test_unary_unary_body(): |
|
|
|
|
server_target, _, _ = await _start_test_server() |
|
|
|
|
|
|
|
|
@ -79,14 +79,17 @@ class TestServer(AioTestBase): |
|
|
|
|
self.assertEqual(response, _RESPONSE) |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_unary_unary_body()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_shutdown(self): |
|
|
|
|
|
|
|
|
|
async def test_shutdown_body(): |
|
|
|
|
_, server, _ = await _start_test_server() |
|
|
|
|
await server.stop(None) |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_shutdown_body()) |
|
|
|
|
|
|
|
|
|
def test_shutdown_after_call(self): |
|
|
|
|
|
|
|
|
|
async def test_shutdown_body(): |
|
|
|
|
server_target, server, _ = await _start_test_server() |
|
|
|
|
|
|
|
|
@ -94,50 +97,59 @@ class TestServer(AioTestBase): |
|
|
|
|
await channel.unary_unary(_SIMPLE_UNARY_UNARY)(_REQUEST) |
|
|
|
|
|
|
|
|
|
await server.stop(None) |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_shutdown_body()) |
|
|
|
|
|
|
|
|
|
def test_graceful_shutdown_success(self): |
|
|
|
|
|
|
|
|
|
async def test_graceful_shutdown_success_body(): |
|
|
|
|
server_target, server, generic_handler = await _start_test_server() |
|
|
|
|
|
|
|
|
|
channel = aio.insecure_channel(server_target) |
|
|
|
|
call_task = self.loop.create_task(channel.unary_unary(_BLOCK_SHORTLY)(_REQUEST)) |
|
|
|
|
call_task = self.loop.create_task( |
|
|
|
|
channel.unary_unary(_BLOCK_SHORTLY)(_REQUEST)) |
|
|
|
|
await generic_handler.wait_for_call() |
|
|
|
|
|
|
|
|
|
await server.stop(test_constants.SHORT_TIMEOUT) |
|
|
|
|
await channel.close() |
|
|
|
|
self.assertEqual(await call_task, _RESPONSE) |
|
|
|
|
self.assertTrue(call_task.done()) |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_graceful_shutdown_success_body()) |
|
|
|
|
|
|
|
|
|
def test_graceful_shutdown_failed(self): |
|
|
|
|
|
|
|
|
|
async def test_graceful_shutdown_failed_body(): |
|
|
|
|
server_target, server, generic_handler = await _start_test_server() |
|
|
|
|
|
|
|
|
|
channel = aio.insecure_channel(server_target) |
|
|
|
|
call_task = self.loop.create_task(channel.unary_unary(_BLOCK_FOREVER)(_REQUEST)) |
|
|
|
|
call_task = self.loop.create_task( |
|
|
|
|
channel.unary_unary(_BLOCK_FOREVER)(_REQUEST)) |
|
|
|
|
await generic_handler.wait_for_call() |
|
|
|
|
|
|
|
|
|
await server.stop(test_constants.SHORT_TIMEOUT) |
|
|
|
|
|
|
|
|
|
with self.assertRaises(aio.AioRpcError) as exception_context: |
|
|
|
|
await call_task |
|
|
|
|
self.assertEqual(exception_context.exception.code(), grpc.StatusCode.UNAVAILABLE) |
|
|
|
|
self.assertEqual(exception_context.exception.code(), |
|
|
|
|
grpc.StatusCode.UNAVAILABLE) |
|
|
|
|
self.assertIn('GOAWAY', exception_context.exception.details()) |
|
|
|
|
await channel.close() |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_graceful_shutdown_failed_body()) |
|
|
|
|
|
|
|
|
|
@unittest.skip('https://github.com/grpc/grpc/issues/20818') |
|
|
|
|
def test_shutdown_before_call(self): |
|
|
|
|
|
|
|
|
|
async def test_shutdown_body(): |
|
|
|
|
server_target, server, _ =_start_test_server() |
|
|
|
|
server_target, server, _ = _start_test_server() |
|
|
|
|
await server.stop(None) |
|
|
|
|
|
|
|
|
|
# Ensures the server is cleaned up at this point. |
|
|
|
|
# Some proper exception should be raised. |
|
|
|
|
async with aio.insecure_channel('localhost:%d' % port) as channel: |
|
|
|
|
await channel.unary_unary(_SIMPLE_UNARY_UNARY)(_REQUEST) |
|
|
|
|
|
|
|
|
|
self.loop.run_until_complete(test_shutdown_body()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|