connectivity_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2019 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 the connectivity state."""
  15. import asyncio
  16. import logging
  17. import threading
  18. import time
  19. import unittest
  20. import grpc
  21. from grpc.experimental import aio
  22. from tests.unit.framework.common import test_constants
  23. from tests_aio.unit import _common
  24. from tests_aio.unit._constants import UNREACHABLE_TARGET
  25. from tests_aio.unit._test_base import AioTestBase
  26. from tests_aio.unit._test_server import start_test_server
  27. class TestConnectivityState(AioTestBase):
  28. async def setUp(self):
  29. self._server_address, self._server = await start_test_server()
  30. async def tearDown(self):
  31. await self._server.stop(None)
  32. async def test_unavailable_backend(self):
  33. async with aio.insecure_channel(UNREACHABLE_TARGET) as channel:
  34. self.assertEqual(grpc.ChannelConnectivity.IDLE,
  35. channel.get_state(False))
  36. self.assertEqual(grpc.ChannelConnectivity.IDLE,
  37. channel.get_state(True))
  38. # Should not time out
  39. await asyncio.wait_for(
  40. _common.block_until_certain_state(
  41. channel, grpc.ChannelConnectivity.TRANSIENT_FAILURE),
  42. test_constants.SHORT_TIMEOUT)
  43. async def test_normal_backend(self):
  44. async with aio.insecure_channel(self._server_address) as channel:
  45. current_state = channel.get_state(True)
  46. self.assertEqual(grpc.ChannelConnectivity.IDLE, current_state)
  47. # Should not time out
  48. await asyncio.wait_for(
  49. _common.block_until_certain_state(
  50. channel, grpc.ChannelConnectivity.READY),
  51. test_constants.SHORT_TIMEOUT)
  52. async def test_timeout(self):
  53. async with aio.insecure_channel(self._server_address) as channel:
  54. self.assertEqual(grpc.ChannelConnectivity.IDLE,
  55. channel.get_state(False))
  56. # If timed out, the function should return None.
  57. with self.assertRaises(asyncio.TimeoutError):
  58. await asyncio.wait_for(
  59. _common.block_until_certain_state(
  60. channel, grpc.ChannelConnectivity.READY),
  61. test_constants.SHORT_TIMEOUT)
  62. async def test_shutdown(self):
  63. channel = aio.insecure_channel(self._server_address)
  64. self.assertEqual(grpc.ChannelConnectivity.IDLE,
  65. channel.get_state(False))
  66. # Waiting for changes in a separate coroutine
  67. wait_started = asyncio.Event()
  68. async def a_pending_wait():
  69. wait_started.set()
  70. await channel.wait_for_state_change(grpc.ChannelConnectivity.IDLE)
  71. pending_task = self.loop.create_task(a_pending_wait())
  72. await wait_started.wait()
  73. await channel.close()
  74. self.assertEqual(grpc.ChannelConnectivity.SHUTDOWN,
  75. channel.get_state(True))
  76. self.assertEqual(grpc.ChannelConnectivity.SHUTDOWN,
  77. channel.get_state(False))
  78. # Make sure there isn't any exception in the task
  79. await pending_task
  80. # It can raise exceptions since it is an usage error, but it should not
  81. # segfault or abort.
  82. with self.assertRaises(aio.UsageError):
  83. await channel.wait_for_state_change(
  84. grpc.ChannelConnectivity.SHUTDOWN)
  85. if __name__ == '__main__':
  86. logging.basicConfig(level=logging.DEBUG)
  87. unittest.main(verbosity=2)