close_channel_test.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright 2020 The gRPC Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tests behavior of closing a grpc.aio.Channel."""
  15. import asyncio
  16. import logging
  17. import unittest
  18. import grpc
  19. from grpc.experimental import aio
  20. from grpc.experimental.aio import _base_call
  21. from src.proto.grpc.testing import messages_pb2, test_pb2_grpc
  22. from tests_aio.unit._test_base import AioTestBase
  23. from tests_aio.unit._test_server import start_test_server
  24. _UNARY_CALL_METHOD_WITH_SLEEP = '/grpc.testing.TestService/UnaryCallWithSleep'
  25. _LONG_TIMEOUT_THAT_SHOULD_NOT_EXPIRE = 60
  26. class TestCloseChannel(AioTestBase):
  27. async def setUp(self):
  28. self._server_target, self._server = await start_test_server()
  29. async def tearDown(self):
  30. await self._server.stop(None)
  31. async def test_graceful_close(self):
  32. channel = aio.insecure_channel(self._server_target)
  33. UnaryCallWithSleep = channel.unary_unary(
  34. _UNARY_CALL_METHOD_WITH_SLEEP,
  35. request_serializer=messages_pb2.SimpleRequest.SerializeToString,
  36. response_deserializer=messages_pb2.SimpleResponse.FromString,
  37. )
  38. call = UnaryCallWithSleep(messages_pb2.SimpleRequest())
  39. await channel.close(grace=_LONG_TIMEOUT_THAT_SHOULD_NOT_EXPIRE)
  40. self.assertEqual(grpc.StatusCode.OK, await call.code())
  41. async def test_none_graceful_close(self):
  42. channel = aio.insecure_channel(self._server_target)
  43. UnaryCallWithSleep = channel.unary_unary(
  44. _UNARY_CALL_METHOD_WITH_SLEEP,
  45. request_serializer=messages_pb2.SimpleRequest.SerializeToString,
  46. response_deserializer=messages_pb2.SimpleResponse.FromString,
  47. )
  48. call = UnaryCallWithSleep(messages_pb2.SimpleRequest())
  49. await channel.close(None)
  50. self.assertEqual(grpc.StatusCode.CANCELLED, await call.code())
  51. async def test_close_unary_unary(self):
  52. channel = aio.insecure_channel(self._server_target)
  53. stub = test_pb2_grpc.TestServiceStub(channel)
  54. calls = [stub.UnaryCall(messages_pb2.SimpleRequest()) for _ in range(2)]
  55. await channel.close()
  56. for call in calls:
  57. self.assertTrue(call.cancelled())
  58. async def test_close_unary_stream(self):
  59. channel = aio.insecure_channel(self._server_target)
  60. stub = test_pb2_grpc.TestServiceStub(channel)
  61. request = messages_pb2.StreamingOutputCallRequest()
  62. calls = [stub.StreamingOutputCall(request) for _ in range(2)]
  63. await channel.close()
  64. for call in calls:
  65. self.assertTrue(call.cancelled())
  66. async def test_close_stream_unary(self):
  67. channel = aio.insecure_channel(self._server_target)
  68. stub = test_pb2_grpc.TestServiceStub(channel)
  69. calls = [stub.StreamingInputCall() for _ in range(2)]
  70. await channel.close()
  71. for call in calls:
  72. self.assertTrue(call.cancelled())
  73. async def test_close_stream_stream(self):
  74. channel = aio.insecure_channel(self._server_target)
  75. stub = test_pb2_grpc.TestServiceStub(channel)
  76. calls = [stub.FullDuplexCall() for _ in range(2)]
  77. await channel.close()
  78. for call in calls:
  79. self.assertTrue(call.cancelled())
  80. async def test_close_async_context(self):
  81. async with aio.insecure_channel(self._server_target) as channel:
  82. stub = test_pb2_grpc.TestServiceStub(channel)
  83. calls = [
  84. stub.UnaryCall(messages_pb2.SimpleRequest()) for _ in range(2)
  85. ]
  86. for call in calls:
  87. self.assertTrue(call.cancelled())
  88. async def test_channel_isolation(self):
  89. async with aio.insecure_channel(self._server_target) as channel1:
  90. async with aio.insecure_channel(self._server_target) as channel2:
  91. stub1 = test_pb2_grpc.TestServiceStub(channel1)
  92. stub2 = test_pb2_grpc.TestServiceStub(channel2)
  93. call1 = stub1.UnaryCall(messages_pb2.SimpleRequest())
  94. call2 = stub2.UnaryCall(messages_pb2.SimpleRequest())
  95. self.assertFalse(call1.cancelled())
  96. self.assertTrue(call2.cancelled())
  97. if __name__ == '__main__':
  98. logging.basicConfig(level=logging.INFO)
  99. unittest.main(verbosity=2)