xref: /aosp_15_r20/external/grpc-grpc/examples/python/errors/test/_error_handling_example_test.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 of the error handling example."""
15
16# NOTE(lidiz) This module only exists in Bazel BUILD file, for more details
17# please refer to comments in the "bazel_namespace_package_hack" module.
18try:
19    from tests import bazel_namespace_package_hack
20
21    bazel_namespace_package_hack.sys_path_to_site_dir_hack()
22except ImportError:
23    pass
24
25import logging
26import unittest
27
28import grpc
29
30from examples.protos import helloworld_pb2_grpc
31from examples.python.errors import client as error_handling_client
32from examples.python.errors import server as error_handling_server
33
34
35class ErrorHandlingExampleTest(unittest.TestCase):
36    def setUp(self):
37        self._server, port = error_handling_server.create_server("[::]:0")
38        self._server.start()
39        self._channel = grpc.insecure_channel("localhost:%d" % port)
40
41    def tearDown(self):
42        self._channel.close()
43        self._server.stop(None)
44
45    def test_error_handling_example(self):
46        stub = helloworld_pb2_grpc.GreeterStub(self._channel)
47        error_handling_client.process(stub)
48        error_handling_client.process(stub)
49        # No unhandled exception raised, test passed!
50
51
52if __name__ == "__main__":
53    logging.basicConfig()
54    unittest.main(verbosity=2)
55