Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_logging.QueueHandlerTest Class Reference
Inheritance diagram for test.test_logging.QueueHandlerTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_logging.QueueHandlerTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def tearDown
def test_queue_handler
def test_queue_listener
def assert_log_lines
def next_message

Public Attributes

 queue
 que_hdlr
 que_logger
 saved_handlers
 saved_handler_list
 saved_loggers
 saved_level_names
 logger_states
 logger1
 logger2
 root_logger
 original_logging_level
 stream
 root_hdlr
 root_formatter

Static Public Attributes

string expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$"
string log_format = "%(name)s -> %(levelname)s: %(message)s"
int message_num = 0

Detailed Description

Definition at line 2067 of file test_logging.py.


Member Function Documentation

def test.test_logging.BaseTest.assert_log_lines (   self,
  expected_values,
  stream = None 
) [inherited]
Match the collected log lines against the regular expression
self.expected_log_pat, and compare the extracted group values to
the expected_values list of tuples.

Definition at line 131 of file test_logging.py.

00131 
00132     def assert_log_lines(self, expected_values, stream=None):
00133         """Match the collected log lines against the regular expression
00134         self.expected_log_pat, and compare the extracted group values to
00135         the expected_values list of tuples."""
00136         stream = stream or self.stream
00137         pat = re.compile(self.expected_log_pat)
00138         try:
00139             stream.reset()
00140             actual_lines = stream.readlines()
00141         except AttributeError:
00142             # StringIO.StringIO lacks a reset() method.
00143             actual_lines = stream.getvalue().splitlines()
00144         self.assertEqual(len(actual_lines), len(expected_values),
00145                           '%s vs. %s' % (actual_lines, expected_values))
00146         for actual, expected in zip(actual_lines, expected_values):
00147             match = pat.search(actual)
00148             if not match:
00149                 self.fail("Log line does not match expected pattern:\n" +
00150                             actual)
00151             self.assertEqual(tuple(match.groups()), expected)
00152         s = stream.read()
00153         if s:
00154             self.fail("Remaining output at end of log stream:\n" + s)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_logging.BaseTest.next_message (   self) [inherited]
Generate a message consisting solely of an auto-incrementing
integer.

Definition at line 155 of file test_logging.py.

00155 
00156     def next_message(self):
00157         """Generate a message consisting solely of an auto-incrementing
00158         integer."""
00159         self.message_num += 1
00160         return "%d" % self.message_num
00161 

Here is the caller graph for this function:

Setup the default logging stream to an internal StringIO instance,
so that we can examine log output as we want.

Reimplemented from test.test_logging.BaseTest.

Definition at line 2071 of file test_logging.py.

02071 
02072     def setUp(self):
02073         BaseTest.setUp(self)
02074         self.queue = queue.Queue(-1)
02075         self.que_hdlr = logging.handlers.QueueHandler(self.queue)
02076         self.que_logger = logging.getLogger('que')
02077         self.que_logger.propagate = False
02078         self.que_logger.setLevel(logging.WARNING)
02079         self.que_logger.addHandler(self.que_hdlr)

Here is the caller graph for this function:

Remove our logging stream, and restore the original logging
level.

Reimplemented from test.test_logging.BaseTest.

Definition at line 2080 of file test_logging.py.

02080 
02081     def tearDown(self):
02082         self.que_hdlr.close()
02083         BaseTest.tearDown(self)

Here is the caller graph for this function:

Definition at line 2084 of file test_logging.py.

02084 
02085     def test_queue_handler(self):
02086         self.que_logger.debug(self.next_message())
02087         self.assertRaises(queue.Empty, self.queue.get_nowait)
02088         self.que_logger.info(self.next_message())
02089         self.assertRaises(queue.Empty, self.queue.get_nowait)
02090         msg = self.next_message()
02091         self.que_logger.warning(msg)
02092         data = self.queue.get_nowait()
02093         self.assertTrue(isinstance(data, logging.LogRecord))
02094         self.assertEqual(data.name, self.que_logger.name)
02095         self.assertEqual((data.msg, data.args), (msg, None))

Here is the call graph for this function:

Definition at line 2098 of file test_logging.py.

02098 
02099     def test_queue_listener(self):
02100         handler = TestHandler(Matcher())
02101         listener = logging.handlers.QueueListener(self.queue, handler)
02102         listener.start()
02103         try:
02104             self.que_logger.warning(self.next_message())
02105             self.que_logger.error(self.next_message())
02106             self.que_logger.critical(self.next_message())
02107         finally:
02108             listener.stop()
02109         self.assertTrue(handler.matches(levelno=logging.WARNING, message='1'))
02110         self.assertTrue(handler.matches(levelno=logging.ERROR, message='2'))
02111         self.assertTrue(handler.matches(levelno=logging.CRITICAL, message='3'))
02112 

Here is the call graph for this function:


Member Data Documentation

string test.test_logging.QueueHandlerTest.expected_log_pat = r"^[\w.]+ -> ([\w]+): ([\d]+)$" [static]

Reimplemented from test.test_logging.BaseTest.

Definition at line 2069 of file test_logging.py.

string test.test_logging.BaseTest.log_format = "%(name)s -> %(levelname)s: %(message)s" [static, inherited]

Definition at line 59 of file test_logging.py.

Definition at line 83 of file test_logging.py.

Definition at line 84 of file test_logging.py.

Definition at line 73 of file test_logging.py.

Definition at line 61 of file test_logging.py.

Definition at line 87 of file test_logging.py.

Definition at line 2074 of file test_logging.py.

Definition at line 2075 of file test_logging.py.

Definition at line 2073 of file test_logging.py.

Definition at line 92 of file test_logging.py.

Definition at line 91 of file test_logging.py.

Definition at line 86 of file test_logging.py.

Definition at line 70 of file test_logging.py.

Definition at line 69 of file test_logging.py.

Definition at line 72 of file test_logging.py.

Definition at line 71 of file test_logging.py.

Definition at line 89 of file test_logging.py.


The documentation for this class was generated from the following file: