_debug_example_test.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. """Test for gRPC Python debug example."""
  15. import asyncio
  16. import logging
  17. import unittest
  18. from examples.python.debug import asyncio_debug_server
  19. from examples.python.debug import asyncio_get_stats
  20. from examples.python.debug import asyncio_send_message
  21. from examples.python.debug import debug_server
  22. from examples.python.debug import get_stats
  23. from examples.python.debug import send_message
  24. _LOGGER = logging.getLogger(__name__)
  25. _LOGGER.setLevel(logging.INFO)
  26. _FAILURE_RATE = 0.5
  27. _NUMBER_OF_MESSAGES = 100
  28. _ADDR_TEMPLATE = 'localhost:%d'
  29. class DebugExampleTest(unittest.TestCase):
  30. def test_channelz_example(self):
  31. server = debug_server.create_server(addr='[::]:0',
  32. failure_rate=_FAILURE_RATE)
  33. port = server.add_insecure_port('[::]:0')
  34. server.start()
  35. address = _ADDR_TEMPLATE % port
  36. send_message.run(addr=address, n=_NUMBER_OF_MESSAGES)
  37. get_stats.run(addr=address)
  38. server.stop(None)
  39. # No unhandled exception raised, test passed!
  40. def test_asyncio_channelz_example(self):
  41. async def body():
  42. server = asyncio_debug_server.create_server(
  43. addr='[::]:0', failure_rate=_FAILURE_RATE)
  44. port = server.add_insecure_port('[::]:0')
  45. await server.start()
  46. address = _ADDR_TEMPLATE % port
  47. await asyncio_send_message.run(addr=address, n=_NUMBER_OF_MESSAGES)
  48. await asyncio_get_stats.run(addr=address)
  49. await server.stop(None)
  50. # No unhandled exception raised, test passed!
  51. asyncio.get_event_loop().run_until_complete(body())
  52. if __name__ == '__main__':
  53. logging.basicConfig(level=logging.DEBUG)
  54. unittest.main(verbosity=2)