Back to index

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

List of all members.

Public Member Functions

def setUp
def tearDown
def get_output
def test_output
def assert_log_lines
def next_message

Public Attributes

 tcpserver
 port
 threads
 sock_hdlr
 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

Detailed Description

Test for SocketHandler objects.

Definition at line 933 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:

Get the log output as received by the TCP server.

Definition at line 965 of file test_logging.py.

00965 
00966     def get_output(self):
00967         """Get the log output as received by the TCP server."""
00968         # Signal the TCP receiver and wait for it to terminate.
00969         self.root_logger.critical(LogRecordStreamHandler.TCP_LOG_END)
00970         self.tcpserver.finished.wait(2.0)
00971         return self.tcpserver.log_output

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:

Set up a TCP server to receive log messages, and a SocketHandler
pointing to that server's address and port.

Reimplemented from test.test_logging.BaseTest.

Definition at line 937 of file test_logging.py.

00937 
00938     def setUp(self):
00939         """Set up a TCP server to receive log messages, and a SocketHandler
00940         pointing to that server's address and port."""
00941         BaseTest.setUp(self)
00942         self.tcpserver = LogRecordSocketReceiver(port=0)
00943         self.port = self.tcpserver.socket.getsockname()[1]
00944         self.threads = [
00945                 threading.Thread(target=self.tcpserver.serve_until_stopped)]
00946         for thread in self.threads:
00947             thread.start()
00948 
00949         self.sock_hdlr = logging.handlers.SocketHandler('localhost', self.port)
00950         self.sock_hdlr.setFormatter(self.root_formatter)
00951         self.root_logger.removeHandler(self.root_logger.handlers[0])
00952         self.root_logger.addHandler(self.sock_hdlr)

Here is the caller graph for this function:

Shutdown the TCP server.

Reimplemented from test.test_logging.BaseTest.

Definition at line 953 of file test_logging.py.

00953 
00954     def tearDown(self):
00955         """Shutdown the TCP server."""
00956         try:
00957             self.tcpserver.abort = True
00958             del self.tcpserver
00959             self.root_logger.removeHandler(self.sock_hdlr)
00960             self.sock_hdlr.close()
00961             for thread in self.threads:
00962                 thread.join(2.0)
00963         finally:
00964             BaseTest.tearDown(self)

Here is the caller graph for this function:

Definition at line 972 of file test_logging.py.

00972 
00973     def test_output(self):
00974         # The log message sent to the SocketHandler is properly received.
00975         logger = logging.getLogger("tcp")
00976         logger.error("spam")
00977         logger.debug("eggs")
00978         self.assertEqual(self.get_output(), "spam\neggs\n")
00979 

Here is the call graph for this function:


Member Data Documentation

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 942 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 948 of file test_logging.py.

Definition at line 89 of file test_logging.py.

Definition at line 941 of file test_logging.py.

Definition at line 943 of file test_logging.py.


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