Back to index

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

List of all members.

Public Member Functions

def setUp
def test_persistent_loggers
def tearDown
def assert_log_lines
def next_message

Public Attributes

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

Private Member Functions

def _watch_for_survival
def _assertTruesurvival

Private Attributes

 _survivors

Detailed Description

Test memory persistence of logger objects.

Definition at line 980 of file test_logging.py.


Member Function Documentation

Assert that all objects watched for survival have survived.

Definition at line 996 of file test_logging.py.

00996 
00997     def _assertTruesurvival(self):
00998         """Assert that all objects watched for survival have survived."""
00999         # Trigger cycle breaking.
01000         gc.collect()
01001         dead = []
01002         for (id_, repr_), ref in self._survivors.items():
01003             if ref() is None:
01004                 dead.append(repr_)
01005         if dead:
01006             self.fail("%d objects should have survived "
01007                 "but have been destroyed: %s" % (len(dead), ", ".join(dead)))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_logging.MemoryTest._watch_for_survival (   self,
  args 
) [private]
Watch the given objects for survival, by creating weakrefs to
them.

Definition at line 989 of file test_logging.py.

00989 
00990     def _watch_for_survival(self, *args):
00991         """Watch the given objects for survival, by creating weakrefs to
00992         them."""
00993         for obj in args:
00994             key = id(obj), repr(obj)
00995             self._survivors[key] = weakref.ref(obj)

Here is the caller graph for this function:

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:

Create a dict to remember potentially destroyed objects.

Reimplemented from test.test_logging.BaseTest.

Definition at line 984 of file test_logging.py.

00984 
00985     def setUp(self):
00986         """Create a dict to remember potentially destroyed objects."""
00987         BaseTest.setUp(self)
00988         self._survivors = {}

Here is the caller graph for this function:

def test.test_logging.BaseTest.tearDown (   self) [inherited]
Remove our logging stream, and restore the original logging
level.

Reimplemented in test.test_logging.BaseFileTest, test.test_logging.QueueHandlerTest, test.test_logging.LogRecordFactoryTest, test.test_logging.SocketHandlerTest, and test.test_logging.MemoryHandlerTest.

Definition at line 104 of file test_logging.py.

00104 
00105     def tearDown(self):
00106         """Remove our logging stream, and restore the original logging
00107         level."""
00108         self.stream.close()
00109         self.root_logger.removeHandler(self.root_hdlr)
00110         while self.root_logger.handlers:
00111             h = self.root_logger.handlers[0]
00112             self.root_logger.removeHandler(h)
00113             h.close()
00114         self.root_logger.setLevel(self.original_logging_level)
00115         logging._acquireLock()
00116         try:
00117             logging._levelNames.clear()
00118             logging._levelNames.update(self.saved_level_names)
00119             logging._handlers.clear()
00120             logging._handlers.update(self.saved_handlers)
00121             logging._handlerList[:] = self.saved_handler_list
00122             loggerDict = logging.getLogger().manager.loggerDict
00123             loggerDict.clear()
00124             loggerDict.update(self.saved_loggers)
00125             logger_states = self.logger_states
00126             for name in self.logger_states:
00127                 if logger_states[name] is not None:
00128                     self.saved_loggers[name].disabled = logger_states[name]
00129         finally:
00130             logging._releaseLock()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1008 of file test_logging.py.

01008 
01009     def test_persistent_loggers(self):
01010         # Logger objects are persistent and retain their configuration, even
01011         #  if visible references are destroyed.
01012         self.root_logger.setLevel(logging.INFO)
01013         foo = logging.getLogger("foo")
01014         self._watch_for_survival(foo)
01015         foo.setLevel(logging.DEBUG)
01016         self.root_logger.debug(self.next_message())
01017         foo.debug(self.next_message())
01018         self.assert_log_lines([
01019             ('foo', 'DEBUG', '2'),
01020         ])
01021         del foo
01022         # foo has survived.
01023         self._assertTruesurvival()
01024         # foo has retained its settings.
01025         bar = logging.getLogger("foo")
01026         bar.debug(self.next_message())
01027         self.assert_log_lines([
01028             ('foo', 'DEBUG', '2'),
01029             ('foo', 'DEBUG', '3'),
01030         ])
01031 

Here is the call graph for this function:


Member Data Documentation

Definition at line 987 of file test_logging.py.

string test.test_logging.BaseTest.expected_log_pat = r"^([\w.]+) -> ([\w]+): ([\d]+)$" [static, inherited]
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 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: