Refs #25328 -- Refactored LiveServerTestCase to make it extensible.

This commit is contained in:
Jakub Gocławski 2015-08-30 14:27:38 +02:00 committed by Tim Graham
parent b929d2d09d
commit a7901c2e09
1 changed files with 14 additions and 5 deletions

View File

@ -1251,8 +1251,7 @@ class LiveServerThread(threading.Thread):
# one that is free to use for the WSGI server. # one that is free to use for the WSGI server.
for index, port in enumerate(self.possible_ports): for index, port in enumerate(self.possible_ports):
try: try:
self.httpd = WSGIServer( self.httpd = self._create_server(port)
(self.host, port), QuietWSGIRequestHandler)
except socket.error as e: except socket.error as e:
if (index + 1 < len(self.possible_ports) and if (index + 1 < len(self.possible_ports) and
e.errno == errno.EADDRINUSE): e.errno == errno.EADDRINUSE):
@ -1276,6 +1275,9 @@ class LiveServerThread(threading.Thread):
self.error = e self.error = e
self.is_ready.set() self.is_ready.set()
def _create_server(self, port):
return WSGIServer((self.host, port), QuietWSGIRequestHandler)
def terminate(self): def terminate(self):
if hasattr(self, 'httpd'): if hasattr(self, 'httpd'):
# Stop the WSGI server # Stop the WSGI server
@ -1338,9 +1340,7 @@ class LiveServerTestCase(TransactionTestCase):
except Exception: except Exception:
msg = 'Invalid address ("%s") for live server.' % specified_address msg = 'Invalid address ("%s") for live server.' % specified_address
six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), sys.exc_info()[2]) six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), sys.exc_info()[2])
cls.server_thread = LiveServerThread(host, possible_ports, cls.server_thread = cls._create_server_thread(host, possible_ports, connections_override)
cls.static_handler,
connections_override=connections_override)
cls.server_thread.daemon = True cls.server_thread.daemon = True
cls.server_thread.start() cls.server_thread.start()
@ -1352,6 +1352,15 @@ class LiveServerTestCase(TransactionTestCase):
cls._tearDownClassInternal() cls._tearDownClassInternal()
raise cls.server_thread.error raise cls.server_thread.error
@classmethod
def _create_server_thread(cls, host, possible_ports, connections_override):
return LiveServerThread(
host,
possible_ports,
cls.static_handler,
connections_override=connections_override,
)
@classmethod @classmethod
def _tearDownClassInternal(cls): def _tearDownClassInternal(cls):
# There may not be a 'server_thread' attribute if setUpClass() for some # There may not be a 'server_thread' attribute if setUpClass() for some