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