_debug_example_test.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import logging
  19. import unittest
  20. from contextlib import contextmanager
  21. import socket
  22. from examples.python.debug import debug_server
  23. from examples.python.debug import send_message
  24. from examples.python.debug import get_stats
  25. _LOGGER = logging.getLogger(__name__)
  26. _LOGGER.setLevel(logging.INFO)
  27. _FAILURE_RATE = 0.5
  28. _NUMBER_OF_MESSAGES = 100
  29. _ADDR_TEMPLATE = 'localhost:%d'
  30. class DebugExampleTest(unittest.TestCase):
  31. def test_channelz_example(self):
  32. server = debug_server.create_server(
  33. addr='[::]:0', failure_rate=_FAILURE_RATE)
  34. port = server.add_insecure_port('[::]:0')
  35. server.start()
  36. address = _ADDR_TEMPLATE % port
  37. send_message.run(addr=address, n=_NUMBER_OF_MESSAGES)
  38. get_stats.run(addr=address)
  39. server.stop(None)
  40. # No unhandled exception raised, test passed!
  41. if __name__ == '__main__':
  42. logging.basicConfig()
  43. unittest.main(verbosity=2)