_logging_test.py
Go to the documentation of this file.
1 # Copyright 2018 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 of gRPC Python's interaction with the python logging module"""
15 
16 import logging
17 import subprocess
18 import sys
19 import unittest
20 
21 import grpc
22 
23 INTERPRETER = sys.executable
24 
25 
26 class LoggingTest(unittest.TestCase):
27 
29  script = """if True:
30  import logging
31 
32  import grpc
33 
34  if len(logging.getLogger().handlers) != 0:
35  raise Exception('expected 0 logging handlers')
36 
37  """
38  self._verifyScriptSucceeds(script)
39 
40  def test_handler_found(self):
41  script = """if True:
42  import logging
43 
44  import grpc
45  """
46  out, err = self._verifyScriptSucceeds(script)
47  self.assertEqual(0, len(err), 'unexpected output to stderr')
48 
50  script = """if True:
51  import logging
52  import six
53 
54  import grpc
55 
56 
57  intended_stream = six.StringIO()
58  logging.basicConfig(stream=intended_stream)
59 
60  if len(logging.getLogger().handlers) != 1:
61  raise Exception('expected 1 logging handler')
62 
63  if logging.getLogger().handlers[0].stream is not intended_stream:
64  raise Exception('wrong handler stream')
65 
66  """
67  self._verifyScriptSucceeds(script)
68 
69  def test_grpc_logger(self):
70  script = """if True:
71  import logging
72 
73  import grpc
74 
75  if "grpc" not in logging.Logger.manager.loggerDict:
76  raise Exception('grpc logger not found')
77 
78  root_logger = logging.getLogger("grpc")
79  if len(root_logger.handlers) != 1:
80  raise Exception('expected 1 root logger handler')
81  if not isinstance(root_logger.handlers[0], logging.NullHandler):
82  raise Exception('expected logging.NullHandler')
83 
84  """
85  self._verifyScriptSucceeds(script)
86 
87  def _verifyScriptSucceeds(self, script):
88  process = subprocess.Popen([INTERPRETER, '-c', script],
89  stdout=subprocess.PIPE,
90  stderr=subprocess.PIPE)
91  out, err = process.communicate()
92  self.assertEqual(
93  0, process.returncode,
94  'process failed with exit code %d (stdout: %s, stderr: %s)' %
95  (process.returncode, out, err))
96  return out, err
97 
98 
99 if __name__ == '__main__':
100  unittest.main(verbosity=2)
tests.unit._logging_test.LoggingTest.test_handler_found
def test_handler_found(self)
Definition: _logging_test.py:40
tests.unit._logging_test.LoggingTest._verifyScriptSucceeds
def _verifyScriptSucceeds(self, script)
Definition: _logging_test.py:87
tests.unit._logging_test.LoggingTest
Definition: _logging_test.py:26
tests.unit._logging_test.LoggingTest.test_grpc_logger
def test_grpc_logger(self)
Definition: _logging_test.py:69
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
tests.unit._logging_test.LoggingTest.test_can_configure_logger
def test_can_configure_logger(self)
Definition: _logging_test.py:49
tests.unit._logging_test.LoggingTest.test_logger_not_occupied
def test_logger_not_occupied(self)
Definition: _logging_test.py:28


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:27