-# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2017 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
"""Test harness for the logging module. Run all tests.
-Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2017 Vinay Sajip. All Rights Reserved.
"""
import logging
"""Set up a TCP server to receive log messages, and a SocketHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_socket, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sock_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_socket, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.SocketHandler
if isinstance(server.server_address, tuple):
def tearDown(self):
"""Shutdown the TCP server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sock_hdlr)
- self.sock_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sock_hdlr:
+ self.root_logger.removeHandler(self.sock_hdlr)
+ self.sock_hdlr.close()
finally:
BaseTest.tearDown(self)
def test_output(self):
# The log message sent to the SocketHandler is properly received.
+ if self.server_exception:
+ self.skipTest(self.server_exception)
logger = logging.getLogger("tcp")
logger.error("spam")
self.handled.acquire()
self.assertEqual(self.log_output, "spam\neggs\n")
def test_noserver(self):
+ if self.server_exception:
+ self.skipTest(self.server_exception)
# Avoid timing-related failures due to SocketHandler's own hard-wired
# one-second timeout on socket.create_connection() (issue #16264).
self.sock_hdlr.retryStart = 2.5
def tearDown(self):
SocketHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class DatagramHandlerTest(BaseTest):
"""Set up a UDP server to receive log messages, and a DatagramHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_datagram, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sock_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_datagram, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.DatagramHandler
if isinstance(server.server_address, tuple):
def tearDown(self):
"""Shutdown the UDP server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sock_hdlr)
- self.sock_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sock_hdlr:
+ self.root_logger.removeHandler(self.sock_hdlr)
+ self.sock_hdlr.close()
finally:
BaseTest.tearDown(self)
def test_output(self):
# The log message sent to the DatagramHandler is properly received.
+ if self.server_exception:
+ self.skipTest(self.server_exception)
logger = logging.getLogger("udp")
logger.error("spam")
self.handled.wait()
def tearDown(self):
DatagramHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class SysLogHandlerTest(BaseTest):
"""Set up a UDP server to receive log messages, and a SysLogHandler
pointing to that server's address and port."""
BaseTest.setUp(self)
- self.server = server = self.server_class(self.address,
- self.handle_datagram, 0.01)
- server.start()
+ # Issue #29177: deal with errors that happen during setup
+ self.server = self.sl_hdlr = self.server_exception = None
+ try:
+ self.server = server = self.server_class(self.address,
+ self.handle_datagram, 0.01)
+ server.start()
+ # Uncomment next line to test error recovery in setUp()
+ # raise OSError('dummy error raised')
+ except OSError as e:
+ self.server_exception = e
+ return
server.ready.wait()
hcls = logging.handlers.SysLogHandler
if isinstance(server.server_address, tuple):
self.handled = threading.Event()
def tearDown(self):
- """Shutdown the UDP server."""
+ """Shutdown the server."""
try:
- self.server.stop(2.0)
- self.root_logger.removeHandler(self.sl_hdlr)
- self.sl_hdlr.close()
+ if self.server:
+ self.server.stop(2.0)
+ if self.sl_hdlr:
+ self.root_logger.removeHandler(self.sl_hdlr)
+ self.sl_hdlr.close()
finally:
BaseTest.tearDown(self)
self.handled.set()
def test_output(self):
+ if self.server_exception:
+ self.skipTest(self.server_exception)
# The log message sent to the SysLogHandler is properly received.
logger = logging.getLogger("slh")
logger.error("sp\xe4m")
def tearDown(self):
SysLogHandlerTest.tearDown(self)
- os.remove(self.address)
+ support.unlink(self.address)
@unittest.skipUnless(threading, 'Threading required for this test.')
class HTTPHandlerTest(BaseTest):